public static Maybe<Type> GetCommonBaseInterfaceType(IEnumerable<Type> types)
{
return
types.Select(t => t.GetInterfaces().Concat(t))
.Aggregate((a, b) => a.Intersect(b))
.OrderBy(x => x, LambdaComparer<Type>.Comparer(((a, b) =>
{
if (a != b)
{
if (a.IsAssignableFrom(b))
{
return 1;
}
if (b.IsAssignableFrom(a))
{
return -1;
}
}
return 0;
})))
.FirstOrNothing();
}
}