vendredi 20 février 2015

Find the name of the invoked method of a Funz delegate

I have a simple invoker where, in order to be able to put some caching logic, I need to know the name of the invoked method of an object that is a parameter of a Func delegate.



class Program
{
static void Main(string[] args)
{
var proxy = new Proxy();
Invoker.invoke(proxy, p => p.formatSomething("Dumb test"));
}
}

public class Proxy
{
public string formatSomething(string input){

return String.Format("-===={0}====-", input);
}
}


public static class Invoker
{
public static void invoke(Proxy proxy, Func<Proxy,string> online){

//Some caching logic that require the name of the method
//invoked on the proxy (in this specific case "formatSomething")
var methodName = ??;
SomeCacheLogic(methodName);
var output = online(proxy);

}

}


Except adding an error prone string parameter where I could specify the name of the called method to the Invoker, do you know any other better way to inspect and get the methodName the Invoker needs?






Aucun commentaire:

Enregistrer un commentaire