Partager via


Viewing Type Information

Microsoft Silverlight will reach end of support after October 2021. Learn more.

The System.Type class is central to reflection. The common language runtime creates the Type object for a loaded type when reflection requests it. You can use a Type object's methods, fields, and properties to find out everything about that type, including its private members. Although you can discover all type information, Silverlight-based applications can invoke only members that are accessible, as explained in Dynamically Loading and Using Types.

Use the Type.GetType method to get Type objects from your application assembly and from the .NET Framework assemblies it was compiled with. Use the Assembly.GetType or Assembly.GetTypes method to obtain Type objects from a specific assembly or from an assembly that your application loaded at run time.

NoteNote:

If you want to examine and manipulate generic types and methods, see the additional information provided in Reflection and Generic Types and How to: Examine and Instantiate Generic Types with Reflection.

Once you obtain a Type object, there are many ways you can discover information about the members of that type. For example, you can list all the type's members, including nested types, by calling the Type.GetMembers method. This method obtains an array of MemberInfo objects that describe the members of the current type. MemberInfo is the base type for all the classes that describe types and members, so this array contains MethodInfo objects for the methods, Type objects for the nested types, and so on. Use the MemberInfo.MemberType property to tell what kind of member each element represents.

You can also use methods on the Type class to retrieve information about constructors, methods, events, fields, or properties that you specify by name. For example, the Type.GetConstructor method obtains a ConstructorInfo for a specific constructor of the current class.

Use the BindingFlags enumeration to specify whether the member or members you retrieve are public and/or nonpublic, and whether static and/or instance members are included.

If you have a Type, you can use the Type.Module property to obtain a Module object that represents the module that contains that type. Similarly, you can obtain the assembly that contains the type by using the Type.Assembly property.

The following example shows how to list the constructors for a class, in this case the String class.

' This code lists all the public constructors 
' of the System.String class.
Dim t As Type = GetType(String)
Dim sb As New StringBuilder()
Dim ctors() As ConstructorInfo = _
    t.GetConstructors(BindingFlags.Public Or BindingFlags.Instance)
For Each ctor As ConstructorInfo In ctors
    sb.AppendLine(ctor.ToString())
Next ctor
myTextBox.Text = sb.ToString()
// This code lists all the public constructors 
// of the System.String class.
Type t = typeof(String);
StringBuilder sb = new StringBuilder();
ConstructorInfo[] ci = 
    t.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
foreach (ConstructorInfo ctor in ctors) 
{
    sb.AppendLine(ctor.ToString());
}
myTextBox.Text = sb.ToString();