2010-09-06 45 views

回答

13

我不知道这与VS扩展做的,但它肯定可以通过反射调用的方法与out参数事后找出out参数的值:

using System; 
using System.Reflection; 

class Test 
{ 
    static void Main() 
    { 
     MethodInfo method = typeof(int).GetMethod 
      ("TryParse", new Type[] { typeof(string), 
             typeof(int).MakeByRefType() }); 

     // Second value here will be ignored, but make sure it's the right type 
     object[] args = new object[] { "10", 0 }; 

     object result = method.Invoke(null, args); 
     Console.WriteLine("Result: {0}", result); 
     Console.WriteLine("args[1]: {0}", args[1]); 
    } 
} 

注意如何您需要保留对用于向该方法传递参数的数组的引用 - 这就是事后获得out参数值的方式。 ref也是如此。

+0

但是如果我不知道第二个参数的类型呢?这是我的问题在这里:http://stackoverflow.com/questions/33338843/dynamically-declare-a-type-for-a-method-out-parameter – alpinescrambler 2015-10-26 06:00:57