Sponsored Ad

Thursday, October 22, 2009

C# GetType

You can get an instance of the kind of guy in a variety of ways. However, the only thing we can do is create a text object directly with the keyword new, as is an abstract class type. Object.GetType method, the typeof operator, and several methods of the Assembly to return an object type. GetType is a member of the class of object, which is the base class everywhere. Therefore, it is inherited by GetType. Types of networks and available for all cases handled. Each instance can call GetType to return the object type related. Typeof operator extracts an object type directly from a reference or value type. Works of several Assembly members have to return objects from one or more Type. For example, the method and returns Assembly.GetTypes lists the types of destination Assembly.

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

Example:

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

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers