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();

结果
image.png

2.属性

这里就列几个基础的=。=完全的可以自己 去看c#的API

属性 作用
GetMethod 获取此属性的 get 访问器 可以看做是一个带返回值的函数,
SetMethod 获取此属性的 set 访问器 可以看做是带着一个赋值参数T的vold函数
CanRead/CanWrite 返回bool用于判断该属性值是否可读/写
PropertyType 获取该属性的类型Type
MemberType 返回一个枚举表示他是一个属性

3.方法 =。=我就写下基础的几个

  • 获取公开的get访问器 就是一个带返回值的函数 如果为空就是私有或者不存在 MethodInfo GetGetMethod();

  • 获取公开和私有的get访问器 就是一个带返回值的函数MethodInfo GetGetMethod(bool nonPublic);

  • 获取公开的set访问器 就是带着一个赋值参数T的vold函数 如果为空就是私有或者不存在 MethodInfo SetGetMethod();

  • 获取公开和私有的set访问器 带着一个赋值参数T的vold函数 MethodInfo GetSetMethod(bool nonPublic);

  • 获取公开的get set访问器 如果为空就是私有或者不存在 MethodInfo[] GetAccessors();

  • 获取公开和私有的get set访问器MethodInfo[] GetAccessors(bool nonPublic);

    • 先声明个类

        public class Property
     {
       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();
      
      
      • 结果
        image.png
        • 里面的MethodInfo.Invoke不知道什么意思的话 可以看这里MethodInfo
        • Set那里的Invoke 等于运行了一个带参数函数赋值给a的 之后a就被赋值了
  • 获取 object GetValue(Object包含这个属性类的实例化对象);

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

    • 当在派生类中被重写时,为直接或间接的基类上的方法返回MethodInfo 就是找到他的 谁创建的这个函数
    • 先声明代码
      •        public class Property
               {
        
                     public string A { get; set; }
        
                 }
        
    • 例子
      •    Property Instance = new Property();
         Instance.A = "凉_开果";
        
        
         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();
        
      • 结果
        image.png
      • Get就是会调属性的get Set对应是Set

好了 差不多结束了=。= 不定期更新下篇

u3d萌新QQ群844087555——一个除了unity3d啥都会的群

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容