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. taobao

    taobao */--> UP | HOME taobao Table of Contents 1 taobao 1 taobao 欣然小铺 Date: 2013-09-25 Wen Autho ...

  2. 【转】 iOS开发之打包上传到App Store——(一)各种证书的理解

    OK,有日子没写iOS开发的相关文章啦,主要是最近的精力都没在这上面,不过既然产品已经快要出来了,就有必要了解一下各种证书啥的(众所周知iOS的一堆证书可是很让人头大呀),最近确实被这个搞得头大,然后 ...

  3. Senparc.Weixin.MP SDK 微信公众平台开发教程(二):成为开发者

    Senparc.Weixin.MP SDK 微信公众平台开发教程(二):成为开发者 这一篇主要讲作为一名使用公众平台接口的开发者,你需要知道的一些东西.其中也涉及到一些微信官方的规定或比较掩蔽的注意点 ...

  4. linux apache 配置fastcgi

    Redhat 上 FastCGI 安装与配置 软件包 相关软件包: httpd 2.2.14      //注意版本 这个版本不会出问题   注:apache httpd安装 fcgi-2.4.0.t ...

  5. DDoS攻防战(二):CC攻击工具实现与防御理论

    我们将要实现一个进行应用层DDoS攻击的工具,综合考虑,CC攻击方式是最佳选择,并用bash shell脚本来快速实现并验证这一工具,并在最后,讨论如何防御来自应用层的DDoS攻击. 第一步:获取大量 ...

  6. centos7虚拟机无法上网的解决办法

    今天在VMware虚拟机中经过千辛万苦终于安装好了centos7..正兴致勃勃的例行yum update 却发现centos系统貌似默认网卡没配置好,反馈无法联网.经过一番研究,终于让centos连上 ...

  7. CString类Format()的用法 .xml

    pre{ line-height:1; color:#9f1d66; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#5d57ff; ...

  8. nagios监控远程主机端口

    1 被监控主机上的操作 修改nrpe插件内容: 在其中增加的内容如下: 表示的含义为监控主机的端口631和661,这个主要是监控命令 重启xinetd服务: 2 监控主机上的操作 查看监控命令配置文件 ...

  9. 黑马程序员——Block数据类型

    Block数据类型,又被称为代码段.因为它可以封装一段代码.苹果官方建议多用block.因为在多线程控制.异步任务,集合遍历.集合排序.动画转场等方面用的很多. Block的特点: 1.Block 用 ...

  10. Lync激活用户遇到ConstraintViolationNoLeadingOrTrailingWhitespace错误

    启用用户的时候出现错误ConstraintViolationNoLeadingOrTrailingWhitespace,如下图 解决方案:域控中,该用户的名字最后多出了个空格,批量生成域用户的脚本问题 ...