zoukankan      html  css  js  c++  java
  • 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的引用。

    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. public int one { set; get; } 
    3. public int two { set; get; } 
    4. public int five { set; get; } 
    5. public int three { set; get; } 
    6. public int four { set; get; } 
     

    然后编写反射该类的代码

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

  • 相关阅读:
    设计一个洗牌的程序?就是将这副牌进行随机交换
    STL中vector,Map,Set的实现原理
    实现一个Memcpy函数:将源指针所指的区域从起始地址开始的n个字节复制到目的指针所指区域
    四个名词(很常见):虚拟内存,虚拟内存地址(线性地址),物理内存,物理内存地址,逻辑地址
    进程的状态
    ubuntu VNC中Xfce4中Tab键失效的解决方法
    GPU安装
    Parted 手册
    opesntack 底层共享存储 迁移配置
    mysql主从同步
  • 原文地址:https://www.cnblogs.com/chengjun/p/4169907.html
Copyright © 2011-2022 走看看