C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
Today
    Last 7 Days more...






    Resources » Articles » .NET Framework »

    Getting Type Information Using Reflection in .NET


    Posted Date: 17 May 2006    Resource Type: Articles    Category: .NET Framework
    Author: phanindra yerraMember Level: Gold    
    Rating: 1 out of 5Points: 14



    Introduction



    In .NET, reflection is the means by which you can discover information about
    an object at runtime. You might have used the Intermediate Language Disassembler, IlDasm.exe, to look at the information in an assembly. By using reflection, you can obtain the same information from a program.

    Type Class



    The key to discovering type information, and to begin exploring reflection, is the Type class, which is a member of the System.Reflection namespace.
    The Type class contains methods for extracting information about a given object, including the methods, properties, and fields, along with the interfaces an object supports. The Type class is abstract, so you cannot create an instance of it directly.

    In C#, every class or structure that you define ultimately derives from the Object class, and thus inherits a GetType() method. When you call GetType(), the method returns a reference to a Type object.From this object, you can call a rich set of methods to get information about a class or structure.

    Assuming you have a class name clsClass, the following snippet shows how to get a Type object:


    clsClass cls = new clsClass();

    Type t = cls.GetType();


    The Type class also has a static method, GetType(), that allows you to get a Type object without having to create an instance of a class or structure. You need only pass the method the quoted name of the class:


    Type t = Type.GetType ("clsClass");


    Yet another method of obtaining a Type object is to use the typeof operator, passing the name of the class as a parameter:


    Type t = typeof (clsClass);


    Once you have the Type object, you can use member methods to extract information
    about the class. The following program, GetType.cs, uses a Type object to list the methods,properties, and fields in a class named clsEmployee:

    GetType.cs -- Demonstrates using Type class to discover information about a class



    using System;
    using System.Reflection;
    namespace nsReflection
    {
    public class clsEmployee
    {


    public string FirstName;
    public string LastName;
    public string ZipCode;
    public int EmployeeID;


    public clsEmployee (string First, string Last, string Zip, int ID)
    {
    FirstName = First;
    LastName = Last;
    EmployeeID = ID;
    ZipCode = Zip;
    }
    public string Name
    {
    get {return (FirstName + " " + LastName);}
    }
    public string Zip
    {
    get {return (ZipCode);}
    }
    public int ID
    {
    get {return (EmployeeID);}
    }
    static public int CompareByName (object o1, object o2)
    {
    clsEmployee emp1 = (clsEmployee) o1;
    clsEmployee emp2 = (clsEmployee) o2;
    return (String.Compare (emp1.LastName, emp2.LastName));
    }
    static public int CompareByZip (object o1, object o2)
    {
    clsEmployee emp1 = (clsEmployee) o1;
    clsEmployee emp2 = (clsEmployee) o2;
    return (String.Compare (emp1.ZipCode, emp2.ZipCode));
    }
    static public int CompareByID (object o1, object o2)
    {
    clsEmployee emp1 = (clsEmployee) o1;
    clsEmployee emp2 = (clsEmployee) o2;
    return (emp1.EmployeeID - emp2.EmployeeID);
    }
    }
    class clsMain
    {
    static public void Main ()
    {
    Type t = typeof(clsEmployee);
    if (t == null)
    {
    Console.WriteLine ("t is null");
    return;
    }
    // Show what namespace the class is in

    Console.WriteLine ("Class clsEmployee is a member of " +
    "the {0} namespace", t.Namespace);

    Console.WriteLine ("\r\nMethods in clsEmployee:");

    MethodInfo [] methods = t.GetMethods ();

    // Show the methods in the class

    foreach (MethodInfo m in methods)
    Console.WriteLine ("\t" + m.Name);

    // Show the properties in the class


    Console.WriteLine ("\r\nProperties in clsEmployee:");
    PropertyInfo [] props = t.GetProperties ();
    foreach (PropertyInfo p in props)
    Console.WriteLine ("\t" + p.Name);

    // Show the fields in the class

    Console.WriteLine ("\r\nFields in clsEmployee:");
    FieldInfo [] fields = t.GetFields ();

    foreach (FieldInfo f in fields)
    Console.WriteLine ("\t" + f.Name);
    }
    }
    }


    Running GetType.cs will list the namespace for the clsEmployee class along with the member methods, properties, and fields. Notice that the methods return only information about members to which you have access. To prove this, in the preceding code, try changing the member fields for the employee’s name to protected or private.

    You also can use reflection to obtain information about an entire assembly.
    The following program, Asy.cs, will load the assembly from the global assembly cache and list information about the assembly, plus information about the structure and class that it contains. To get the assembly information, you use the Assembly class, which is also a member of the System.Reflection namespace. Calling the static member Load will return a reference to the assembly information.

    Asy.cs -- Demonstrates using reflection to get information about an assembly.



    using System;
    using System.Reflection;
    class clsMain
    {
    static public void Main ()
    {
    Assembly asy = null;
    try
    {
    asy = Assembly.Load ("ASMNAME");
    }
    catch (Exception e)
    {
    Console.WriteLine (e.Message);
    return;
    }
    Type [] types = asy.GetTypes();
    foreach (Type t in types)
    ShowTypeInfo (t);
    }
    static void ShowTypeInfo (Type t)
    {
    Console.WriteLine ("{0} is a member of the {1} namespace",
    t.Name, t.Namespace);
    Console.WriteLine ("\r\nMethods in {0}:", t.Name);

    MethodInfo [] methods = t.GetMethods ();
    foreach (MethodInfo m in methods)

    Console.WriteLine ("\t" + m.Name);
    Console.WriteLine ("\r\nProperties in {0}:", t.Name);

    PropertyInfo [] props = t.GetProperties ();
    foreach (PropertyInfo p in props)
    Console.WriteLine ("\t" + p.Name);

    Console.WriteLine ("\r\nFields in {0}:", t.Name);
    FieldInfo [] fields = t.GetFields ();
    foreach (FieldInfo f in fields)
    Console.WriteLine ("\t" + f.Name);
    }
    }


    When you run Asy.cs, you should see the classes ,structures
    member properties, methods, and fields presented in the assembly ASMNAME.


    Summary



    This article explained how to get object information at runtime using System.Reflections




    Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Reflection using c#  .  Reflection in .net  .  Getting type information using reflection in .net  .  Getting type information using reflection  .  Getting type information in .net  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Displaying data in datagrid as you need
    Previous Resource: Password Hashing in c#
    Return to Discussion Resource Index
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    Related Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use