檢視型別資訊
更新:2007 年 11 月
System.Type 類別為反映的核心。當反映要求時,Common Language Runtime 會為載入的型別建立 Type。您可以使用 Type 物件的方法、欄位、屬性和巢狀類別,找出該型別的一切事項。
使用 Assembly.GetType 或 Assembly.GetTypes,可以從尚未載入的組件取得 Type 物件,傳入型別名稱或您所需的型別。使用 Type.GetType 可以從已載入的組件取得 Type 物件。使用 Module.GetType 和 Module.GetTypes 可取得模組 Type 物件。
注意事項: |
---|
如果您想要檢視和使用泛型型別和方法,請參閱反映和泛型型別和 HOW TO:使用反映檢視和執行個體化泛型型別中提供的其他資訊。 |
下列範例說明取得 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
使用 MemberInfo、MethodInfo、FieldInfo 或 PropertyInfo 物件,取得型別的方法、屬性、事件和欄位的資訊。
下列範例使用 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;
}
}
下列程式碼範例使用所有的 Reflection *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();
}
}
請參閱
參考
檢視型別資訊