Type InvokeMember()用法简介

  1. 举例:  
  2.   Type tDate = typeof(System.DateTime);  
  3.    Object result = tDate.InvokeMember("Now",   
  4.            BindingFlags.GetProperty, null, null, new Object[0]);  
  5.    Console.WriteLine(result.ToString());  
  6.   
  7.   例2:  /*注意:下面备注的方法都是其他类的方法,比如:TestClass类方法*/  
  8.   TestClass tc = new TestClass (); //AddUp为tc的方法  
  9.   tc.GetType().InvokeMember ("AddUp", BindingFlags.Public |  
  10.                BindingFlags.Instance | BindingFlags.CreateInstance,  
  11.                null, tc, new object [] {});  
  12. /* 
  13.     public void AddUp () 
  14.        { 
  15.            methodCalled++; 
  16.            Console.WriteLine ("AddUp Called {0} times", methodCalled); 
  17.        } 
  18. */  
  19. //----------------------------下面传参数 执行ComputeSum方法 带有两个参数  
  20. Type t = typeof (TestClass);  
  21.   object [] args = new object [] {100.09, 184.45};  
  22.   object result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args);  
  23.    /* 
  24.     public static double ComputeSum (double d1, double d2) 
  25.        { 
  26.            return d1 + d2; 
  27.        } 
  28. */  
  29.   //-----------SayHello为静态方法调用  
  30.    t.InvokeMember ("SayHello", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object [] {});  
  31.   //-----------实例方法调用  
  32.    TestClass c = new TestClass ();  
  33.   c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {});  
  34.   
  35.   c.GetType().InvokeMember ("AddUp", BindingFlags.Public |  
  36.                BindingFlags.Instance | BindingFlags.CreateInstance,  
  37.                null, c, new object [] {});  
  38.   
  39.   //----------获取字段  
  40.    result = t.InvokeMember ("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});  
  41.    result = t.InvokeMember ("Value", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});   
  42. /* 
  43.     public String Name; 
  44.     public Object Value 
  45.        { 
  46.            get 
  47.            { 
  48.                return "the value"; 
  49.            } 
  50.        } 
  51. */  
  52.    result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});  
  53.   //---------设置字段  
  54.    t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"}); //NewName设置的属性值  
  55.   //---------调用类方法 带参数  
  56.    object[] argValues = new object [] {"Mouse", "Micky"};  
  57.    String [] argNames = new String [] {"lastName", "firstName"};  
  58.    t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);  
  59.    /* 
  60.     public static void PrintName (String firstName, String lastName) 
  61.        { 
  62.            Console.WriteLine ("{0},{1}", lastName,firstName); 
  63.        } 
  64. */  
  65. TestClass obj = new TestClass();  
  66. System.Reflection.MethodInfo methInfo =  
  67.                obj.GetType().GetMethod("PrintName");  
  68.            methInfo.Invoke(obj,BindingFlags.SuppressChangeType |  
  69.                BindingFlags.InvokeMethod, null,new object[]  
  70.                {"Brad","Smith"},null);  
  71.      
  72.    methInfo = obj.GetType().GetMethod("PrintName");  
  73.            methInfo.Invoke(obj,BindingFlags.IgnoreCase |   //忽略大小写 指定当绑定时不应考虑成员名的大小写  
  74.                BindingFlags.InvokeMethod, null,new object[]  
  75.                {"brad","smith"},null);  
  76.   
  77. methInfo = obj.GetType().GetMethod("PrintName");  
  78.            methInfo.Invoke(obj,BindingFlags.IgnoreReturn |  // 在 COM interop 中用于指定可以忽略成员的返回值  
  79.                BindingFlags.InvokeMethod, null,new object[]  
  80.                {"Brad","Smith"},null);   
  81.   
  82.  methInfo = obj.GetType().GetMethod("PrintName");  
  83.            methInfo.Invoke(obj,BindingFlags.OptionalParamBinding |  
  84.                BindingFlags.InvokeMethod, null,new object[]  
  85.                {"Brad","Smith"},null);  
  86.   
  87.            // BindingFlags.ExactBinding  
  88.            methInfo = obj.GetType().GetMethod("PrintName");  
  89.            methInfo.Invoke(obj,BindingFlags.ExactBinding |  
  90.                BindingFlags.InvokeMethod, null,new object[]  
  91.                {"Brad","Smith"},null);  
  92.   
  93.            // BindingFlags.FlattenHierarchy  
  94.            methInfo = obj.GetType().GetMethod("PrintName");  
  95.            methInfo.Invoke(obj,BindingFlags.FlattenHierarchy |  
  96.                BindingFlags.InvokeMethod, null,new object[]  
  97.                {"Brad","Smith"},null);  
  98.   //----------调用一个默认的方法  
  99.    Type t3 = typeof (TestClass2);  
  100. /* 
  101. [DefaultMemberAttribute ("PrintTime")] 
  102.    public class TestClass2 
  103.    { 
  104.        public void PrintTime () 
  105.        { 
  106.            Console.WriteLine (DateTime.Now); 
  107.        } 
  108.    } 
  109. */  
  110.    t3.InvokeMember ("", BindingFlags.InvokeMethod | BindingFlags.Default, null, new TestClass2(), new object [] {});  
  111. //---------调用一个引用方法  
  112.  MethodInfo m = t.GetMethod("Swap");  
  113.   args = new object[2];  
  114.      args[0] = 1;  
  115.      args[1] = 2;  
  116.      m.Invoke(new TestClass(),args);  
  117.  /* 
  118.     public void Swap(ref int a, ref int b)  交换 a b 
  119.        { 
  120.            int x = a; 
  121.            a = b; 
  122.            b = x; 
  123.        } 
  124.  */  
原文地址:https://www.cnblogs.com/tsql/p/8676741.html