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?

Justin Adkins
  • 1,214
  • 2
  • 23
  • 41

2 Answers2

4

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

Justin Adkins
  • 1,214
  • 2
  • 23
  • 41
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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523