Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 88 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
88
Dung lượng
416,69 KB
Nội dung
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