在反射和泛型中经常会使用到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的更多相关文章

  1. c# 之 System.Type.GetType()与Object.GetType()与typeof比较

    Object.GetType()与typeof的区别 //运算符,获得某一类型的 System.Type 对象. Type t = typeof(int); //方法,获取当前实例的类型. ; Con ...

  2. c# GetType()和typeof()的区别

    c#   GetType()和typeof()的区别 C#中任何对象都具有GetType()方法,返回Type类型的当前对象的类型. GetType()是基类System.Object的方法,因此只有 ...

  3. C# GetType和typeof的区别

    typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...

  4. c#种GetType()和TypeOf()的区别

    C#中任何对象都具有GetType()方法,它的作用和typeof()相同,返回Type类型的当前对象的类型. typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称:GetType ...

  5. C#基础之GetType 与 typeof的区别

      C#中GetType 与 typeof的区别 在实际开发中经常需要了解具体对象的类型,所以经常会使用GetType()和typeof().尽管可以得到相应的类型.但两者之间也存在一些差别,接下来我 ...

  6. GetType() 和typeof() 的区别

    GetType() 非强类型,支持跨程序集发射,用来支持动态引用, A obja=new A(); Type t=obja.GetType() typeof() 强类型,静态的 Type t=type ...

  7. typeof,GetType

    typeof: 是运算符,获得某一类型的 System.Type 对象. Int32 t = new Int32(); Type t = typeof(int); GetType: 是方法,获取当前实 ...

  8. typeof与GetType

    typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...

  9. typeof与GetType区别及反射的见解

    http://www.cnblogs.com/knowledgesea/archive/2013/03/02/2935920.html http://www.cnblogs.com/Jax/archi ...

随机推荐

  1. IOS开发基础知识--碎片50

      1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...

  2. 【代码笔记】iOS-用户发布后能保存崩溃

    一,工程图. 二,代码. AppDelegate.m #import "AppDelegate.h" #import "RootViewController.h" ...

  3. Java 消息摘要 散列 MD5 SHA

    package xxx.common.util; import java.math.BigInteger; import java.security.MessageDigest; import jav ...

  4. sys.dm_os_waiting_tasks 引发的疑问(中)

    上一篇我们说了一下sys.dm_exec_requests 和 sys.dm_os_waiting_tasks 在获取并行等待的时候得不同结果,这一篇我们谈论下我的第二个疑问:为什么一个并行计划(4线 ...

  5. 安装Ubuntu的那些事儿

    这是博主第一次写博客,本人虽然目前就读的专业属计算机,但目前也是属于新手上路的那一类人.正好不久前解决了一个困扰了我很久的问题 ,现在拿出来给大家分享一下. 上个学期学校的工作室给大家集中普及linu ...

  6. 安装TFS(2015)工作组模式代理服务器(Agent)

    TFS的代理服务器(agent)用于持续集成编译和发布,为开发.测试团队和运维团队带来的非常便捷高效的发布和测试速度,许多企业和研发团队都在自己的研发测试平台中广泛使用这一技术. 在部署TFS代理服务 ...

  7. bug描述技巧

    进入测试行业已经两年了,我从未认真的考虑过提交一个bug需要注意哪些问题,只是主观的认为我只需要描述清楚就OK了,但是我在工作中发现有个别的开发经常跑来告诉我"这个bug你是不是描述错了&q ...

  8. 【原】pageResponse - 让H5适配移动设备全家(移动端适配)

    上一篇文章<为什么选择iPhone5的分辨率作为H5视觉稿尺寸>最后留下了问题:是否需要视觉设计师设计多套的视觉稿供给前端工程师做页面适配呢?按照自己以前的方法,通常会要求设计师设计2套的 ...

  9. 【repost】JavaScript 事件模型 事件处理机制

    什么是事件? 事件(Event)是JavaScript应用跳动的心脏 ,也是把所有东西粘在一起的胶水.当我们与浏览器中 Web 页面进行某些类型的交互时,事件就发生了.事件可能是用户在某些内容上的点击 ...

  10. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...