c# - 如何将参数作为反射传递/传出? - visual studio 可扩展性 c#

标签 c# visual-studio parameters vsx extensibility

我有一个输出参数。 是否可以将其作为反射传输? 你能给我一些如何做到这一点的例子吗?

最佳答案

我不确定这与 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/

相关文章:

c# - 发布应用时显示停机页面

c# - 使用 DotNetZip 从 zip 文件中提取特定文件夹

c# - 为什么 AutogenerateBindingRedirects 不适用于 Visual Studio 2017 中的 Web.config

visual-studio - 阻止从 Visual Studio 部署到 Azure 函数应用程序

c# - 出现 SharePoint 未知 SPRequest 错误。更多信息 : 0x80070005

c# - 在 ExceptionFilter 中访问 dbcontext

c# - 每1秒自动发送一次消息

windows - 将具有特殊字符的参数从一个批处理文件发送到另一个批处理文件

javascript - 我可以将 $scope 变量从一个函数传递到 Controller 内的另一个函数吗? AngularJS

c# - 如何传递匿名类型作为参数?