我有一个输出参数。 是否可以将其作为反射传输? 你能给我一些如何做到这一点的例子吗?
最佳答案
我不确定这与 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
也是如此。
关于c# - 如何将参数作为反射传递/传出? - visual studio 可扩展性 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3652503/