无论参数类型如何,调用对象方法
我使用 DynamicObject 来包装内部对象并屏蔽泛型,但是当我尝试在内部对象上调用某些方法时,它们需要类型化参数,但是我将所有参数视为类型对象,因此调用失败。
代码:
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
try
{
result = mInternalObject.GetType().InvokeMember(binder.Name, (BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public), null, mInternalObject, args);
return true;
}
catch (Exception)
{
return base.TryInvokeMember(binder, args, out result);
}
}
所以基本上,我想知道如何让它忽略参数类型并无论如何调用对象的方法,有什么建议吗?
I am using a DynamicObject to wrap an internal object and mask generics, however when I try to invoke certain methods on the internal object they require typed paramaters, however I am treating all paramaters as the type Object so the invoke fails.
Code:
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
try
{
result = mInternalObject.GetType().InvokeMember(binder.Name, (BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public), null, mInternalObject, args);
return true;
}
catch (Exception)
{
return base.TryInvokeMember(binder, args, out result);
}
}
So basically, I am wondering how to make it ignore the paramater types and invoke the method with the object anyway, any sugestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑你想要类似的东西(伪代码,非常简单):
I suspect you want something along the lines of (psuedo code, very simplified):
由于您使用的是动态,因此您可以使用真正的后期绑定 来自开源 ImpromptuInterface 项目。它比反射更快,并且可以处理更多类型的对象(例如其他动态对象),并且可以推断泛型,同时也可以让您指定,从而使一切变得更加简单。
Instead of reflection, since you are using dynamic, you can use really late binding from the open source ImpromptuInterface project. It's faster than reflection and will work on more kinds of objects (such as other dynamic objects) and can infer generics while alternatively letting you specify as well, making everything much simpler.