typeof与GetType区别及反射的见解
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的引用。
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,微软已经为我们提供了足够多的方法。
首先建立一个测试的类
- public class MyClass
- {
- public int one { set; get; }
- public int two { set; get; }
- public int five { set; get; }
- public int three { set; get; }
- public int four { set; get; }
- }
然后编写反射该类的代码
- MyClass obj = new MyClass();
- Type t = typeof(MyClass);
- //循环赋值
- int i = 0;
- foreach (var item in t.GetProperties())
- {
- item.SetValue(obj, i, null);
- i += 1;
- }
- //单独赋值
- t.GetProperty("five").SetValue(obj, 11111111, null);
- //循环获取
- StringBuilder sb = new StringBuilder();
- foreach (var item in t.GetProperties())
- {
- sb.Append("类型:" + item.PropertyType.FullName + " 属性名:" + item.Name + " 值:" + item.GetValue(obj, null) + "<br />");
- }
- //单独取值
- int five = Convert.ToInt32(t.GetProperty("five").GetValue(obj, null));
- sb.Append("单独取five的值:" + five);
- string result = sb.ToString();
- 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区别及反射的见解的更多相关文章
- C# typeof() 和 GetType()区别
1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...
- Typeof() 和 GetType()区别
1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...
- 值类型与引用类型(特殊的string) Typeof和GetType() 静态和非静态使用 参数传递 相关知识
学习大神博客链接: http://www.cnblogs.com/zhili/category/421637.html 一 值类型与引用类型 需要注意的string 是特殊类型的引用类型. 使用方法: ...
- typeof与GetType
typeof: The typeof operator is used to obtain the System.Type object for a type. 运算符,获得某一类型的 System. ...
- tips instanceof运算符和typeof运算符的区别
tips instanceof运算符和typeof运算符的区别 一.instanceof运算符: 此运算符可以判断一个变量是否是某个对象(类)的实例,返回值是布尔类型的(true和fal ...
- javascript:typeof与instanceof区别
from:http://www.wxwdesign.cn/article/skills/javascript_typeof_instanceof.htm JavaScript中typeof和insta ...
- typeof,instanceof的区别,扩展知识:显示原型(prototype)与隐式类型(__protot__)
3.typeof 和instanceof区别 1.typeof 主要用于判断对象类型 console.log(typeof null) //object console.log(typeof unde ...
- C# typeof 与GetType()的区别
C#中Type类的介绍:https://msdn.microsoft.com/zh-cn/library/system.type(VS.80).aspx C#中任何对象都具有GetType()方法,它 ...
- typeof和GetType的区别
http://stackoverflow.com/questions/4537945/what-is-the-difference-of-getting-type-by-using-gettype-a ...
随机推荐
- zoj 2105 Lifting the Stone
题意 裸的计算几何 求多边形重心: #include<iostream> #include<stdio.h> #include<cstring> #inclu ...
- cocos2d-x for android:SimpleGame分析
cocos2d-x for android:SimpleGame分析 作为cocos2d-x的标配DEMO,SimpleGame可算是给入门学cocos2d-x的俺们这些新手门学习的对象了,那么来分析 ...
- Android RemoteViews 11问11答
1.什么是RemoteView? 答:其实就是一种特殊的view结构,这种view 能够跨进程传输.并且这种remoteview 还提供了一些方法 可以跨进程更新界面.具体在android里面 一个是 ...
- bootstrap-datepicker 插件修改为默认中文
bootstrap-datepicker 是一个非常优秀的时间选择插件,默认是英文显示日期的,通过下面几个小修改让其支持默认中文 1.首先将 bootstrap-datepicker.js 另存为 u ...
- poj 2185(二维kmp)
题意:让你求一个最小的覆盖子矩阵. 分析:首先第一点是确定的:那就是这个子矩阵肯定位于左上角,然后按行考虑,求出每一行可能的重复子串的长度,然后取所有行都存在并且长度最短的长度最为最小子矩阵的宽, 最 ...
- JVM参数汇总
一.java启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容:其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都满足 ...
- 存储过程中使用事务与try catch
一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 : ) ) ...
- Sikuli简介
Sikuli是利用屏幕上能够看到的图型做自动化,能够通过这个手段来识别和控制元素,非常适合和Selenium和Robot Framework一起结合起来做自动化. 1.Sikuli主页 http:// ...
- 如何在Docker中部署DzzOffice
一.一些背景 之前研究Docker很久了,并且在公司内部实际使用起来了,目前分两种场景使用Docker 1.作为PAAS,提供一致,统一的编译/测试环境: 2.作为虚拟机,直接分配给新来的开发人员使用 ...
- windows下mysql5.7安装及配置
装完msi后,复制my-default.ini文件,黏贴为my.ini文件,内容修改如下: # For advice on how to change settings please see# htt ...