查看类型信息

更新:2007 年 11 月

System.Type 类对于反射起着核心的作用。当反射请求加载的类型时,公共语言运行库将为它创建一个 Type。您可以使用 Type 对象的方法、字段、属性和嵌套类来查找有关该类型的所有信息。

使用 Assembly.GetTypeAssembly.GetTypes 从尚未加载的程序集中获取 Type 对象,并传入所需类型的名称。使用 Type.GetType 可从已加载的程序集中获取 Type 对象。使用 Module.GetTypeModule.GetTypes 可获取模块 Type 对象。

说明:

如果想要检查并操作泛型类型和方法,请参见反向和泛型类型如何:使用反射检查和实例化泛型类型 中提供的其他信息。

下面的示例显示在获取程序集的 Assembly 对象和模块时所必需的语法。

' Gets the mscorlib assembly in which the object is defined.
Dim a As Reflection.Assembly = GetType(Object).Module.Assembly
// Gets the mscorlib assembly in which the object is defined.
Assembly a = typeof(Object).Module.Assembly;

下面的示例说明如何从已加载的程序集中获取 Type 对象。

' Loads an assembly using its file name.
Dim a As Reflection.Assembly = Reflection.Assembly.LoadFrom("MyExe.exe")
' Gets the type names from the assembly.
Dim types2 As Type() = a.GetTypes()
Dim t As Type
For Each t In  types2
    Console.WriteLine(t.FullName)
Next t
// Loads an assembly using its file name.
Assembly a = Assembly.LoadFrom ("MyExe.exe");
// Gets the type names from the assembly.
Type [] types2 = a.GetTypes ();
foreach (Type t in types2)
{
    Console.WriteLine (t.FullName);
}

在获取了一个 Type 后,您可以采用许多方法来发现与该类型的成员有关的信息。例如,通过调用 Type.GetMembers 方法(该方法将获取对当前类型的每个成员进行描述的一组 MemberInfo 对象),您可以找到有关该类型的所有成员的信息。

您也可以在 Type 类上使用方法,以检索有关按名称指定的一个或多个构造函数、方法、事件、字段或属性的信息。例如,Type.GetConstructor 封装当前类的特定构造函数。

如果具有 Type,则可以使用 Type.Module 属性来获取一个封装该类型所在模块的对象。使用 Module.Assembly 属性可查找封装模块所在程序集的对象。使用 Type.Assembly 属性可直接获取封装该类型的程序集。

System.Type 和 ConstructorInfo

下面的示例演示如何列出一个类(此示例中为 String 类)的构造函数。

' This program lists all the public constructors 
' of the System.String class.
Imports System
Imports System.Reflection
Class ListMembers
    Public Shared Sub Main()
        Dim t As Type = GetType(String)
        Console.WriteLine("Listing all the public constructors of the {0} type", t)
        ' Constructors.
        Dim ci As ConstructorInfo() = t.GetConstructors((BindingFlags.Public Or BindingFlags.Instance))
        Console.WriteLine("//Constructors")
        PrintMembers(ci)
    End Sub
    Public Shared Sub PrintMembers(ms() As MemberInfo)
        Dim m As MemberInfo
        For Each m In ms
            Console.WriteLine("{0}{1}", "     ", m)
        Next m
        Console.WriteLine()
    End Sub
End Class
// This program lists all the public constructors 
// of the System.String class.
using System;
using System.Reflection;
class ListMembers {
    public static void Main(String[] args) {
        Type t = typeof(System.String);
        Console.WriteLine ("Listing all the public constructors of the {0} type", t);
        // Constructors.
        ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
        Console.WriteLine ("//Constructors");
        PrintMembers (ci);
    }
    public static void PrintMembers(MemberInfo [] ms) {
        foreach (MemberInfo m in ms) {
            Console.WriteLine ("{0}{1}", "     ", m);
        }
        Console.WriteLine();
    }
}

MemberInfo、MethodInfo、FieldInfo 和 PropertyInfo

使用 MemberInfoMethodInfoFieldInfoPropertyInfo 对象获取有关该类型的方法、属性、事件和字段的信息。

下面的示例使用 MemberInfo 列出 System.IO.File 类中的成员数量并使用 System.Type.IsPublic 属性确定该类的可见性。

Option Explicit
Option Strict
Imports System
Imports System.IO
Imports System.Reflection
Imports Microsoft.VisualBasic
Class Mymemberinfo
    Public Shared Sub Main()
        Console.WriteLine(ControlChars.Cr & "Reflection.MemberInfo")
        ' Gets the Type and MemberInfo.
        Dim MyType As Type = Type.GetType("System.IO.File")
        Dim Mymemberinfoarray As MemberInfo() = MyType.GetMembers()
        ' Gets and displays the DeclaringType method. 
        Console.WriteLine(ControlChars.Cr & "There are {0} members in {1}.", Mymemberinfoarray.Length, MyType.FullName)
        Console.WriteLine("{0}.", MyType.FullName)
        If MyType.IsPublic Then
            Console.WriteLine("{0} is public.", MyType.FullName)
        End If
    End Sub
