| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Viewing Type Information |
03/30/2017 |
.net-framework |
|
article |
|
|
7e7303a9-4064-4738-b4e7-b75974ed70d2 |
13 |
rpetrusha |
ronpet |
wpickett |
Viewing Type Information
The xref:System.Type?displayProperty=nameWithType class is central to reflection. The common language runtime creates the Type for a loaded type when reflection requests it. You can use a Type object's methods, fields, properties, and nested classes to find out everything about that type.
Use xref:System.Reflection.Assembly.GetType%2A?displayProperty=nameWithType or xref:System.Reflection.Assembly.GetTypes%2A?displayProperty=nameWithType to obtain Type objects from assemblies that have not been loaded, passing in the name of the type or types you want. Use xref:System.Type.GetType%2A?displayProperty=nameWithType to get the Type objects from an assembly that is already loaded. Use xref:System.Reflection.Module.GetType%2A?displayProperty=nameWithType and xref:System.Reflection.Module.GetTypes%2A?displayProperty=nameWithType to obtain module Type objects.
[!NOTE] If you want to examine and manipulate generic types and methods, please see the additional information provided in Reflection and Generic Types and How to: Examine and Instantiate Generic Types with Reflection.
The following example shows the syntax necessary to get the xref:System.Reflection.Assembly object and module for an assembly.
[!code-cppConceptual.Types.ViewInfo#6] [!code-csharpConceptual.Types.ViewInfo#6] [!code-vbConceptual.Types.ViewInfo#6]
The following example demonstrates getting Type objects from a loaded assembly.
[!code-cppConceptual.Types.ViewInfo#7] [!code-csharpConceptual.Types.ViewInfo#7] [!code-vbConceptual.Types.ViewInfo#7]
Once you obtain a Type, there are many ways you can discover information about the members of that type. For example, you can find out about all the type's members by calling the xref:System.Type.GetMembers%2A?displayProperty=nameWithType method, which obtains an array of xref:System.Reflection.MemberInfo objects describing each of the members of the current type.
You can also use methods on the Type class to retrieve information about one or more constructors, methods, events, fields, or properties that you specify by name. For example, xref:System.Type.GetConstructor%2A?displayProperty=nameWithType encapsulates a specific constructor of the current class.
If you have a Type, you can use the xref:System.Type.Module%2A?displayProperty=nameWithType property to obtain an object that encapsulates the module containing that type. Use the xref:System.Reflection.Module.Assembly%2A?displayProperty=nameWithType property to locate an object that encapsulates the assembly containing the module. You can obtain the assembly that encapsulates the type directly by using the xref:System.Type.Assembly%2A?displayProperty=nameWithType property.
System.Type and ConstructorInfo
The following example shows how to list the constructors for a class, in this case, the xref:System.String class.
[!code-cppConceptual.Types.ViewInfo#1] [!code-csharpConceptual.Types.ViewInfo#1] [!code-vbConceptual.Types.ViewInfo#1]
MemberInfo, MethodInfo, FieldInfo, and PropertyInfo
Obtain information about the type's methods, properties, events, and fields using xref:System.Reflection.MemberInfo, xref:System.Reflection.MethodInfo, xref:System.Reflection.FieldInfo, or xref:System.Reflection.PropertyInfo objects.
The following example uses MemberInfo to list the number of members in the System.IO.File class and uses the xref:System.Type.IsPublic%2A property to determine the visibility of the class.
[!code-cppConceptual.Types.ViewInfo#2] [!code-csharpConceptual.Types.ViewInfo#2] [!code-vbConceptual.Types.ViewInfo#2]
The following example investigates the type of the specified member. It performs reflection on a member of the MemberInfo class, and lists its type.
[!code-cppConceptual.Types.ViewInfo#3] [!code-csharpConceptual.Types.ViewInfo#3] [!code-vbConceptual.Types.ViewInfo#3]
The following example uses all the Reflection *Info classes along with xref:System.Reflection.BindingFlags to list all the members (constructors, fields, properties, events, and methods) of the specified class, dividing the members into static and instance categories.
[!code-cppConceptual.Types.ViewInfo#4] [!code-csharpConceptual.Types.ViewInfo#4] [!code-vbConceptual.Types.ViewInfo#4]
See Also
xref:System.Reflection.BindingFlags
xref:System.Reflection.Assembly.GetType%2A?displayProperty=nameWithType
xref:System.Reflection.Assembly.GetTypes%2A?displayProperty=nameWithType
xref:System.Type.GetType%2A?displayProperty=nameWithType
xref:System.Type.GetMembers%2A?displayProperty=nameWithType
xref:System.Type.GetFields%2A?displayProperty=nameWithType
xref:System.Reflection.Module.GetType%2A?displayProperty=nameWithType
xref:System.Reflection.Module.GetTypes%2A?displayProperty=nameWithType
xref:System.Reflection.MemberInfo
xref:System.Reflection.ConstructorInfo
xref:System.Reflection.MethodInfo
xref:System.Reflection.FieldInfo
xref:System.Reflection.EventInfo
xref:System.Reflection.ParameterInfo
Reflection and Generic Types