다음을 통해 공유


형식 정보 보기

System.Type 클래스는 리플렉션의 중심적인 요소입니다. 공용 언어 런타임에서는 리플렉션에서 요청할 때 로드된 형식의 Type을 만듭니다. Type 개체의 메서드, 필드, 속성 및 중첩 클래스를 사용하여 해당 형식에 대한 모든 정보를 검색할 수 있습니다.

로드되지 않은 어셈블리에서 Type 개체를 가져오려면 Assembly.GetType 또는 Assembly.GetTypes를 사용합니다. 이때 원하는 형식의 이름을 전달합니다. 이미 로드된 어셈블리에서 Type 개체를 가져오려면 Type.GetType을 사용합니다. 모듈 Type 개체를 가져오려면 Module.GetTypeModule.GetTypes를 사용합니다.

참고참고

제네릭 형식과 메서드를 검사하고 조작하려면 리플렉션 및 제네릭 형식방법: 리플렉션을 사용하여 제네릭 형식 검사 및 인스턴스화에서 제공하는 추가 정보를 참조하십시오.

다음 예제에서는 Assembly 개체와 어셈블리의 모듈을 가져오는 데 필요한 구문을 보여 줍니다.

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

다음 예제에서는 로드된 어셈블리에서 Type 개체를 가져오는 방법을 보여 줍니다.

' Loads an assembly using its file name.
Dim a As Assembly = Assembly.LoadFrom("MyExe.exe")
' Gets the type names from the assembly.
Dim types2() As Type = a.GetTypes()
For Each t As Type 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);
}
// Loads an assembly using its file name.
Assembly^ a = Assembly::LoadFrom("MyExe.exe");
// Gets the type names from the assembly.
array<Type^>^ types2 = a->GetTypes();
for each (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()
    {
        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();
    }
}
// This program lists all the public constructors
// of the System.String class.
using namespace System;
using namespace System::Reflection;

class ListMembers
{
public:
    static void Main()
    {
        Type^ t = System::String::typeid;
        Console::WriteLine ("Listing all the public constructors of the {0} type", t);
        // Constructors.
        array<ConstructorInfo^>^ ci = t->GetConstructors(BindingFlags::Public | BindingFlags::Instance);
        Console::WriteLine ("//Constructors");
        PrintMembers(ci);
    }

    static void PrintMembers(array<MemberInfo^>^ ms)
    {
        for each (MemberInfo^ m in ms)
        {
            Console::WriteLine ("{0}{1}", "     ", m);
        }
        Console::WriteLine();
    }
};

int main()
{
    ListMembers::Main();
}

MemberInfo, MethodInfo, FieldInfo 및 PropertyInfo

Obtain information about the type's methods, properties, events, and fields using MemberInfo, MethodInfo, FieldInfo 또는 PropertyInfo 개체를 사용하여 형식의 메서드, 속성, 이벤트 및 필드에 대한 정보를 가져올 수 있습니다.

다음 예제에서는 MemberInfo를 사용하여 System.IO.File 클래스에 있는 멤버 수를 나열하고 System.Type.IsPublic 속성을 사용하여 클래스의 표시 여부를 확인합니다.

Imports System
Imports System.IO
Imports System.Reflection

Class Mymemberinfo
    Public Shared Sub Main()
        Console.WriteLine ("\nReflection.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("\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)
        End If
    End Sub
End Class
using System;
using System.IO;
using System.Reflection;

class Mymemberinfo
{
    public static void Main()
    {
        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);
        }
    }
}
using namespace System;
using namespace System::IO;
using namespace System::Reflection;

public ref class Mymemberinfo
{
public:
    static void Main()
    {
        Console::WriteLine ("\nReflection.MemberInfo");
        // Gets the Type and MemberInfo.
        Type^ MyType = Type::GetType("System.IO.File");
        array<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);
        }
    }
};

int main()
{
    Mymemberinfo::Main();
}

다음 예제에서는 지정된 멤버의 형식을 조사합니다. 또한 MemberInfo 클래스의 멤버에 대해 리플렉션을 수행하고 해당 멤버의 형식을 나열합니다.

' This code displays information about the GetValue method of FieldInfo.
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;
    }
}
// This code displays information about the GetValue method of FieldInfo.
using namespace System;
using namespace System::Reflection;

public ref 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;
    }
};

int main()
{
    MyMethodInfo::Main();
}

다음 예제에서는 모든 리플렉션 *Info 클래스를 BindingFlags와 함께 사용하여 지정된 클래스의 모든 멤버(생성자, 필드, 속성, 이벤트 및 메서드)를 나열하고 멤버를 정적 및 인스턴스 범주로 분류합니다.

' This program lists all the members of the
' System.IO.BufferedStream class.
Imports System
Imports System.IO
Imports System.Reflection

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()
    {
        // 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();
    }
}
// This program lists all the members of the
// System.IO.BufferedStream class.
using namespace System;
using namespace System::IO;
using namespace System::Reflection;

public ref class ListMembers
{
public:
    static void Main()
    {
        // Specifies the class.
        Type^ t = System::IO::BufferedStream::typeid;
        Console::WriteLine("Listing all the members (public and non public) of the {0} type", t);

        // Lists static fields first.
        array<FieldInfo^>^ fi = t->GetFields(BindingFlags::Static |
            BindingFlags::NonPublic | BindingFlags::Public);
        Console::WriteLine("// Static Fields");
        PrintMembers(fi);

        // Static properties.
        array<PropertyInfo^>^ pi = t->GetProperties(BindingFlags::Static |
            BindingFlags::NonPublic | BindingFlags::Public);
        Console::WriteLine("// Static Properties");
        PrintMembers(pi);

        // Static events.
        array<EventInfo^>^ ei = t->GetEvents(BindingFlags::Static |
            BindingFlags::NonPublic | BindingFlags::Public);
        Console::WriteLine("// Static Events");
        PrintMembers(ei);

        // Static methods.
        array<MethodInfo^>^ mi = t->GetMethods (BindingFlags::Static |
            BindingFlags::NonPublic | BindingFlags::Public);
        Console::WriteLine("// Static Methods");
        PrintMembers(mi);

        // Constructors.
        array<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();
    }

    static void PrintMembers(array<MemberInfo^>^ ms)
    {
        for each (MemberInfo^ m in ms)
        {
            Console::WriteLine ("{0}{1}", "     ", m);
        }
        Console::WriteLine();
    }
};

int main()
{
    ListMembers::Main();
}

참고 항목

참조

BindingFlags

Assembly.GetType

Assembly.GetTypes

Type.GetType

Type.GetMembers

Type.GetFields

Module.GetType

Module.GetTypes

MemberInfo

ConstructorInfo

MethodInfo

FieldInfo

EventInfo

ParameterInfo

개념

리플렉션 및 제네릭 형식