精彩文章免费看

c#反射入门篇(Reflection)——PropertyInfo 发现属性

也算记录自己的学习篇=。= 适合入门看 这里简单介绍下PropertyInfo和他基本的几个方法

Property发现属性并提供对属性 元数据的访问。

1.如何获取?

1.如何获取?

Type.GetProperty(String) 获取该类的指定的名字String公开的属性 如果私有会为空
Type.GetProperty(String,BindingFlags) 获取该类的指定的名字String,和指定类型BindingFlags的属性
Type.GetProperties() 获取该类的所有公开属性
Type.GetProperties(BindingFlags) 获取该类的所有指定类型BindingFlags的函数方法

先定义个类型
    public class Property
        public int A { get; set; }
        public string B { get; set; }
        private int C { get; set; }
        private string D { get; set; }
Type.GetMethod(String) 获取该类的指定的名字String公开的函数方法 如果私有会为空
Type.GetMethod(String,BindingFlags) 获取该类的指定的名字String,和指定类型BindingFlags的函数方
Type.GetMethods() 获取该类的所有公开的函数方法
Type.GetMethods(BindingFlags) 获取该类的所有指定类型BindingFlags的函数方法
            PropertyInfo property1 = typeof(Property).GetProperty("A");
            PropertyInfo property2 = typeof(Property).GetProperty("C");
            Console.WriteLine("公开的" + property1.Name);
            Console.WriteLine(property2!=null?"存在":"不存在");
            property2 = typeof(Property).GetProperty("C",BindingFlags.Instance|BindingFlags.NonPublic);//BindingFlags.Instance(对象) 和 BindingFlags.Static(静态) 必须有一个,
            Console.WriteLine(property2 != null ? "存在" : "不存在");
            PropertyInfo[] propertys1 = typeof(Property).GetProperties();
            PropertyInfo[] propertys2 = typeof(Property).GetProperties();
            foreach (var item in propertys1)
                Console.WriteLine("公开的"+item.Name);
            propertys2 = typeof(Property).GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
            foreach (var item in propertys2)
                Console.WriteLine("私有的的" + item.Name);
            Console.ReadKey();
       public string a;
       public string A1 { get { Console.WriteLine("运行了一次A1的Get"); return a; } set { Console.WriteLine("运行了一次A1的Get"); a = value; } }
       public string A2 { private get { Console.WriteLine("运行了一次A2的私有的Get"); return a; }  set { Console.WriteLine("运行了一次A2的Set"); a = value; } }
  Property Instance = Activator.CreateInstance(typeof(Property)) as Property;
Instance.a = "凉_开果";
Console.WriteLine(typeof(Property).GetProperty("A2").GetGetMethod()!=null?"存在":"不存在");
Console.WriteLine("用了GetGetMethod(true)之后");
typeof(Property).GetProperty("A2").GetGetMethod(true).Invoke(Instance, null);
Console.WriteLine("A1公开的get 和 set 访问器{0}个", typeof(Property).GetProperty("A1").GetAccessors().Length);
Console.WriteLine("A2公开的get 和 set 访问器{0}个", typeof(Property).GetProperty("A2").GetAccessors().Length);
Console.WriteLine("A2私有和公开的get 和 set 访问器{0}个", typeof(Property).GetProperty("A2").GetAccessors(true).Length);
Console.WriteLine("未调用Set之前a是:{0}", Instance.a);
typeof(Property).GetProperty("A1").GetSetMethod(true).Invoke(Instance, new object[] { "赋值后的开果"});
Console.WriteLine("调用Set后a是:{0}", Instance.a);
Console.ReadKey();

赋值SetValue(object包含这个属性类的实例化对象 , object 要赋的值);

  • 当在派生类中被重写时,为直接或间接的基类上的方法返回MethodInfo 就是找到他的 谁创建的这个函数
  • 先声明代码
  •        public class Property
                 public string A { get; set; }
     Console.WriteLine(typeof(Property).GetProperty("A").GetValue(Instance));
     Console.WriteLine("修改后");
     typeof(Property).GetProperty("A").SetValue(Instance,"修改后的开果");
     Console.WriteLine(typeof(Property).GetProperty("A").GetValue(Instance));
     Console.ReadKey();
    
  •