Can I get the name of a type at runtime?

Can I get the name of a type at runtime?

Yes, use the GetType method of the object class (which all types inherit from). For example:

    using System;

    class CTest
    {
        class CApp
        {
            public static void Main()
            {
                long i = 10;
                CTest ctest = new CTest();

                DisplayTypeInfo( ctest );
                DisplayTypeInfo( i );
            }
       
            static void DisplayTypeInfo( object obj )
            {
                Console.WriteLine( “Type name = {0}, full type name = {1}”, obj.GetType(), obj.GetType().FullName );
            }
        }
    }produces the following output:

    Type name = CTest, full type name = CTest
    Type name = Int64, full type name = System.Int64

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.