我可以在 C# 4 中实现 method_missing 并让它实际返回一个值吗?

发布于 2024-07-08 13:59:33 字数 1363 浏览 13 评论 0原文

我试图根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶花眉 2024-07-15 13:59:33

我相信您需要返回一个新的元对象,并将返回值作为常量表达式。

这肯定是此 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 :)

感受沵的脚步 2024-07-15 13:59:33

而不是

return this;

尝试做这样的事情

return RepositoryMetaObject<CustomerRepository>(
       _Repository
     , System.Linq.Expressions.Expression.Constant(returnValue, returnValueType)
);

(仍然不确定为什么,但它对我有用)。

Instead of

return this;

Try doing something like this

return RepositoryMetaObject<CustomerRepository>(
       _Repository
     , System.Linq.Expressions.Expression.Constant(returnValue, returnValueType)
);

(still not sure why, but it works for me).

ゝ杯具 2024-07-15 13:59:33

但是如果我尝试访问 myCustomer 上的属性,就会挂起

您可以在 service.GetByID(1) 之后的线路上设置断点吗? 看看您从该电话中真正得到了什么。 否则很难说清到底发生了什么。

but if I try to access a property on myCustomer, is just hangs

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文