using System; using System.Collections.Generic; using System.Linq; namespace Chernobyl.Reflection { /// /// Extension methods for use with .NET reflection. /// public static class Extensions { /// /// Gets the common base type of a /// collection. /// /// References: this method was taken and modified from /// http://stackoverflow.com/questions/353430/easiest-way-to-get-a-common-base-class-from-a-collection-of-types /// /// The collection of types to get the common base /// class of. /// The base type that is common for each of the types passed /// in. If the array passed in has a zero length, then typeof(object) /// is returned. public static Type GetCommonBaseType(this IEnumerable types) { Type result = types.FirstOrDefault(); if (result != null) { result = types.First(); foreach (Type type in types) { if (type.IsAssignableFrom(result)) result = type; else { // This will always terminate when return == typeof(object) while (!result.IsAssignableFrom(type)) result = result.BaseType; } } } else result = typeof(object); return result; } } }