版本:.NET Framework 3.5
先來一個(gè)反射調(diào)用方法的例子:
using System;using System.Reflection;class Example{ static void Main() { Type t = typeof(String); MethodInfo substr = t.GetMethod("Substring", new Type[] { typeof(int), typeof(int) }); // 用MethodBase類的Invoke(object obj, object[] parameters)方法 Object result = substr.Invoke("Hello, World!", new Object[] { 7, 5 });// 射調(diào)用String的Substring(int startIndex, int length)方法 Console.WriteLine("{0} returned \"{1}\".", substr, result); }}
/* 輸出:
System.String Substring(Int32, Int32) returned “World”.
*/
進(jìn)入正題:通過Type.InvokeMethod實(shí)現(xiàn)方法的重載
1)介紹type
Type 為 System.Reflection 功能的根,也是訪問元數(shù)據(jù)的主要方式。使用 Type 的成員獲取關(guān)于類型聲明的信息,如構(gòu)造函數(shù)、方法、字段、屬性和類的事件,以及在其中部署該類的模塊和程序集。
在多線程方案中,不要鎖定 Type 對(duì)象以同步對(duì) static 數(shù)據(jù)的訪問。其他不受您控制的代碼可能也會(huì)鎖定您的類類型。這可能會(huì)導(dǎo)致死鎖。應(yīng)轉(zhuǎn)而通過鎖定私有 static 對(duì)象來同步對(duì)靜態(tài)數(shù)據(jù)的訪問。
這個(gè)類是線程安全的;多個(gè)線程可以同時(shí)從此類型的一個(gè)實(shí)例讀取數(shù)據(jù)。Type 的實(shí)例可表示以下任何類型:
類
值類型
數(shù)組
接口
指針
枚舉
構(gòu)造泛型類型和泛型類型定義
構(gòu)造泛型類型、泛型類型定義和泛型方法定義的類型實(shí)參和類型形參
2)實(shí)例
Assembly assembly = Assembly.LoadFrom(sDllName);
Type fType = assembly.GetType(sClassName);
object instance = Activator.CreateInstance (fType);
fType.InvokeMember(“classmethod”,BindingFlags.InvokeMethod,null,instance,sParams);//調(diào)用指定實(shí)例instance的classmethod方法,sParams為傳入?yún)?shù),數(shù)量不固定,達(dá)到方法的重載
3)擴(kuò)展
枚舉類BindingFlags各屬性含義
Default 不指定綁定標(biāo)志。
IgnoreCase 指定當(dāng)綁定時(shí)不應(yīng)考慮成員名的大小寫。
DeclaredOnly 指定只應(yīng)考慮在所提供類型的層次結(jié)構(gòu)級(jí)別上聲明的成員。不考慮繼承成員。
Instance 指定實(shí)例成員將包括在搜索中。
Static 指定靜態(tài)成員將包括在搜索中。
Public 指定公共成員將包括在搜索中。
NonPublic 指定非公共成員將包括在搜索中。
FlattenHierarchy 指定應(yīng)返回層次結(jié)構(gòu)上的公共靜態(tài)成員和受保護(hù)的靜態(tài)成員。不返回繼承類中的私有靜態(tài)成員。靜態(tài)成員包括字段、方法、事件和屬性。不返回嵌套類型。
InvokeMethod 指定要調(diào)用一個(gè)方法。它不能是構(gòu)造函數(shù)或類型初始值設(shè)定項(xiàng)。
CreateInstance 指定“反射”應(yīng)該創(chuàng)建指定類型的實(shí)例。調(diào)用與給定參數(shù)匹配的構(gòu)造函數(shù)。忽略提供的成員名。如果未指定查找類型,將應(yīng)用 (Instance |Public)。調(diào)用類型初始值設(shè)定項(xiàng)是不可能的。
GetField 指定應(yīng)返回指定字段的值。
SetField 指定應(yīng)設(shè)置指定字段的值。
GetProperty 指定應(yīng)返回指定屬性的值。
SetProperty 指定應(yīng)設(shè)置指定屬性的值。對(duì)于 COM 屬性,指定此綁定標(biāo)志與指定 PutDispProperty 和 PutRefDispProperty 是等效的。
PutDispProperty 指定應(yīng)調(diào)用 COM 對(duì)象的 PROPPUT 成員。PROPPUT 指定使用值的屬性設(shè)置函數(shù)。如果屬性同時(shí)具有 PROPPUT 和 PROPPUTREF,而且需要區(qū)分調(diào)用哪一個(gè),請(qǐng)使用 PutDispProperty。
PutRefDispProperty 指定應(yīng)調(diào)用 COM 對(duì)象的 PROPPUTREF 成員。PROPPUTREF 指定使用引用而不是值的屬性設(shè)置函數(shù)。如果屬性同時(shí)具有 PROPPUT 和 PROPPUTREF,而且需要區(qū)分調(diào)用哪一個(gè),請(qǐng)使用 PutRefDispProperty。
ExactBinding 指定提供參數(shù)的類型必須與對(duì)應(yīng)形參的類型完全匹配。如果調(diào)用方提供一個(gè)非空 Binder 對(duì)象,則“反射”將引發(fā)異常,因?yàn)檫@意味著調(diào)用方正在提供的 BindToXXX 實(shí)現(xiàn)將選取適當(dāng)?shù)姆椒ā?br>SuppressChangeType 未實(shí)現(xiàn)。
OptionalParamBinding 返回其參數(shù)計(jì)數(shù)與提供參數(shù)的數(shù)目匹配的成員集。此綁定標(biāo)志用于所帶參數(shù)具有默認(rèn)值的方法和帶變量參數(shù) (varargs) 的方法。此標(biāo)志應(yīng)只與 Type.InvokeMember 一起使用。
IgnoreReturn 在 COM interop 中用于指定可以忽略成員的返回值。
舉例
using System;using System.Reflection;using System.IO;namespace BindingFlagsSnippet{ class EntryPoint { static void Main(string[] args) { Invoke.Go(); } } class Invoke { public static void Go() { // BindingFlags.InvokeMethod // Call a static method. Type t = typeof (TestClass); Console.WriteLine(); Console.WriteLine("Invoking a static method."); Console.WriteLine("-------------------------"); t.InvokeMember ("SayHello", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object [] {}); // BindingFlags.InvokeMethod // Call an instance method. TestClass c = new TestClass (); Console.WriteLine(); Console.WriteLine("Invoking an instance method."); Console.WriteLine("----------------------------"); c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {}); c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {}); // BindingFlags.InvokeMethod // Call a method with parameters. object [] args = new object [] {100.09, 184.45}; object result; Console.WriteLine(); Console.WriteLine("Invoking a method with parameters."); Console.WriteLine("---------------------------------"); result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args); Console.WriteLine ("{0} {1} = {2}", args[0], args[1], result); // BindingFlags.GetField, SetField Console.WriteLine(); Console.WriteLine("Invoking a field (getting and setting.)"); Console.WriteLine("--------------------------------------"); // Get a field value. result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {}); Console.WriteLine ("Name == {0}", result); // Set a field. t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"}); result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {}); Console.WriteLine ("Name == {0}", result); Console.WriteLine(); Console.WriteLine("Invoking an indexed property (getting and setting.)"); Console.WriteLine("--------------------------------------------------"); // BindingFlags.GetProperty // Get an indexed property value. int index = 3; result = t.InvokeMember ("Item", BindingFlags.GetProperty, null, c, new object [] {index}); Console.WriteLine ("Item[{0}] == {1}", index, result); // BindingFlags.SetProperty // Set an indexed property value. index = 3; t.InvokeMember ("Item", BindingFlags.SetProperty, null, c, new object [] {index, "NewValue"}); result = t.InvokeMember ("Item", BindingFlags.GetProperty , null, c, new object [] {index}); Console.WriteLine ("Item[{0}] == {1}", index, result); Console.WriteLine(); Console.WriteLine("Getting a field or property."); Console.WriteLine("----------------------------"); // BindingFlags.GetField // Get a field or property. result = t.InvokeMember ("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {}); Console.WriteLine ("Name == {0}", result); // BindingFlags.GetProperty result = t.InvokeMember ("Value", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {}); Console.WriteLine ("Value == {0}", result); Console.WriteLine(); Console.WriteLine("Invoking a method with named parameters."); Console.WriteLine("---------------------------------------"); // BindingFlags.InvokeMethod // Call a method using named parameters. object[] argValues = new object [] {"Mouse", "Micky"}; String [] argNames = new String [] {"lastName", "firstName"}; t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames); Console.WriteLine(); Console.WriteLine("Invoking a default member of a type."); Console.WriteLine("------------------------------------"); // BindingFlags.Default // Call the default member of a type. Type t3 = typeof (TestClass2); t3.InvokeMember ("", BindingFlags.InvokeMethod | BindingFlags.Default, null, new TestClass2(), new object [] {}); // BindingFlags.Static, NonPublic, and Public // Invoking a member by reference. Console.WriteLine(); Console.WriteLine("Invoking a method by reference."); Console.WriteLine("-------------------------------"); MethodInfo m = t.GetMethod("Swap"); args = new object[2]; args[0] = 1; args[1] = 2; m.Invoke(new TestClass(),args); Console.WriteLine ("{0}, {1}", args[0], args[1]); // The string is case-sensitive. Type type = Type.GetType("System.String"); // Check to see if the value is valid. If the object is null, the type does not exist. if (type == null) { Console.WriteLine("Please ensure that you specify only valid types in the type field."); Console.WriteLine("The type name is case-sensitive."); return; } // Declare and populate the arrays to hold the information. // You must declare either NonPublic or Public with Static or the search will not work. FieldInfo [] fi = type.GetFields (BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); // BindingFlags.NonPublic MethodInfo [] miNonPublic = type.GetMethods (BindingFlags.Static | BindingFlags.NonPublic); // BindingFlags.Public MethodInfo [] miPublic = type.GetMethods (BindingFlags.Static | BindingFlags.Public); // Iterate through all the nonpublic methods. foreach (MethodInfo method in miNonPublic) { Console.WriteLine(method); } // Iterate through all the public methods. foreach (MethodInfo method in miPublic) { Console.WriteLine(method); } // Iterate through all the fields. foreach (FieldInfo f in fi) { Console.WriteLine(f); } // BindingFlags.Instance // Call an instance method. TestClass tc = new TestClass (); Console.WriteLine(); Console.WriteLine("Invoking an Instance method."); Console.WriteLine("----------------------------"); tc.GetType().InvokeMember ("AddUp", BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, tc, new object [] {}); // BindingFlags.CreateInstance // Calling and creating an instance method. Console.WriteLine(); Console.WriteLine("Invoking and creating an instance method."); Console.WriteLine("-----------------------------------------"); tc.GetType().InvokeMember ("AddUp", BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, tc, new object [] {}); // BindingFlags.DeclaredOnly TestClass tc2 = new TestClass(); Console.WriteLine(); Console.WriteLine("DeclaredOnly members"); Console.WriteLine("---------------------------------"); System.Reflection.MemberInfo[] memInfo = tc2.GetType().GetMembers(BindingFlags.DeclaredOnly); for(int i=0;i<memInfo.Length;i ) { Console.WriteLine(memInfo[i].Name); } // BindingFlags.SuppressChangeType TestClass obj = new TestClass(); Console.WriteLine(); Console.WriteLine("Invoking static method - PrintName"); Console.WriteLine("---------------------------------"); System.Reflection.MethodInfo methInfo = obj.GetType().GetMethod("PrintName"); methInfo.Invoke(obj,BindingFlags.SuppressChangeType | BindingFlags.InvokeMethod, null,new object[] {"Brad","Smith"},null); // BindingFlags.IgnoreCase Console.WriteLine(); Console.WriteLine("Using IgnoreCase and invoking the PrintName method."); Console.WriteLine("---------------------------------------------------"); methInfo = obj.GetType().GetMethod("PrintName"); methInfo.Invoke(obj,BindingFlags.IgnoreCase | BindingFlags.InvokeMethod, null,new object[] {"brad","smith"},null); // BindingFlags.IgnoreReturn Console.WriteLine(); Console.WriteLine("Using IgnoreReturn and invoking the PrintName method."); Console.WriteLine("-----------------------------------------------------"); methInfo = obj.GetType().GetMethod("PrintName"); methInfo.Invoke(obj,BindingFlags.IgnoreReturn | BindingFlags.InvokeMethod, null,new object[] {"Brad","Smith"},null); // BindingFlags.OptionalParamBinding Console.WriteLine(); Console.WriteLine("Using OptionalParamBinding and invoking the PrintName method."); Console.WriteLine("-------------------------------------------------------------"); methInfo = obj.GetType().GetMethod("PrintName"); methInfo.Invoke(obj,BindingFlags.OptionalParamBinding | BindingFlags.InvokeMethod, null,new object[] {"Brad","Smith"},null); // BindingFlags.ExactBinding Console.WriteLine(); Console.WriteLine("Using ExactBinding and invoking the PrintName method."); Console.WriteLine("-----------------------------------------------------"); methInfo = obj.GetType().GetMethod("PrintName"); methInfo.Invoke(obj,BindingFlags.ExactBinding | BindingFlags.InvokeMethod, null,new object[] {"Brad","Smith"},null); // BindingFlags.FlattenHierarchy Console.WriteLine(); Console.WriteLine("Using FlattenHierarchy and invoking the PrintName method."); Console.WriteLine("---------------------------------------------------------"); methInfo = obj.GetType().GetMethod("PrintName"); methInfo.Invoke(obj,BindingFlags.FlattenHierarchy | BindingFlags.InvokeMethod, null,new object[] {"Brad","Smith"},null); } }
轉(zhuǎn)載:https://www.cnblogs.com/snake-hand/p/3143075.html
來源:https://www.icode9.com/content-4-280851.html聯(lián)系客服