• 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; 
           } 
     */  
         
  • 相关阅读:
    16--Box2D使用(二、显示物理世界)
    15--Box2D使用(一、创建物理世界)
    14--物理引擎Box2D
    13--游戏存档
    12--使用背景音乐
    11--瓦片地图(一)简单实用
    border
    LESS
    jquery Ajax
    解决跨域访问
  • 原文地址:https://www.cnblogs.com/ShaYeBlog/p/4811297.html
Copyright © 2020-2023  润新知