Can I implement a method_missing method in C # 4 and does it return a value?

I was trying to figure out how to implement the method_missing method in C # 4, based on all 2 blog posts floating around on IDynamicObject.

What I want to do is to have a business logic level with a repository, and if this method is not at the business logic level, just call the repository and transfer 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 as follows:

  public override MetaObject Call(CallAction action, MetaObject[] args) { typeof(T).GetMethod(action.Name).Invoke(_Repository, getParameterArray(args)); return this; } 

(The rest of the RepositoryMetaObject code is probably not interesting, but I included it here: http://pastie.org/312842 )

The problem is, I think that I never do anything with the result of Invoke, I just return MetaObject.

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, it just hangs.

Can anybody help?

The full code can be downloaded ehre: https://dl.getdropbox.com/u/277640/BusinessLogicLayer.zip

+4
source share
3 answers

I believe that you need to return a new MetaObject with the return value as a constant expression.

Of course what happens on this CodeProject page . Worth a try:)

+1
source

but if I try to access a property on myCustomer it just freezes

Can you set a breakpoint in the line after the .GetByID (1) service? See what you really got from this call. Otherwise, it is difficult to say exactly what happened.

0
source

Instead

 return this; 

Try to do something like this

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

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

0
source

All Articles