[Previous] [Contents] [Next]

Retrieving the Type from a Name


Besides retrieving a variable's Type object, you can also create a Type object from a type name. In other words, you don't need to have an instance of the type. Here's an example of doing this for the System.Int32 type: -

using System;
using System.Reflection;

class TypeObjectFromNameApp
{
    public static void Main(string[] args)
    {
        Type t = Type.GetType("System.Int32");
        Console.WriteLine(t.Name);
    }
}

Note that you cannot use the C# aliases when calling the Type.GetType method, because this method is used by all languages. Therefore, you cannot use the C# specific int in place of System.Int32.

[Previous] [Contents] [Next]