ParameterModifier[] 使用

来源:互联网 发布:ip 连不上数据库mysql 编辑:程序博客网 时间:2024/05/01 16:08

ParameterModifier[]   argument   is   only   used   when   doing   late   bound   COM   interop   calls  
   
  for   your   purpose,   you   only   need   to   do    
   
   
  Type[]   paramTypes   =   new   Type[]   {Type.GetType("System.String&")};  
  MethodInfo   mi   =   typeof(TestRef).GetMethod(   "GetMessage",   paramTypes   );  
   
  try  
   
  using   System;  
  using   System.Reflection;  
   
  class   TestRef  
  {  
      public   int   GetMessage(ref   string   Message,   out   int   value2)  
      {  
  Console.WriteLine(Message);  
  Message   =   "123";  
  value2   =   100;  
  return   2;  
      }  
   
      public   static   void   Main()  
      {  
  TestRef   tr   =   new   TestRef();  
  String   s="";  
  int   out2;  
  int   i   =   tr.GetMessage(ref   s,   out   out2);  
  Console.WriteLine("normal   way:   {0}:{1}:{2}",   i,   s,   out2);  
   
  Type[]   paramTypes   =   new   Type[]   {Type.GetType("System.String&"),   Type.GetType("System.Int32&")};  
  MethodInfo   mi   =   typeof(TestRef).GetMethod(   "GetMessage",   paramTypes   );  
  if   (mi   !=   null)  
  {  
  Console.WriteLine(mi.Name);  
   
  string   s2   =   "abc";  
  object[]   o   =   new   object[2];  
  o[0]   =     s2;  
  int   i2   =   (int)mi.Invoke(tr,   o);  
  Console.WriteLine("reflection:{0}:{1}:{2}",   i2,   o[0],   o[1]);  
  }  
      }  
  }