如何传递/转出参数作为反射? - 视觉工作室可扩展性c#

r.r*_*r.r 4 c# parameters vsx extensibility visual-studio

我有一个out参数.是否可以将其作为反射转移?你能举几个例子怎么做吗?

Jon*_*eet 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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意您需要如何保持对用于将参数传递给方法的数组的引用 - 这就是您之后获取out参数值的方式.同样如此ref.