一个简单的反射例子(通过类型或者类型参数获取类的名称)

msg 类的定义

using System; using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication2

{    public  class user<T> where T :class,new()  

   {

       public string getClassName()     

   {            return typeof(T).Name.ToString();

       }       

    }

}

user<T> 类的定义

using System;

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication2

{    public  class user<T> where T :class,new()

    {

       public string getClassName()      

       {            return typeof(T).Name.ToString();

       }       

    }

}

模拟场景类的定义  

using System;

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApplication2

{    

class Program   

  {      

   static void Main(string[] args)     

    {         

   // Console.WriteLine(  typeof(user).Name.ToString());  

           user<msg> um = new user<msg>();     

          Console.WriteLine("msg.fullName="+typeof(msg).FullName);     

         Console.WriteLine("um.GetType="+ um.GetType().ToString());   

         Console.WriteLine("um<T> T.ClassName="+um.getClassName());      

         Console.ReadKey();

        }

    }

}

posted on 2014-04-09 09:24  听哥哥的话  阅读(209)  评论(0编辑  收藏  举报

导航