我有一个out参数。是否可以将其作为反射进行传输?你能给我一些如何做到这一点的例子吗?
发布于 2010-09-06 23:18:28
我不确定这与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也是如此。
https://stackoverflow.com/questions/3652503
复制相似问题