using System; using System.Collections.Generic; namespace Chernobyl.Reflection { /// /// A class that holds utility methods specifically for reflection. /// public static class Utility { /// /// Get all of the base types and interfaces that the passed in /// inherits from. Note that this method does not /// return the types in any specific order. /// /// The type whose base types and interfaces are to /// be retrieved for. /// A collection of types the passed in /// inherits from. If the passed in is an /// , the type will be returned. public static IEnumerable GetBaseTypes() { return GetBaseTypes(typeof (T)); } /// /// Get all of the base types and interfaces that the passed in /// inherits from. Note that this method does not /// return the types in any specific order. /// /// The whose base types and /// interfaces are to be retrieved for. /// A collection of types the passed in /// inherits from. If the passed in is an /// , the type will be returned. public static IEnumerable GetBaseTypes(Type type) { // yield the interfaces foreach (Type @interface in type.GetInterfaces()) yield return @interface; // yield the base types Type nextType = type.BaseType ?? type; do { yield return nextType; nextType = nextType.BaseType; } while (nextType != null); } } }