3

I have the following method:

public TResult Call<TResult>(Expression<Func<T, TResult>> code) { var returnValue = default(TResult); // code that will inspect the interface method that is being called // along with lots of other code // and then call a WebAPI service. return returnValue; } 

In this instance, T is an interface named ICustomer and TResult will be a class CustomerData In this specific instance, I'm doing the following:

var model = client.Call(customer => customer.Get(1)); 

My ultimate goal with this is to be able to inspect the interface method for certain attributes. Based on those attributes, I'd like to call a WebAPI service and pass to it any parameters that were in the interface method.

How do I figure out in the Call method that the interface.Get(1) method was called?

2
  • You should put some type constraints on T and TResult if they're always going to be those types. Commented Jan 18, 2015 at 19:35
  • @GeorgeMauer they won't always be those types. I went ahead and updated the post. Thanks. Commented Jan 18, 2015 at 19:39

2 Answers 2

4

After fooling around, All I needed to do was cast the Body of the expression as MethodCallExpression.

Sign up to request clarification or add additional context in comments.

Comments

2

You can try using ExpressionVisitor for this. Overriding VisitMethodCall method will let you examine each method call inside the expression. In case of customer => customer.Get(1) you would get a single callback with MethodCallExpression with Object property set to ParameterExpression representing customer, Method parameter set to MethodInfo of the Get method, and Arguments set to a collection of a single constant expression representing integer constant 1.

3 Comments

I'm not able to override VisitMethodCall. It's protected. The only thin you're able to override is Visit.
You can override protected methods in inheritor class. .Net allows to do such things
@JustinAdkins It shouldn't be an issue - take a look at this Q&A for an example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.