Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get MethodInfo from Expression tree

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?

like image 443
Justin Adkins Avatar asked Apr 29 '26 11:04

Justin Adkins


1 Answers

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

like image 154
Justin Adkins Avatar answered May 02 '26 01:05

Justin Adkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!