如何通过/传递参数作为反射? -Visual Studio可扩展性C# [英] how to pass/transfer out parameter as reflection? - visual studio extensibility c#
本文介绍了如何通过/传递参数作为反射? -Visual Studio可扩展性C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个out参数. 是否可以将其作为反射传递? 你能给我一些例子怎么做吗?
I have an out parameter. Is it possible to transfer it as reflection? Can you give me some examples how to do that?
推荐答案
我不确定这与VS可扩展性有什么关系,但是肯定可以通过反射调用带有out
参数的方法并找出答案之后out
参数的值:
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]);
}
}
请注意您需要如何保留对用于将参数传递给方法的数组的引用-这就是随后获取out
参数值的方式.对于ref
也是一样.
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
.
这篇关于如何通过/传递参数作为反射? -Visual Studio可扩展性C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文