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

http://www.cnblogs.com/Jax/archive/2009/10/16/1584527.html

http://www.cnblogs.com/yaozhenfa/p/CSharp_Reflection_1.html

http://www.cnblogs.com/binfire/archive/2013/01/17/2864887.html

C# 反射机制

[c#美味] 使用反射动态创建实例并调用方法

http://www.cnblogs.com/greenerycn/archive/2010/05/19/csharp_reflection_basic.html

http://www.cnblogs.com/jimtomjim/archive/2009/08/08/1541725.html

C# 反射如何取自定义类型的List<>列表的 Type 类型

http://bbs.csdn.net/topics/350135033

typeof: The typeof operator is used to obtain the System.Type object for a type.

运算符,获得某一类型的 System.Type 对象。

Type t = typeof(int);

GetType: Gets the Type of the current instance.

方法,获取当前实例的类型

 int i = 10;

Console.WriteLine(i.GetType());

区别:   Typeof()是运算符而GetType是方法

    • GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用(初始化以后)
    • Typeof()的参数只能是int,string,String,自定义类型,且不能是实例
    • GetType() 和typeof()都返回System.Type的引用。

C#反射技术的简单操作(读取和设置类的属性)

http://www.cnblogs.com/william-lin/archive/2013/06/05/3118233.html

public class A
{
public int Property1 { get; set; }
}
static void Main(){
A aa = new A();
Type type = aa.GetType();//获取类型
System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1");
propertyInfo.SetValue(aa, 5, null);//给对应属性赋值
int value = (int)propertyInfo.GetValue(aa, null);
Console.WriteLine(value );
}

少量属性的自动化操作手动添加几下当然是没有问题的,但是属性数量较多的时候敲起这些繁锁的代码可以困了,再说对扩展和维护性造成很多的不便,这时,就需要使用反射来实现了。

要想对一个类型实例的属性或字段进行动态赋值或取值,首先得得到这个实例或类型的Type,微软已经为我们提供了足够多的方法。

首先建立一个测试的类

  1. public class MyClass
  2. {
  3. public int one { set; get; }
  4. public int two { set; get; }
  5. public int five { set; get; }
  6. public int three { set; get; }
  7. public int four { set; get; }
  8. }
 

然后编写反射该类的代码

  1. MyClass obj = new MyClass();
  2. Type t = typeof(MyClass);
  3. //循环赋值
  4. int i = 0;
  5. foreach (var item in t.GetProperties())
  6. {
  7. item.SetValue(obj, i, null);
  8. i += 1;
  9. }
  10. //单独赋值
  11. t.GetProperty("five").SetValue(obj, 11111111, null);
  12. //循环获取
  13. StringBuilder sb = new StringBuilder();
  14. foreach (var item in t.GetProperties())
  15. {
  16. sb.Append("类型:" + item.PropertyType.FullName + " 属性名:" + item.Name + " 值:" + item.GetValue(obj, null) + "<br />");
  17. }
  18. //单独取值
  19. int five = Convert.ToInt32(t.GetProperty("five").GetValue(obj, null));
  20. sb.Append("单独取five的值:" + five);
  21. string result = sb.ToString();
  22. Response.Write(result);
 

测试显示结果: 
类型:System.Int32 属性名:one 值:0 
类型:System.Int32 属性名:two 值:1 
类型:System.Int32 属性名:five 值:11111111 
类型:System.Int32 属性名:three 值:3 
类型:System.Int32 属性名:four 值:4 
单独取five的值:11111111

了解了类的属性反射使用后,那么方法也是可以这样做的,即t.GetProperties()改为t.GetMethods(),操作方法同上。

注:以上代码中如不能直接使用请添加using System.Text;的引用。

protected virtual void InitAuthority()
{
Dictionary<string, bool> AuthorityDic = CBF.WMS.DAL.RightCtrl.RightCtrl.PageRightCtrl(this.Page.User.Identity.Name.Trim(), this.CurrentModuleID);
foreach (KeyValuePair<string, bool> entry in AuthorityDic)
{
string propertyName = "Enable" + entry.Key.Replace("RGP", "").Trim();
if (this.GetType().GetProperty(propertyName) != null)
{
this.GetType().GetProperty(propertyName)
.SetValue(this, Convert.ChangeType(entry.Value, this.GetType()
.GetProperty(propertyName).PropertyType), null);
}
}
}

typeof与GetType区别及反射的见解的更多相关文章

  1. C# typeof() 和 GetType()区别

    1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...

  2. Typeof() 和 GetType()区别

    1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...

  3. 值类型与引用类型(特殊的string) Typeof和GetType() 静态和非静态使用 参数传递 相关知识

    学习大神博客链接: http://www.cnblogs.com/zhili/category/421637.html 一 值类型与引用类型 需要注意的string 是特殊类型的引用类型. 使用方法: ...

  4. typeof与GetType

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

  5. tips instanceof运算符和typeof运算符的区别

    tips instanceof运算符和typeof运算符的区别  一.instanceof运算符:       此运算符可以判断一个变量是否是某个对象(类)的实例,返回值是布尔类型的(true和fal ...

  6. javascript:typeof与instanceof区别

    from:http://www.wxwdesign.cn/article/skills/javascript_typeof_instanceof.htm JavaScript中typeof和insta ...

  7. typeof,instanceof的区别,扩展知识:显示原型(prototype)与隐式类型(__protot__)

    3.typeof 和instanceof区别 1.typeof 主要用于判断对象类型 console.log(typeof null) //object console.log(typeof unde ...

  8. C# typeof 与GetType()的区别

    C#中Type类的介绍:https://msdn.microsoft.com/zh-cn/library/system.type(VS.80).aspx C#中任何对象都具有GetType()方法,它 ...

  9. typeof和GetType的区别

    http://stackoverflow.com/questions/4537945/what-is-the-difference-of-getting-type-by-using-gettype-a ...

随机推荐

  1. Hive技术文档

    Hive是什么? Hive是蜂房的意思,为什么hadoop上的这层数据仓库叫Hive? 因为生物学上蜂房是一个结构相当精良的建筑,取名Hive足见则个数据仓库在数据存储上也是堪称精良的.Hive是Fa ...

  2. Oracle数据库中有关记录个数的查询

    一.查询表中全部的记录个数 可用两种方法,一种是在oracle的系统表中统计,另一种需要写存储过程统计,方法分别如下. 1.系统表中统计: SELECT sum(num_rows) FROM user ...

  3. Effective java笔记7--线程

    一.对可共享数据的同步访问 synchronized关键字可以保证在同一时刻,只有一个线程在执行一条语句,或者一段代码块.正确地使用同步可以保证其他任何方法都不会看到对象处于不一致的状态中,还能保证通 ...

  4. MongoDB之一介绍(MongoDB与MySQL的区别、BSON与JSON的区别)

    MySQL与MongoDB的操作对比,以及区别 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL ...

  5. 安装Sikulix

    1.sikulix可以在xp,win7,8,10 Mac 10.10.x 以及Linux/Unix 系统上安装 2.安装Java支持 3.下载sikulisetup1.1.0.jar(那里下前篇有介绍 ...

  6. python的编码

    python的编码 1.概述 讲述编码,那么就要涉及到几个方面,包括系统中如何来显示字符,文件中如何来保存字符. 1.1 系统环境 在系统中显示字符,那么就必须要考虑到系统中使用的编码格式. 在lin ...

  7. 禁止Windows安装软件

    今天电脑莫名安装上百度杀毒,想永久解决这个问题. 1.卸载百度杀毒 2.运行cmd-->sc delete 'service name' 3.sc delete BDMiniDlUpdate/B ...

  8. Python状况:为什么PyPy是Python的未来?

    Python 现在已经不仅仅是胶水脚本语言了. 不信?看看下面使用Python的成功案例: YouTube - 主要由 Python编写 NASA Industrial Light & Mag ...

  9. tcpdump dns包(linux高性能编程读书笔记2)

      tcpdump -i eth0 -nt -s 500 port domain host -t A www.baidu.com www.baidu.com is an alias for www.a ...

  10. jquery ajax跨域的完美解决方法(jsonp方式)

    ajax跨域请求的问题,JQuery对于Ajax的跨域请求有两类解决方案,不过都是只支持get方式,接下来为大家详细介绍下客户端JQuery.ajax的调用代码     今天在项目中需要做远程数据加载 ...