how to pass/transfer out parameter as reflection? - visual studio extensibility c#

家住魔仙堡 提交于 2019-12-12 09:45:12

问题


I have an out parameter. Is it possible to transfer it as reflection? Can you give me some examples how to do that?


回答1:


I'm not sure what this has to do with VS extensibility, but it's certainly possible to invoke a method with an out parameter by reflection and find out the out parameter's value afterwards:

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]);
    }
}

Note how you need to keep a reference to the array used to pass arguments to the method - that's how you get the out parameter value afterwards. The same is true for ref.



来源:https://stackoverflow.com/questions/3652503/how-to-pass-transfer-out-parameter-as-reflection-visual-studio-extensibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!