3
GetCached(BLCustomer.GetAll, "GetAll");

where "GetAll" is session key. How can I do something like this?

GetCached(BLCustomer.GetAll, BLCustomer.GetAll.ToString());

UPDATE:

other worlds I wanna to get the string "GetAll" (not names of customers, but the name of the method) from method name BLCustomer.GetAll().

I want to use something like this

GetCached(BLCustomer.GetSingle, BLCustomer.GetSingle.ToString());

instead of

GetCached(BLCustomer.GetSingle, "GetSingle");

to avoid hardcoding name of methods.

Alexandre
  • 13,030
  • 35
  • 114
  • 173

2 Answers2

4

Change GetCached like this:

ReturnType GetCached(SomeFunc f)
{
  var methodname = f.Method.Name;
  // add rest of code
}

Assumptions:

I guess GetCached actually looks like this currently:

T GetCached<T>(Func<T> accessor, string name)
{
   ...
}

Given the accessor is already a delegate, the name can be determined as shown above.

If not, my suggestion will not work.

The above also assumes that BLCustomer.GetSingle is a method (instance or static should be ok).

The call would then be:

var r = GetCached(BLCustomer.GetSingle); // delegate implicitly created
leppie
  • 115,091
  • 17
  • 196
  • 297
3

SIMPLE SOLUTION: (stolen from leppie below)

Simply remove the second parameter from your GetCached method:

ReturnType GetCached(Func<T> func)
{
    var name = func.Method.Name;

    // execute func here
}

This assumes, that it will be called like this:

GetCached(BLCustomer.GetAll);

and not like this:

GetCached(() => BLCustomer.GetAll());

COMPLEX SOLUTION:

You can do it like this:

string GetMethodName(Expression<Func<Func<dynamic>>> methodExpression)
{
    dynamic memberExpression = methodExpression.Body;
    MethodInfo result = memberExpression.Operand.Arguments[2].Value;
    return result.Name;
}

Call it like this:

GetCached(BLCustomer.GetSingle, GetMethodName(() => BLCustomer.GetSingle));

This approach makes two assumptions:

  1. The call always needs to look like in the example, i.e. it mustn't have a parameter and the body of the delegate must contain only the method you want the name from and nothing else
  2. The method you want the name from must not be of type void and must not have any parameters.

You can use this also for non static methods:

BLCustomer customer = new BLCustomer();
GetCached(customer.GetSingle, GetMethodName(() => customer.GetSingle));

You can even change GetCached to the following to cleanup its API:

ReturnType GetCached<T>(Expression<Func<Func<T>>> methodExpression)
{
    var name = GetMethodName(methodExpression);
    var func = methodExpression.Compile()();

    // execute func and do stuff
}

For this to work, you need to make GetMethodName generic instead of using dynamic:

string GetMethodName<T>(Expression<Func<Func<T>>> methodExpression)
{
    dynamic memberExpression = methodExpression.Body;
    MethodInfo result = memberExpression.Operand.Arguments[2].Value;
    return result.Name;
}

And then you can call it like this:

GetCached<IEnumerable<Customer>>(() => BLCustomer.GetAll)
GetCached<Customer>(() => BLCustomer.GetSingle)
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Your approach is getting closer to what I suggested. I dont think you really need to use `Expression` unless you have closures involved (as per your example). Again, I might be making too many assumptions :) – leppie Jul 13 '11 at 09:43
  • @leppie: I didn't know that was possible... You answer should be the accepted one! As currently mine is, I will update my answer to show how to do it the simple way, because I think your answer is a bit short :-) – Daniel Hilgarth Jul 13 '11 at 09:47
  • Thanks, but I still feel there is too many assumptions to be made here to give an exact answer :) – leppie Jul 13 '11 at 09:50
  • @leppie: You are right. But I think we showed him all possibilities now ;-) – Daniel Hilgarth Jul 13 '11 at 09:52
  • Could you point out the differences between `GetCached(BLCustomer.GetAll);` and `GetCached(() => BLCustomer.GetAll());` I've always thought it's absolutely the same, but it appeared to be not. – StuffHappens Jul 13 '11 at 10:11
  • @StuffHappens: It is not the same, see [here](http://stackoverflow.com/questions/6662532/lamba-expression-and-method-group/6662545#6662545). – Daniel Hilgarth Jul 13 '11 at 10:12