型情報の表示

System.Type クラスは、リフレクションの中枢です。リフレクションが Type の作成を要求すると、共通言語ランタイムは、読み込まれた型に対して Type オブジェクトを作成します。Typeオブジェクトのメソッド、フィールド、プロパティ、および入れ子クラスを使用すると、その型についてのすべての情報を調べることができます。

まだ読み込まれていないアセンブリから Type オブジェクトを取得するには、Assembly.GetType または Assembly.GetTypes を使用し、必要な型の名前を 1 つ以上渡します。既に読み込まれているアプリケーションから Type オブジェクトを取得するには、Type.GetType を使用します。モジュールの Type オブジェクトを取得するには、Module.GetType および Module.GetTypes を使用します。

Noteメモ :

If you want to examine and manipulate ジェネリック型およびメソッドを調べて操作する場合は、「リフレクションとジェネリック型」および「方法 : リフレクションを使用してジェネリック型をチェックおよびインスタンス化する」を参照してください。

アセンブリの 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 を取得した後は、さまざまな方法で、この型のメンバに関する情報を探索できます。たとえば、現在の型の各メンバが記述されている MemberInfo オブジェクトの配列を取得するType.GetMembers メソッドを呼び出すと、その型のすべてのメンバについて調べることができます。

また、Type クラスのメソッドを使用し、名前を指定することで、1 つ以上のコンストラクタ、メソッド、イベント、フィールド、またはプロパティについての情報を取得できます。たとえば、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

MemberInfoMethodInfoFieldInfo、または 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;
    }
}

すべてのリフレクション *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

その他の技術情報

リフレクションとジェネリック型