Shiman’s Weblog

Collecting stones from the sea-shore.

C# .NET Reflection : Obtaining a Type Object with Object.GetType()

We can obtain an instance of the Type class in a variety of ways. However, the one thing we cannot do is directly create a Type object using the new keyword, as Type is an abstract class. The Object.GetType method, the typeof operator, and various methods of the Assembly object return a Type object. GetType is a member of the Object class, which is the ubiquitous base class. Therefore, GetType is inherited by .NET types and available to all managed instances. Each instance can call GetType to return the related Type object. The typeof operator extracts a Type object directly from a reference or value type. Assembly objects have several members that return one or more Type objects. For example, the Assembly.GetTypes method enumerates and returns the Types of the target assembly.

As a member base class object, GetType is accessible to all instances of reference and values types. GetType returns the Type object of the instance. This is the syntax of the GetType method:

Type GetType()

The following code creates instances of a value and a reference type, which are passed individually as object parameters to successive calls to the DisplayType method, which homogenizes the objects, where each object loses its distinction. The function extracts the type of the instance and displays the Type name. Finally, if the Type represents a MyClass, the MyClass.Display method is called:

using System;

namespace Examples.Reflection
{
    class Program
    {
        static void Main()
        {
            int value = 5;
            MyClass obj = new MyClass();
            DisplayType(value);
            DisplayType(obj);

            Console.ReadKey();
        }

        static void DisplayType(object parameterObject)
        {
            Type parameterType = parameterObject.GetType();
            string name = parameterType.Name;
            Console.WriteLine("Type is " + name);

            if (name == "MyClass")
            {
                ((MyClass)parameterObject).Display();
            }
        }
    }

    class MyClass
    {
        public void Display()
        {
            Console.WriteLine("MyClass > Display");
        }
    }
}

February 12, 2009 - Posted by shiman | .NET, C#.Net, Computer Science, OOP, Programming | , , , , , , , , , , | 4 Comments

4 Comments »

  1. Just passing by.Btw, you website have great content!

    _________________________________
    Making Money $150 An Hour

    Comment by Mike | March 1, 2009 | Reply

  2. Hello,

    Instead of
    throw new Exception(“abc”);
    throw new UnauthorizedAccessException(“abc”);

    I would like to create a type dynamically,
    have something like type creator

    throw new CreateType(“System.Exception”)(“abc”);
    throw new CreateType(“System.UnauthorizedAccessException”)(“abc”);

    Is there a way to do this ?

    Comment by Charles Rex | April 3, 2009 | Reply

  3. Hello,

    Meanwhile I’ve found how to create a type dynamically

    Examples:

    throw (Exception)Activator.CreateInstance(Type.GetType(“System.Exception”), “abc”);

    throw (Exception)Activator.CreateInstance(Type.GetType(“System.UnauthorizedAccessException”), “abc”);

    Great solution presented here:

    http://stackoverflow.com/questions/648160/how-do-i-create-an-instance-from-a-string-in-c

    Comment by Charles Rex | April 3, 2009 | Reply

  4. Clearly, the economic effect is the same. ,

    Comment by Crazy72 | October 10, 2009 | Reply


Leave a comment