End Class
using System;
using System.IO;
using System.Reflection;

class Mymemberinfo
{ 
    public static void Main(string[] args)
    { 
        Console.WriteLine ("\nReflection.MemberInfo");
        // Gets the Type and MemberInfo.
        Type MyType =Type.GetType("System.IO.File");
        MemberInfo[] Mymemberinfoarray = MyType.GetMembers(); 
        // Gets and displays the DeclaringType method. 
        Console.WriteLine("\nThere are {0} members in {1}.", 
            Mymemberinfoarray.Length, MyType.FullName);
        Console.WriteLine("{0}.", MyType.FullName); 
        if (MyType.IsPublic)
        {
            Console.WriteLine("{0} is public.", MyType.FullName);
        }
    }
}

下面的示例调查指定成员的类型。它对 MemberInfo 类的一个成员执行反射,然后列出其类型。

' This code displays information about the GetValue method of FieldInfo.
Option Explicit
Option Strict
Imports System
Imports System.Reflection
Class MyMethodInfo
    Public Shared Sub Main()
        Console.WriteLine("Reflection.MethodInfo")
        ' Gets and displays the Type.
        Dim MyType As Type = Type.GetType("System.Reflection.FieldInfo")
        ' Specifies the member for which you want type information.
        Dim Mymethodinfo As MethodInfo = MyType.GetMethod("GetValue")
        Console.WriteLine((MyType.FullName & "." & Mymethodinfo.Name))
        ' Gets and displays the MemberType property.
        Dim Mymembertypes As MemberTypes = Mymethodinfo.MemberType
        If MemberTypes.Constructor = Mymembertypes Then
            Console.WriteLine("MemberType is of type All")
        ElseIf MemberTypes.Custom = Mymembertypes Then
            Console.WriteLine("MemberType is of type Custom")
        ElseIf MemberTypes.Event = Mymembertypes Then
            Console.WriteLine("MemberType is of type Event")
        ElseIf MemberTypes.Field = Mymembertypes Then
            Console.WriteLine("MemberType is of type Field")
        ElseIf MemberTypes.Method = Mymembertypes Then
            Console.WriteLine("MemberType is of type Method")
        ElseIf MemberTypes.Property = Mymembertypes Then
            Console.WriteLine("MemberType is of type Property")
        ElseIf MemberTypes.TypeInfo = Mymembertypes Then
            Console.WriteLine("MemberType is of type TypeInfo")
        End If
        Return
    End Sub
End Class
// This code displays information about the GetValue method of FieldInfo.
using System;
using System.Reflection;
class MyMethodInfo {
    public static int Main() {
        Console.WriteLine("Reflection.MethodInfo");
        // Gets and displays the Type.
        Type MyType = Type.GetType("System.Reflection.FieldInfo");
        // Specifies the member for which you want type information.
        MethodInfo Mymethodinfo = MyType.GetMethod("GetValue");
        Console.WriteLine(MyType.FullName + "." + Mymethodinfo.Name);
        // Gets and displays the MemberType property.
        MemberTypes Mymembertypes = Mymethodinfo.MemberType;
        if (MemberTypes.Constructor == Mymembertypes) {
            Console.WriteLine("MemberType is of type All"); 
        }
        else if (MemberTypes.Custom == Mymembertypes) {
            Console.WriteLine("MemberType is of type Custom"); 
        }
        else if (MemberTypes.Event == Mymembertypes) {
            Console.WriteLine("MemberType is of type Event"); 
        }
        else if (MemberTypes.Field == Mymembertypes) {
            Console.WriteLine("MemberType is of type Field"); 
        }
        else if (MemberTypes.Method == Mymembertypes) {
            Console.WriteLine("MemberType is of type Method"); 
        }
        else if (MemberTypes.Property == Mymembertypes) {
            Console.WriteLine("MemberType is of type Property"); 
        }
        else if (MemberTypes.TypeInfo == Mymembertypes) {
            Console.WriteLine("MemberType is of type TypeInfo"); 
        }
        return 0;
    }
}

下面的示例使用所有的反射 *Info 类以及 BindingFlags 来列出指定类的所有成员(构造函数、字段、属性、事件和方法),并将这些成员划分为静态和实例类别。

