Görünüm türü bilgileri

System.Type sınıfı, yansıma için merkezi bir sınıftır. Ortak dil çalışma zamanı, yansıma istediğinde yüklenen bir tür için öğesini oluşturur Type . Bir nesnenin yöntemlerini, alanlarını, özelliklerini ve iç içe yerleştirilmiş sınıflarını kullanarak Type bu türle ilgili her şeyi bulabilirsiniz.

yüklenmemiş derlemelerden istediğiniz türün veya türlerin adını geçirerek nesneleri almak Type için veya Assembly.GetTypes kullanınAssembly.GetType. Önceden yüklenmiş bir derlemeden nesneleri almak Type için kullanınType.GetType. Modül Type nesnelerini almak için ve Module.GetTypes kullanınModule.GetType.

Not

Genel türleri ve yöntemleri incelemek ve işlemek istiyorsanız, lütfen Düşünceler ion ve Genel Türler'de sağlanan ek bilgilere ve Nasıl yapılır: Düşünceler ion ile Genel Türleri İnceleme ve Örneği Oluşturma bölümüne bakın.

Aşağıdaki örnek, bir derlemenin nesnesini ve modülünü almak için gereken söz dizimini Assembly gösterir.

// Gets the mscorlib assembly in which the object is defined.
Assembly^ a = Object::typeid->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.
Dim a As Assembly = GetType(Object).Module.Assembly

Aşağıdaki örnekte, yüklenen bir derlemeden nesne alma Type gösterilmektedir.

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

bir Typeelde ettikten sonra, bu türün üyeleri hakkındaki bilgileri bulmanın birçok yolu vardır. Örneğin, geçerli türün üyelerinden her birini açıklayan bir nesne dizisi MemberInfo elde eden yöntemini çağırarak Type.GetMembers türün tüm üyeleri hakkında bilgi edinebilirsiniz.

Ayrıca, ada göre belirttiğiniz bir veya daha fazla oluşturucu, yöntem, olay, alan veya özellik hakkında bilgi almak için sınıfındaki Type yöntemleri de kullanabilirsiniz. Örneğin, Type.GetConstructor geçerli sınıfın belirli bir oluşturucuyu kapsüller.

bir varsa Type, bu türü içeren modülü kapsülleyen bir nesne elde etmek için özelliğini kullanabilirsiniz Type.Module . modülünü Module.Assembly içeren derlemeyi kapsülleyen bir nesneyi bulmak için özelliğini kullanın. özelliğini kullanarak türü kapsülleyen derlemeyi Type.Assembly doğrudan alabilirsiniz.

System.Type ve ConstructorInfo

Aşağıdaki örnekte, bir sınıfın oluşturucularının nasıl listelenerek sınıfın gösterildiği gösterilmiştir String .

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

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

MemberInfo, MethodInfo, FieldInfo ve PropertyInfo

, MethodInfoFieldInfo, veya PropertyInfo nesnelerini kullanarak MemberInfotürün yöntemleri, özellikleri, olayları ve alanları hakkında bilgi edinin.

Aşağıdaki örnek, sınıfındaki üye System.IO.File sayısını listelemek için kullanır MemberInfo ve sınıfın görünürlüğünü belirlemek için özelliğini kullanırIsPublic.

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();
}
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);
        }
    }
}
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

Aşağıdaki örnek, belirtilen üyenin türünü araştırır. Sınıfın MemberInfo bir üyesi üzerinde yansıma gerçekleştirir ve türünü listeler.

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

Aşağıdaki örnek, belirtilen sınıfın tüm üyelerini (oluşturucular, alanlar, özellikler, olaylar ve yöntemler) listelemek ve üyeleri statik ve örnek kategorilerine bölmek için tüm Düşünceler ion *Info sınıflarını kullanırBindingFlags.

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