在反射和泛型中经常会使用到Type类,获取Type的最常用的方法是 obj.GetType(),和typeof(T)。在获取泛型的type时有些小坑。

  1. public static void Main(string[] args)
  2. {
  3. A a = new B
  4. {
  5. a = "a",
  6. b = "b",
  7. c = "c",
  8. };
  9. B c = new B
  10. {
  11. a = "a",
  12. b = "b",
  13. c = "c",
  14. };
  15. put(a);
  16. put<A>(c);
  17. put<B>(c);
  18. put<IC>(c);
  19. Console.ReadLine();
  20. }
  21. public static void put<T>(T t)
  22. {
  23.  
  24. Type type1 = typeof(T);
  25. Console.WriteLine();
  26. Console.WriteLine("****************typeof*******************************");
  27. foreach (var item in type1.GetProperties())
  28. {
  29. string name = item.Name;
  30. string value = item.GetValue(t).ToString();
  31. Console.WriteLine("name=" + name + ",value=" + value);
  32. }
  33. Console.WriteLine("****************GetType*******************************");
  34. Type type2 = t.GetType();
  35.  
  36. foreach (var item in type2.GetProperties())
  37. {
  38. string name = item.Name;
  39. string value = item.GetValue(t).ToString();
  40. Console.WriteLine("name=" + name + ",value=" + value);
  41. }
  42.  
  43. }
  44.  
  45. public class A
  46. {
  47. public string a { get; set; }
  48. }
  49. public interface IC
  50. {
  51. string c { get; set; }
  52. }
  53. public class B : A,IC
  54. {
  55. public string c { get; set; }
  56. public string b { get; set; }
  57. }

在看看代码的执行结果:

  发现一个问题 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()有什么妙用的,我们来看下一段代码:

  1. public static void Main(string[] args)
  2. {
  3. D d = new D
  4. {
  5. a = "a",
  6. b = ,
  7. d1 = new D1 { d1 = },
  8. time = DateTime.Now,
  9. };
  10. put2(d);
  11. Console.ReadLine();
  12. }
  13. public static void put2<T>(T t)
  14. {
  15. Type type1 = typeof(T);
  16. Console.WriteLine();
  17. PropertyInfo[] Properties = type1.GetProperties();
  18.  
  19. foreach (PropertyInfo item in Properties)
  20. {
  21. Console.WriteLine(item.GetType().FullName);
  22. string name = item.Name;
  23. object value = item.GetValue(t);
  24.  
  25. Console.WriteLine("参数的命名空间为:" +value.GetType().FullName);
  26. Console.WriteLine("name=" + name + ",value=" + value.ToString());
  27. }
  28. }
  29. public class D
  30. {
  31. public string a { get; set; }
  32. public int b { get; set; }
  33. public DateTime time { get; set; }
  34. private string c { get; set; }
  35. public D1 d1 { get; set; }
  36.  
  37. }
  38. public class D1
  39. {
  40. public int d1 { get; set; }
  41. public override string ToString()
  42. {
  43. return d1.ToString();
  44. }
  45. }

这段代码输出为:

  这段代码的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. 使用mac终端生成RSA私钥和公钥文件

    89:~ zhangwenquan$ 89:~ zhangwenquan$ openssl OpenSSL> genrsa -out rsa_private_key.pem 1024 Gener ...

  2. Appfuse:第一张表维护

    1. 建立表userinfo 列名 描述 UserID 主键.自增 UserName 用户名 Pwd 密码 CreateDate 创建日期 2. 在src/main/resources目录下增加文件h ...

  3. nrm NPM源管理工具

    nrm NPM源管理工具 工具 前端 npm cnpm 今天经过同事介绍,发现一个好玩的东西——nrm(NPM registry manager) nrm 是一个可以快速切换NPM源的node插件.由 ...

  4. CSS3:linear-gradient,线性渐变的使用方法

    CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必须使用图像来实现这些效果,现在通过使用 CSS3 的渐变(gradients)即可实现.此外,渐变效果 ...

  5. SQL Server 2008 R2——当前日期下,一年前数据的统计值

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  请通过右侧公告中的“联系邮 ...

  6. Linux shell脚本编程(二)

    Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...

  7. win7系统c盘瘦身,去虚拟内存方式

    电脑使用过程中,C盘出现个情况,c盘属性上的大小 > c盘内容加起来的大小 原因就是"虚拟内存"在作祟. 运行  powercfg -h off 关闭系统休眠,删除C盘 hi ...

  8. mac 键盘映射 karabiner

    mac 键盘映射 karabiner 今天在vim编辑的时候觉得用mac的方向键有点麻烦 需要移动我的小右手,然后就搜个映射方案. 百度出来了 karabiner. 官网 安装什么的就不说了, 安完了 ...

  9. JavaMail和James

      JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输.我们可以基于JavaMail开发出类似于Micr ...

  10. git免密操作

    windows下找到用户目录,新建 _netrc 文件 machine git.notech.cc login user password xxxxxx Linux下同样可行,需要在~目录下新建 .n ...