如何将参数传递到Action<T>
?代码示例应该突出显示我想要实现的目标。抱歉,有点长。
public class Program
{
public static void Main(string[] args)
{
var foo = new Foo();
foo.GetParams(x => x.Bar(7, "hello"));
}
}
public class Foo
{
public void Bar(int val, string thing) { }
}
public static class Ex
{
public static object[] GetParams<T>(this T obj, Action<T> action)
{
// Return new object[] { 7, "hello" }
}
}
唯一看起来有点有用的选项是 GetInitationList()、Method 和 Target。但它们似乎都不包含我想要的数据(我认为这是因为我声明操作的方式)。谢谢
编辑:这不是我想要的类型,而是实际值 - 正如代码注释中所述。
要做到这一点,它实际上应该是Expression<Action<T>>
。那么就是分解表达式的情况。幸运的是我在 protobuf-net 中有所有的代码,here- 尤其ResolveMethod
,它返回中的值out
数组(在遍历任何捕获的变量等之后)。
制作完成后ResolveMethod
公开(并删除所有内容above ResolveMethod
),代码就是:
public static object[] GetParams<T>(this T obj, Expression<Action<T>> action)
{
ProtoClientExtensions.ResolveMethod<T>(
action, out Action ignoreThis, out object[] args);
return args;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)