' This program lists all the members of the 
' System.IO.BufferedStream class.
Imports System
Imports System.IO
Imports System.Reflection
Imports Microsoft.VisualBasic
Class ListMembers
    Public Shared Sub Main()
        ' Specifies the class.
        Dim t As Type = GetType(System.IO.BufferedStream)
        Console.WriteLine("Listing all the members (public and non public) of the {0} type", t)
        ' Lists static fields first.
        Dim fi As FieldInfo() = t.GetFields((BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Static Fields")
        PrintMembers(fi)
        ' Static properties.
        Dim pi As PropertyInfo() = t.GetProperties((BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Static Properties")
        PrintMembers(pi)
        ' Static events.
        Dim ei As EventInfo() = t.GetEvents((BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Static Events")
        PrintMembers(ei)
        ' Static methods.
        Dim mi As MethodInfo() = t.GetMethods((BindingFlags.Static Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Static Methods")
        PrintMembers(mi)
        ' Constructors.
        Dim ci As ConstructorInfo() = t.GetConstructors((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Constructors")
        PrintMembers(ci)
        ' Instance fields.
        fi = t.GetFields((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Instance Fields")
        PrintMembers(fi)
        ' Instance properites.
        pi = t.GetProperties((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Instance Properties")
        PrintMembers(pi)
        ' Instance events.
        ei = t.GetEvents((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Instance Events")
        PrintMembers(ei)
        ' Instance methods.
        mi = t.GetMethods((BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public))
        Console.WriteLine("// Instance Methods")
        PrintMembers(mi)
        Console.WriteLine(ControlChars.CrLf & "Press ENTER to exit.")
        Console.Read()
    End Sub
    Public Shared Sub PrintMembers(ms() As MemberInfo)
        Dim m As MemberInfo
        For Each m In  ms
            Console.WriteLine("{0}{1}", "     ", m)
        Next m
        Console.WriteLine()
    End Sub
End Class
// This program lists all the members of the 
// System.IO.BufferedStream class.
using System;
using System.IO;
using System.Reflection;

class ListMembers {
    public static void Main(String[] args) {
        // Specifies the class.
        Type t = typeof (System.IO.BufferedStream);
        Console.WriteLine ("Listing all the members (public and non public) of the {0} type", t);

        // Lists static fields first.
        FieldInfo [] fi = t.GetFields (BindingFlags.Static | 
         BindingFlags.NonPublic | BindingFlags.Public);
        Console.WriteLine ("// Static Fields");
        PrintMembers (fi);

        // Static properties.
        PropertyInfo [] pi = t.GetProperties (BindingFlags.Static | 
         BindingFlags.NonPublic | BindingFlags.Public);
        Console.WriteLine ("// Static Properties");
        PrintMembers (pi);

        // Static events.
        EventInfo [] ei = t.GetEvents (BindingFlags.Static | 
         BindingFlags.NonPublic | BindingFlags.Public);
        Console.WriteLine ("// Static Events");
        PrintMembers (ei);

        // Static methods.
        MethodInfo [] mi = t.GetMethods (BindingFlags.Static | 
         BindingFlags.NonPublic | BindingFlags.Public);
        Console.WriteLine ("// Static Methods");
        PrintMembers (mi);

        // Constructors.
        ConstructorInfo [] ci = t.GetConstructors (BindingFlags.Instance | 
         BindingFlags.NonPublic | BindingFlags.Public);
        Console.WriteLine ("// Constructors");
        PrintMembers (ci);

        // Instance fields.
        fi = t.GetFields (BindingFlags.Instance | BindingFlags.NonPublic | 
         BindingFlags.Public);
        Console.WriteLine ("// Instance Fields");
        PrintMembers (fi);

        // Instance properites.
        pi = t.GetProperties (BindingFlags.Instance | BindingFlags.NonPublic | 
         BindingFlags.Public);
        Console.WriteLine ("// Instance Properties");
        PrintMembers (pi);

        // Instance events.
        ei = t.GetEvents (BindingFlags.Instance | BindingFlags.NonPublic | 
         BindingFlags.Public);
        Console.WriteLine ("// Instance Events");
        PrintMembers (ei);

        // Instance methods.
        mi = t.GetMethods (BindingFlags.Instance | BindingFlags.NonPublic   
         | BindingFlags.Public);
        Console.WriteLine ("// Instance Methods");
        PrintMembers (mi);

        Console.WriteLine ("\r\nPress ENTER to exit.");
        Console.Read();
    }

    public static void PrintMembers (MemberInfo [] ms) {
        foreach (MemberInfo m in ms) {
            Console.WriteLine ("{0}{1}", "     ", m);
        }
        Console.WriteLine();
    }
}

请参见

参考

查看类型信息

BindingFlags

Assembly.GetType

Assembly.GetTypes

Type.GetType

Type.GetMembers

Type.GetFields

Module.GetType

Module.GetTypes

MemberInfo

ConstructorInfo

MethodInfo

FieldInfo

EventInfo

ParameterInfo

其他资源

反射类型和泛型类型