Type InvokeMember()用法簡介


舉例:  
  Type tDate = typeof(System.DateTime);  
   Object result = tDate.InvokeMember("Now",   
           BindingFlags.GetProperty, null, null, new Object[0]);  
   Console.WriteLine(result.ToString());  
  
  例2:  /*注意:下面備注的方法都是其他類的方法,比如:TestClass類方法*/  
  TestClass tc = new TestClass (); //AddUp為tc的方法  
  tc.GetType().InvokeMember ("AddUp", BindingFlags.Public |  
               BindingFlags.Instance | BindingFlags.CreateInstance,  
               null, tc, new object [] {});  
/* 
    public void AddUp () 
       { 
           methodCalled++; 
           Console.WriteLine ("AddUp Called {0} times", methodCalled); 
       } 
*/  
//----------------------------下面傳參數 執行ComputeSum方法 帶有兩個參數  
Type t = typeof (TestClass);  
  object [] args = new object [] {100.09, 184.45};  
  object result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args);  
   /* 
    public static double ComputeSum (double d1, double d2) 
       { 
           return d1 + d2; 
       } 
*/  
  //-----------SayHello為靜態方法調用  
   t.InvokeMember ("SayHello", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object [] {});  
  //-----------實例方法調用  
   TestClass c = new TestClass ();  
  c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {});  
  
  c.GetType().InvokeMember ("AddUp", BindingFlags.Public |  
               BindingFlags.Instance | BindingFlags.CreateInstance,  
               null, c, new object [] {});  
  
  //----------獲取字段  
   result = t.InvokeMember ("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});  
   result = t.InvokeMember ("Value", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});   
/* 
    public String Name; 
    public Object Value 
       { 
           get 
           { 
               return "the value"; 
           } 
       } 
*/  
   result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});  
  //---------設置字段  
   t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"}); //NewName設置的屬性值  
  //---------調用類方法 帶參數  
   object[] argValues = new object [] {"Mouse", "Micky"};  
   String [] argNames = new String [] {"lastName", "firstName"};  
   t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);  
   /* 
    public static void PrintName (String firstName, String lastName) 
       { 
           Console.WriteLine ("{0},{1}", lastName,firstName); 
       } 
*/  
TestClass obj = new TestClass();  
System.Reflection.MethodInfo methInfo =  
               obj.GetType().GetMethod("PrintName");  
           methInfo.Invoke(obj,BindingFlags.SuppressChangeType |  
               BindingFlags.InvokeMethod, null,new object[]  
               {"Brad","Smith"},null);  
     
   methInfo = obj.GetType().GetMethod("PrintName");  
           methInfo.Invoke(obj,BindingFlags.IgnoreCase |   //忽略大小寫 指定當綁定時不應考慮成員名的大小寫  
               BindingFlags.InvokeMethod, null,new object[]  
               {"brad","smith"},null);  
  
methInfo = obj.GetType().GetMethod("PrintName");  
           methInfo.Invoke(obj,BindingFlags.IgnoreReturn |  // 在 COM interop 中用於指定可以忽略成員的返回值  
               BindingFlags.InvokeMethod, null,new object[]  
               {"Brad","Smith"},null);   
  
 methInfo = obj.GetType().GetMethod("PrintName");  
           methInfo.Invoke(obj,BindingFlags.OptionalParamBinding |  
               BindingFlags.InvokeMethod, null,new object[]  
               {"Brad","Smith"},null);  
  
           // BindingFlags.ExactBinding  
           methInfo = obj.GetType().GetMethod("PrintName");  
           methInfo.Invoke(obj,BindingFlags.ExactBinding |  
               BindingFlags.InvokeMethod, null,new object[]  
               {"Brad","Smith"},null);  
  
           // BindingFlags.FlattenHierarchy  
           methInfo = obj.GetType().GetMethod("PrintName");  
           methInfo.Invoke(obj,BindingFlags.FlattenHierarchy |  
               BindingFlags.InvokeMethod, null,new object[]  
               {"Brad","Smith"},null);  
  //----------調用一個默認的方法  
   Type t3 = typeof (TestClass2);  
/* 
[DefaultMemberAttribute ("PrintTime")] 
   public class TestClass2 
   { 
       public void PrintTime () 
       { 
           Console.WriteLine (DateTime.Now); 
       } 
   } 
*/  
   t3.InvokeMember ("", BindingFlags.InvokeMethod | BindingFlags.Default, null, new TestClass2(), new object [] {});  
//---------調用一個引用方法  
 MethodInfo m = t.GetMethod("Swap");  
  args = new object[2];  
     args[0] = 1;  
     args[1] = 2;  
     m.Invoke(new TestClass(),args);  
 /* 
    public void Swap(ref int a, ref int b)  交換 a b 
       { 
           int x = a; 
           a = b; 
           b = x; 
       } 
 */  
     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM