c#反射入門篇(Reflection)——PropertyInfo 發(fā)現(xiàn)屬性

也算記錄自己的學(xué)習(xí)篇=。= 適合入門看 這里簡(jiǎn)單介紹下PropertyInfo和他基本的幾個(gè)方法

簡(jiǎn)介

Property發(fā)現(xiàn)屬性并提供對(duì)屬性 元數(shù)據(jù)的訪問。

1.如何獲取?

1.如何獲取?

Type.GetProperty(String) 獲取該類的指定的名字String公開的屬性 如果私有會(huì)為空
Type.GetProperty(String,BindingFlags) 獲取該類的指定的名字String,和指定類型BindingFlags的屬性
Type.GetProperties() 獲取該類的所有公開屬性
Type.GetProperties(BindingFlags) 獲取該類的所有指定類型BindingFlags的函數(shù)方法

例子

先定義個(gè)類型
    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公開的函數(shù)方法 如果私有會(huì)為空
Type.GetMethod(String,BindingFlags) 獲取該類的指定的名字String,和指定類型BindingFlags的函數(shù)方
Type.GetMethods() 獲取該類的所有公開的函數(shù)方法
Type.GetMethods(BindingFlags) 獲取該類的所有指定類型BindingFlags的函數(shù)方法
            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(對(duì)象) 和 BindingFlags.Static(靜態(tài)) 必須有一個(gè),
            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();

結(jié)果
image.png

2.屬性

這里就列幾個(gè)基礎(chǔ)的=。=完全的可以自己 去看c#的API

屬性 作用
GetMethod 獲取此屬性的 get 訪問器 可以看做是一個(gè)帶返回值的函數(shù),
SetMethod 獲取此屬性的 set 訪問器 可以看做是帶著一個(gè)賦值參數(shù)T的vold函數(shù)
CanRead/CanWrite 返回bool用于判斷該屬性值是否可讀/寫
PropertyType 獲取該屬性的類型Type
MemberType 返回一個(gè)枚舉表示他是一個(gè)屬性

3.方法 =。=我就寫下基礎(chǔ)的幾個(gè)

  • 獲取公開的get訪問器 就是一個(gè)帶返回值的函數(shù) 如果為空就是私有或者不存在 MethodInfo GetGetMethod();

  • 獲取公開和私有的get訪問器 就是一個(gè)帶返回值的函數(shù)MethodInfo GetGetMethod(bool nonPublic);

  • 獲取公開的set訪問器 就是帶著一個(gè)賦值參數(shù)T的vold函數(shù) 如果為空就是私有或者不存在 MethodInfo SetGetMethod();

  • 獲取公開和私有的set訪問器 帶著一個(gè)賦值參數(shù)T的vold函數(shù) MethodInfo GetSetMethod(bool nonPublic);

  • 獲取公開的get set訪問器 如果為空就是私有或者不存在 MethodInfo[] GetAccessors();

  • 獲取公開和私有的get set訪問器MethodInfo[] GetAccessors(bool nonPublic);

    • 先聲明個(gè)類

        public class Property
     {
       public string a;

       public string A1 { get { Console.WriteLine("運(yùn)行了一次A1的Get"); return a; } set { Console.WriteLine("運(yùn)行了一次A1的Get"); a = value; } }
       public string A2 { private get { Console.WriteLine("運(yùn)行了一次A2的私有的Get"); return a; }  set { Console.WriteLine("運(yùn)行了一次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}個(gè)", typeof(Property).GetProperty("A1").GetAccessors().Length);
      Console.WriteLine("A2公開的get 和 set 訪問器{0}個(gè)", typeof(Property).GetProperty("A2").GetAccessors().Length);
      Console.WriteLine("A2私有和公開的get 和 set 訪問器{0}個(gè)", typeof(Property).GetProperty("A2").GetAccessors(true).Length);
      
      Console.WriteLine("未調(diào)用Set之前a是:{0}", Instance.a);
      typeof(Property).GetProperty("A1").GetSetMethod(true).Invoke(Instance, new object[] { "賦值后的開果"});
      Console.WriteLine("調(diào)用Set后a是:{0}", Instance.a);
      
      Console.ReadKey();
      
      
      • 結(jié)果
        image.png
        • 里面的MethodInfo.Invoke不知道什么意思的話 可以看這里MethodInfo
        • Set那里的Invoke 等于運(yùn)行了一個(gè)帶參數(shù)函數(shù)賦值給a的 之后a就被賦值了
  • 獲取 object GetValue(Object包含這個(gè)屬性類的實(shí)例化對(duì)象);

  • 賦值SetValue(object包含這個(gè)屬性類的實(shí)例化對(duì)象 , object 要賦的值);

    • 當(dāng)在派生類中被重寫時(shí),為直接或間接的基類上的方法返回MethodInfo 就是找到他的 誰創(chuàng)建的這個(gè)函數(shù)
    • 先聲明代碼
      •        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();
        
      • 結(jié)果
        image.png
      • Get就是會(huì)調(diào)屬性的get Set對(duì)應(yīng)是Set

好了 差不多結(jié)束了=。= 不定期更新下篇

u3d萌新QQ群844087555——一個(gè)除了unity3d啥都會(huì)的群

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容