Viewing Type Information

The System::Type 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 Assembly::GetType or Assembly::GetTypes to obtain Type objects from assemblies that have not been loaded, passing in the name of the type or types you want. Use Type::GetType to get the Type objects from an assembly that is already loaded. Use Module::GetType and Module::GetTypes to obtain module Type objects.

Note 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 Assembly object and module for an assembly.

// Gets the mscorlib assembly in which the object is defined.
Assembly^ a = Object::typeid->Module->Assembly;

The following example demonstrates getting Type objects from a loaded assembly.

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

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 Type::GetMembers method, which obtains an array of 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, Type::GetConstructor encapsulates a specific constructor of the current class.

If you have a Type, you can use the Type::Module property to obtain an object that encapsulates the module containing that type. Use the Module::Assembly 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 Type::Assembly property.

The following example shows how to list the constructors for a class, in this case, the String class.

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

Obtain information about the type's methods, properties, events, and fields using MemberInfo, MethodInfo, FieldInfo, or PropertyInfo objects.

The following example uses MemberInfo to list the number of members in the System.IO.File class and uses the System.Type.IsPublic property to determine the visibility of the class.

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

The following example investigates the type of the specified member. It performs reflection on a member of the MemberInfo class, and lists its type.

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

The following example uses all the Reflection *Info classes along with BindingFlags to list all the members (constructors, fields, properties, events, and methods) of the specified class, dividing the members into static and instance categories.

// 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();
}
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft