通过与类型参数进行反射创建动作,以代理调用
给定一个简单的对象
public class Foo {
public void RunAction(Action<int, string, byte> action) {
Action(0, null, 0);
}
}
,并给出了一个示例伪代码方法,该方法读取此类以通过反射获得操作类型:
public void DoProxy(object [] calledWithParameters) {
... some code ...
}
public void DoReflection(Foo foo) {
var actionParameterInfo = foo.getType().getMethod("RunAction").GetParameters()[0];
var parameterTypes = actionParameterInfo.ParameterType.GenericTypeArguments;
// Now I have the action type and the Type[] of the necessary parameters.
}
在这种情况下,我如何动态创建一个将我的doproxy调用,并在呼叫上接收到的参数?
Given a simple object
public class Foo {
public void RunAction(Action<int, string, byte> action) {
Action(0, null, 0);
}
}
And given a sample pseudo-code method that reads this class to obtain the action type via reflection:
public void DoProxy(object [] calledWithParameters) {
... some code ...
}
public void DoReflection(Foo foo) {
var actionParameterInfo = foo.getType().getMethod("RunAction").GetParameters()[0];
var parameterTypes = actionParameterInfo.ParameterType.GenericTypeArguments;
// Now I have the action type and the Type[] of the necessary parameters.
}
In this case how could I create dynamically an Action that calls my DoProxy with the received parameters on call ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个代码,将操作参数转换为数组,然后再将其传递给代理。最简单的方法是使用具有不同数量的通用参数的一组通用函数:
然后在doreflection()中:
You need a code which convert action parameters to an array before passing to a proxy. The easiest way is to use set of generic functions with different number of generic arguments:
and then in DoReflection():