2

tl;dr version: nameof(ToCelsius) returns ToCelsius. I want something that returns the name of the underlying function, ToCelsiusByA, though.

My ToCelsius is assigned a concrete implementation thus:

 Func<decimal,decimal> ToCelsius = ToCelsiusByA;

I then call the ToCelsius function:

var degreesC = ToCelsius(degreesF);

I'd like to name of the implemented function (ToCelsiusByA) returned, so I'm trying this:

Console.WriteLine(degreesF + " degrees F is " + degreesC + " degrees C! using " + nameof(ToCelsius));

but, of course, this shows 'ToCelsuis' in the string.

PakiPat
  • 1,020
  • 14
  • 27

1 Answers1

4
Console.WriteLine(theDelegate.Method.Name);

but note: this won't work well for a range of scenarios involving delegates such as combined delegates or lambdas / anonymous methods.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900