也算記錄自己的學(xué)習(xí)篇=定铜。= 適合入門看 這里簡(jiǎn)單介紹下MethodInfo和他基本的幾個(gè)方法
簡(jiǎn)介
MethodInfo就是通過(guò)反射指定類獲取到的 屬性并提供對(duì)方法函數(shù)數(shù)據(jù)的訪問(wèn)。
1.如何獲取?
Type.GetMethod(String) 獲取該類的指定的名字String公開(kāi)的函數(shù)方法 如果私有會(huì)為空
Type.GetMethod(String,BindingFlags) 獲取該類的指定的名字String,和指定類型BindingFlags的函數(shù)方法
Type.GetMethods() 獲取該類的所有公開(kāi)的函數(shù)方法
Type.GetMethods(BindingFlags) 獲取該類的所有指定類型BindingFlags的函數(shù)方法
例子
先定義個(gè)類型
public class Method
{
public int A;
public string B;
public void M(string data)
{
Console.WriteLine(data);
}
public void M1()
{
Console.WriteLine("M1");
}
private void M2()
{
Console.WriteLine("M2");
}
}
Type.GetMethod(String) 獲取該類的指定的名字String公開(kāi)的函數(shù)方法 如果私有會(huì)為空
typeof(Method).GetMethod("M1").Invoke(method,null);
typeof(Method).GetMethod("M").Invoke(method, new string[] { "涼_開(kāi)果"});
Console.ReadKey();
結(jié)果image.png
可以看出來(lái)invoke就是啟動(dòng)函數(shù)的 第一個(gè)是觸發(fā)的類肿嘲,第二個(gè)是要代入的參數(shù)
Type.GetMethod(String,BindingFlags) 獲取該類的指定的名字String,和指定類型BindingFlags的函數(shù)方法
image.png
可以看出來(lái)如果不指定BindingFlags是搜索不到了
看看加上之后的
typeof(Method).GetMethod("M1").Invoke(method,null);
typeof(Method).GetMethod("M").Invoke(method, new string[] { "涼_開(kāi)果"});
typeof(Method).GetMethod("M2",BindingFlags.Instance|BindingFlags.NonPublic).Invoke(method, null);//BindingFlags.Instance(對(duì)象) 和 BindingFlags.Static(靜態(tài)) 必須有一個(gè)葫松,
Console.ReadKey();
image.png
Type.GetMethods() 獲取該類的所有公開(kāi)的函數(shù)方法
Type.GetMethods(BindingFlags) 獲取該類的所有指定類型BindingFlags的函數(shù)方法
MethodInfo[] Methods1= typeof(Method).GetMethods();
MethodInfo[] Methods2 = typeof(Method).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var item in Methods1)
{
Console.WriteLine("不加BindingFlags" + item.Name);
}
foreach (var item in Methods2)
{
Console.WriteLine("加BindingFlags" + item.Name);
}
Console.ReadKey();
結(jié)果image.png
2.屬性
這里就列幾個(gè)基礎(chǔ)的=。=完全的可以自己 去看c#的API
屬性 | 作用 |
---|---|
ReturnType | 獲取這個(gè)函數(shù)方法返回的類型 |
MemberType | 返回一個(gè)MemberTypes枚舉表示 他是個(gè)方法草丧。 |
DeclaringType | 獲取是誰(shuí)聲明該模塊成員的類的Type |
還有一堆is開(kāi)頭的bool屬性 | 都是字面意思 就是判斷是否含有 |
3.方法 =漾岳。=我就寫(xiě)下基礎(chǔ)的幾個(gè)
-
接著上面聲明的類就好
-
運(yùn)行函數(shù)Invoke(object obj, object[] parameters);
- 上面已經(jīng)用過(guò)了
typeof(Method).GetMethod("M1").Invoke(method,null); typeof(Method).GetMethod("M").Invoke(method, new string[] { "涼_開(kāi)果"});
-
結(jié)果image.png
- 可以看出來(lái)invoke就是啟動(dòng)函數(shù)的 第一個(gè)是觸發(fā)的類轰绵,第二個(gè)是要代入的參數(shù)
-
轉(zhuǎn)成委托 Delegate CreateDelegate(Type delegateType, object target);
和Delegate的CreateDelegate一樣的就是少寫(xiě)了個(gè)指定MethodInfo 大概用途是 注冊(cè)事件時(shí)候,需要反射出函數(shù)的委托進(jìn)行注冊(cè)尼荆,
static event Action A1; static event Action<string> A2; static void Main(string[] args) { Method Instance = Activator.CreateInstance(typeof(Method)) as Method; A1?.Invoke(); Console.WriteLine("沒(méi)注冊(cè)之前"); A1+= Instance.GetType().GetMethod("M1").CreateDelegate(typeof(Action), Instance) as Action; A1?.Invoke(); Console.WriteLine("注冊(cè)之后"); Console.ReadKey(); }
-
結(jié)果
image.png- 可以看出 事件注冊(cè)之前沒(méi)東西的通過(guò)轉(zhuǎn)化成的委托進(jìn)行注冊(cè) 就有了 第二個(gè)賦值的是包含這個(gè)函數(shù)的實(shí)例化對(duì)象
-
找到擁有該函數(shù)初始父類 MethodInfo GetBaseDefinition();
- 當(dāng)在派生類中被重寫(xiě)時(shí)左腔,為直接或間接的基類上的方法返回MethodInfo 就是找到他的 誰(shuí)創(chuàng)建的這個(gè)函數(shù)
-
先聲明代碼
public class BaseMethod { public virtual void M3() { } } public class Method: BaseMethod { public int A; public string B; public void M( string data) { Console.WriteLine(data); } public void M1() { Console.WriteLine("M1"); } private void M2(int data) { Console.WriteLine("M2"); } public override void M3() { base.M3(); } }
-
例子
Console.WriteLine(typeof(Method).GetMethod("M3").ReflectedType.Name); Console.WriteLine("創(chuàng)建他的父類"+typeof(Method).GetMethod("M3").GetBaseDefinition().ReflectedType.Name); Console.WriteLine(typeof(Method).GetMethod("ToString").ReflectedType.Name); Console.WriteLine("創(chuàng)建他的父類" + typeof(Method).GetMethod("ToString").GetBaseDefinition().ReflectedType.Name); Console.ReadKey();
-
結(jié)果image.png