Thông tin tài liệu
private static IEnumerable GetBaseTypes(this Type type) { Type current = type; while (current != null) { yield return current; current = current.BaseType; } } If you want to obtain only the inheritance hierarchy of a specific type as a string, use the following DisplayInheritanceChain overload: public static void DisplayInheritanceChain(string asmPath,string baseType) { Assembly asm = Assembly.LoadFrom(asmPath); string typeDisplay = asm.GetType(baseType).GetBaseTypeDisplay( ); Console.WriteLine(typeDisplay); } To display the inheritance hierarchy of all types within an assembly, use the first instance of the DisplayInheritanceChain method call To obtain the inheritance hierarchy of a single type as a string, use the GetBaseTypeDisplay extension method on the Type In this instance, you are looking for the type hierarchy of the CSharpRecipes.ReflectionUtils+DerivedOverrides nested class: public static void DisplayInheritanceHierarchyType( ) { Process current = Process.GetCurrentProcess( ); // Get the path of the current module string asmPath = current.MainModule.FileName; // A specific type DisplayInheritanceChain(asmPath, "CSharpRecipes.ReflectionUtils+DerivedOverrides"); // All types in the assembly DisplayInheritanceChain(asmPath); } These methods result in output like the following: Derived Type: CSharpRecipes.Reflection Base Type List: Object
Ngày đăng: 12/08/2014, 09:22
Xem thêm: C# 3.0 Cookbook phần 7 pot, C# 3.0 Cookbook phần 7 pot, 15 Checking Out a Web Server’s Custom Error Pages