您的位置:首页 > 其它

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

2014-04-09 09:24 609 查看
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();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