我可以在 C# 4 中实现 method_missing 并让它实际返回一个值吗?
我试图根据 IDynamicObject 上的所有 2 篇博客文章找出如何在 C# 4 中实现 method_missing。
我想要做的是拥有一个具有存储库的业务逻辑层,如果业务逻辑层中缺少该方法,只需调用存储库并传递其结果即可。 所以我有一个看起来像这样的类:
public class CustomerServices : IDynamicObject
{
protected CustomerRepository _Repository = new CustomerRepository();
MetaObject IDynamicObject.GetMetaObject(Expression parameter)
{
return new RepositoryMetaObject<CustomerRepository>(_Repository, parameter);
}
}
在 RepositoryMetaObect 中,我实现了 Call 方法,如下所示:
public override MetaObject Call(CallAction action, MetaObject[] args)
{
typeof(T).GetMethod(action.Name).Invoke(_Repository, getParameterArray(args));
return this;
}
(RepositoryMetaObject 代码的其余部分可能并不有趣,但我已将其包含在此处: http://pastie.org/312842)
我认为问题是我从来没有对 Invoke 的结果做任何事情,我只是返回元对象本身。
现在,当我这样做时:
dynamic service = new CustomerServices();
var myCustomer = service.GetByID(1);
GetByID 被调用,但如果我尝试访问 myCustomer 上的属性,它就会挂起。
有人可以帮忙吗?
完整的代码可以在这里下载: https://dl.getdropbox.com/u/277640/业务逻辑层.zip
I was trying to figure out how to implement method_missing in C# 4, based on all of 2 blog posts floating around on IDynamicObject.
What I want to do is have a Business Logic Layer that has a Repository, and if the method is missing from the Business Logic Layer, just call the Repository and pass through its result. So i have a class that looks like this:
public class CustomerServices : IDynamicObject
{
protected CustomerRepository _Repository = new CustomerRepository();
MetaObject IDynamicObject.GetMetaObject(Expression parameter)
{
return new RepositoryMetaObject<CustomerRepository>(_Repository, parameter);
}
}
In RepositoryMetaObect I implement the Call method like so:
public override MetaObject Call(CallAction action, MetaObject[] args)
{
typeof(T).GetMethod(action.Name).Invoke(_Repository, getParameterArray(args));
return this;
}
(The rest of RepositoryMetaObject code probably isn't interesting, but I've included it here: http://pastie.org/312842)
The problem I think is that I'm never doing anything with the result of the Invoke, I'm just returning the MetaObject itself.
Now when I do this:
dynamic service = new CustomerServices();
var myCustomer = service.GetByID(1);
GetByID is called, but if I try to access a property on myCustomer, is just hangs.
Can anyone please help?
Complete code can be downloaded ehre: https://dl.getdropbox.com/u/277640/BusinessLogicLayer.zip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您需要返回一个新的元对象,并将返回值作为常量表达式。
这肯定是此 CodeProject 页面上发生的情况。 值得一试 :)
I believe you need to return a new MetaObject with the returned value as a constant expression.
That's certainly what happens on this CodeProject page. Worth a try :)
而不是
尝试做这样的事情
(仍然不确定为什么,但它对我有用)。
Instead of
Try doing something like this
(still not sure why, but it works for me).
您可以在 service.GetByID(1) 之后的线路上设置断点吗? 看看您从该电话中真正得到了什么。 否则很难说清到底发生了什么。
Can you set a breakpoint on the line after service.GetByID(1)? See what you've really got back from that call. Otherwise it's hard to tell what exactly happened.