Using reflection to get this data is quite expensive.
| Method | Mean | Error | StdDev |
|--------------------- |--------------:|-----------:|-----------:|
| MethodNameReflection | 1,452.4709 ns | 24.5287 ns | 50.6561 ns |
| MethodNameNameOf | 0.0543 ns | 0.0521 ns | 0.0678 ns |
| TypeName | 14.4099 ns | 0.3964 ns | 0.7543 ns |
| TypeNameReflection | 1,659.5866 ns | 33.1575 ns | 90.7682 ns |
And code (using BenchmarkDotNet)
public class CurrentMethodInfoBenchmarks
{
[Benchmark]
public string MethodNameReflection() => MethodBase.GetCurrentMethod().Name;
[Benchmark]
public string MethodNameNameOf() => nameof(MethodNameNameOf);
[Benchmark]
public string TypeName() => GetType().Name;
[Benchmark]
public string TypeNameReflection() => MethodBase.GetCurrentMethod().DeclaringType.Name;
}
CallerMemberAttributes , nameof()
, .