C# 3.0 Cookbook phần 7 pot

88 381 0
C# 3.0 Cookbook phần 7 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

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

Từ khóa liên quan

Mục lục

  • C# 3.0 Cookbook

    • Reflection

      • 13.6 Displaying the Inheritance Hierarchy for a Type

        • Discussion

        • See Also

        • 13.7 Finding the Subclasses of a Type

          • Problem

          • Solution

          • Discussion

          • See Also

          • 13.8 Finding All Serializable Types Within an Assembly

            • Problem

            • Solution

            • Discussion

            • See Also

            • 13.9 Dynamically Invoking Members

              • Problem

              • Solution

              • Discussion

              • See Also

              • 13.10 Determining If a Type or Method Is Generic

                • Problem

                • Solution

                • Discussion

                • See Also

                • 13.11 Accessing Local Variable Information

                  • Problem

                  • Solution

Tài liệu cùng người dùng

Tài liệu liên quan