4
我有一个out参数。 是否有可能将其作为反射转移? 你能举几个例子怎么做吗?如何通过/传出参数作为反射? - visual studio可扩展性c#
我有一个out参数。 是否有可能将其作为反射转移? 你能举几个例子怎么做吗?如何通过/传出参数作为反射? - visual studio可扩展性c#
我不知道这与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
也是如此。
但是如果我不知道第二个参数的类型呢?这是我的问题在这里:http://stackoverflow.com/questions/33338843/dynamically-declare-a-type-for-a-method-out-parameter – alpinescrambler 2015-10-26 06:00:57