也算記錄自己的學習篇=。= 適合入門看 這里簡單介紹下PropertyInfo和他基本的幾個方法
簡介
Property發(fā)現(xiàn)屬性并提供對屬性 元數(shù)據(jù)的訪問。
1.如何獲取?
1.如何獲取?
Type.GetProperty(String) 獲取該類的指定的名字String公開的屬性 如果私有會為空
Type.GetProperty(String,BindingFlags) 獲取該類的指定的名字String,和指定類型BindingFlags的屬性
Type.GetProperties() 獲取該類的所有公開屬性
Type.GetProperties(BindingFlags) 獲取該類的所有指定類型BindingFlags的函數(shù)方法
例子
先定義個類型
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ù)方法 如果私有會為空
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(對象) 和 BindingFlags.Static(靜態(tài)) 必須有一個胖缤,
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 訪問器 可以看做是一個帶返回值的函數(shù)分俯, |
SetMethod | 獲取此屬性的 set 訪問器 可以看做是帶著一個賦值參數(shù)T的vold函數(shù) |
CanRead/CanWrite | 返回bool用于判斷該屬性值是否可讀/寫 |
PropertyType | 獲取該屬性的類型Type |
MemberType | 返回一個枚舉表示他是一個屬性 |
3.方法 =讥此。=我就寫下基礎的幾個
-
獲取公開的get訪問器 就是一個帶返回值的函數(shù) 如果為空就是私有或者不存在 MethodInfo GetGetMethod();
-
獲取公開和私有的get訪問器 就是一個帶返回值的函數(shù)MethodInfo GetGetMethod(bool nonPublic);
-
獲取公開的set訪問器 就是帶著一個賦值參數(shù)T的vold函數(shù) 如果為空就是私有或者不存在 MethodInfo SetGetMethod();
-
獲取公開和私有的set訪問器 帶著一個賦值參數(shù)T的vold函數(shù) 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 等于運行了一個帶參數(shù)函數(shù)賦值給a的 之后a就被賦值了
-
-
-
獲取 object GetValue(Object包含這個屬性類的實例化對象);
-
賦值SetValue(object包含這個屬性類的實例化對象 , object 要賦的值);
- 當在派生類中被重寫時减江,為直接或間接的基類上的方法返回MethodInfo 就是找到他的 誰創(chuàng)建的這個函數(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();
-
結果image.png
- Get就是會調屬性的get Set對應是Set