C# GetType与typeof
在反射和泛型中经常会使用到Type类,获取Type的最常用的方法是 obj.GetType(),和typeof(T)。在获取泛型的type时有些小坑。
public static void Main(string[] args)
{
A a = new B
{
a = "a",
b = "b",
c = "c",
};
B c = new B
{
a = "a",
b = "b",
c = "c",
};
put(a);
put<A>(c);
put<B>(c);
put<IC>(c);
Console.ReadLine();
}
public static void put<T>(T t)
{ Type type1 = typeof(T);
Console.WriteLine();
Console.WriteLine("****************typeof*******************************");
foreach (var item in type1.GetProperties())
{
string name = item.Name;
string value = item.GetValue(t).ToString();
Console.WriteLine("name=" + name + ",value=" + value);
}
Console.WriteLine("****************GetType*******************************");
Type type2 = t.GetType(); foreach (var item in type2.GetProperties())
{
string name = item.Name;
string value = item.GetValue(t).ToString();
Console.WriteLine("name=" + name + ",value=" + value);
} } public class A
{
public string a { get; set; }
}
public interface IC
{
string c { get; set; }
}
public class B : A,IC
{
public string c { get; set; }
public string b { get; set; }
}
在看看代码的执行结果:

发现一个问题 GetType 和typeof的结果不一样。put<T>(T t) 显而易见,在传入相同的对象不同泛型 t.GetType()的返回值是确定的,而typeof(T)是可以变化的。obj.GetType()和定义obj的类型没有直接的关系,它的返回值是 YYYY obj = new XXXX() ; XXXX的类型,不一定是YYYY的类型。typeof就不用多说了
所以在此处代码应该写typeof(T),而不是t.GetType(),不然就失去泛型的意思。
GetType()有什么妙用的,我们来看下一段代码:
public static void Main(string[] args)
{
D d = new D
{
a = "a",
b = ,
d1 = new D1 { d1 = },
time = DateTime.Now,
};
put2(d);
Console.ReadLine();
}
public static void put2<T>(T t)
{
Type type1 = typeof(T);
Console.WriteLine();
PropertyInfo[] Properties = type1.GetProperties(); foreach (PropertyInfo item in Properties)
{
Console.WriteLine(item.GetType().FullName);
string name = item.Name;
object value = item.GetValue(t); Console.WriteLine("参数的命名空间为:" +value.GetType().FullName);
Console.WriteLine("name=" + name + ",value=" + value.ToString());
}
}
public class D
{
public string a { get; set; }
public int b { get; set; }
public DateTime time { get; set; }
private string c { get; set; }
public D1 d1 { get; set; } }
public class D1
{
public int d1 { get; set; }
public override string ToString()
{
return d1.ToString();
}
}
这段代码输出为:

这段代码的21行是输出item的命名空间,结果却是RuntimePropertyInfio不是定义的PropertyInfio。并且RuntimePropertyInfio这个类是不可以访问的。简单的推测出RuntimePropertyInfio 类的修饰词可能是private或者是internal,而且这个类是继承了PropertyInfio,同时也能推测出继承PropertyInfio的类绝对不是这一种。这个是c#源码中常用的一些手段。
再来看item.getValue(t)中 在源码中的返回值是object,

而我们却而已通过GetType() 获得类具体的命名空间,通过这些方法就可以处理不用的参数。
C# GetType与typeof的更多相关文章
- c# 之 System.Type.GetType()与Object.GetType()与typeof比较
Object.GetType()与typeof的区别 //运算符,获得某一类型的 System.Type 对象. Type t = typeof(int); //方法,获取当前实例的类型. ; Con ...
- c# GetType()和typeof()的区别
c# GetType()和typeof()的区别 C#中任何对象都具有GetType()方法,返回Type类型的当前对象的类型. GetType()是基类System.Object的方法,因此只有 ...
- C# GetType和typeof的区别
typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...
- c#种GetType()和TypeOf()的区别
C#中任何对象都具有GetType()方法,它的作用和typeof()相同,返回Type类型的当前对象的类型. typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称:GetType ...
- C#基础之GetType 与 typeof的区别
C#中GetType 与 typeof的区别 在实际开发中经常需要了解具体对象的类型,所以经常会使用GetType()和typeof().尽管可以得到相应的类型.但两者之间也存在一些差别,接下来我 ...
- GetType() 和typeof() 的区别
GetType() 非强类型,支持跨程序集发射,用来支持动态引用, A obja=new A(); Type t=obja.GetType() typeof() 强类型,静态的 Type t=type ...
- typeof,GetType
typeof: 是运算符,获得某一类型的 System.Type 对象. Int32 t = new Int32(); Type t = typeof(int); GetType: 是方法,获取当前实 ...
- typeof与GetType
typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...
- typeof与GetType区别及反射的见解
http://www.cnblogs.com/knowledgesea/archive/2013/03/02/2935920.html http://www.cnblogs.com/Jax/archi ...
随机推荐
- [笔记]linux磁盘管理
sudo mount -r /dev/sda3 /mnt/vista 只读挂载 sudo umount sudo umount -r 无法卸载时只读重新挂载 mount -t(指明设备类型) 可用参数 ...
- sublime text 3 快捷键大全以及配置编译环境
Sublime text 3是码农最喜欢的代码编辑器,每天和代码打交道,必先利其器,掌握基本的代码编辑器的快捷键,能让你打码更有效率.刚开始可能有些生疏,只要花一两个星期坚持使用并熟悉这些常用的快捷键 ...
- Ioc和Ao使用扩展
一.Bean作用域 spring容器创建的时候,会将所有配置的bean对象创建出来,默认bean都是单例的.代码通过getBean()方法从容器获取指定的bean实例,容器首先会调用Bean类的无参构 ...
- POJ2104 K-th Number [分块做法]
传送:主席树做法http://www.cnblogs.com/candy99/p/6160704.html 做那倒带修改的主席树时就发现分块可以做,然后就试了试 思想和教主的魔法差不多,只不过那个是求 ...
- Spring之BeanFactory及Bean生命周期
1.spring通过BeanFactory灵活配置.管理bean,Spring对管理的bean没有任何特别的要求,完全支持对POJO的管理: 2.BeanFactory有个ApplicationCon ...
- js form 表达关于onpress 的一个问题
<form id="search-form" method="get" action="/search"> <fields ...
- [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- IntelliJ idea创建Spring MVC的Maven项目
参考:http://my.oschina.net/gaussik/blog/385697?fromerr=Pie9IlFV 创建Maven Web项目 菜单File->New Project可进 ...
- 51Nod 1278 相离的圆
51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...
- Goodbye 2016 总结与展望
今天居然是2016年的最后一天了,写点什么回忆吧. 2016开始的时候我刚拿到普及组一等奖,还只是压线,水平很差.学校并不知道这有多差,于是狠狠宣传这所谓的"光荣事迹".那段时间我 ...