Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method name ToString()

Tags:

c#

.net-4.0

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.

like image 813
Alexandre Avatar asked Aug 23 '26 02:08

Alexandre


1 Answers

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
like image 183
leppie Avatar answered Aug 25 '26 16:08

leppie