2

The answers and comments in the following questions provide conflicting information.

How to get the name of the current method from code

Can you use reflection to find the name of the currently executing method?

It is not mentioned in the documentation as well.

https://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx

Community
  • 1
  • 1
Brk
  • 478
  • 2
  • 6
  • 17

1 Answers1

1

No, the JIT compiler is free to inline the method.

You'll need to add the [MethodImpl(MethodImplOptions.NoInlining)] to any method that calls GetCurrentMethod() to prevent it from being inlined.

If you're only interested in the method name, CallerMemberNameAttribute is much easier to use.

Thomas Gerstendörfer
  • 2,646
  • 1
  • 23
  • 21
  • I agree with it not probably prevent the inlining, but in my experience it doesn't take much "complex" code to make the method become ineligible for inlining so I would assume that a call to GetCurrentMethod, though not a hard stop, would still make it vastly less probably that it will be inlined. `CallerMemberNameAttribute` would also be correct in terms of referring to the method/member the code was written in, *even* if the method was inlined. – Lasse V. Karlsen Jan 04 '17 at 09:32
  • Relying on the assumption that the method won't get inlined yields those hard to trace production errors that never show up in debugger – Thomas Gerstendörfer Jan 04 '17 at 09:44
  • You can't rely on an assumption, and I didn't say you should. My point was just that it "probably" makes it less probably that the method will get inlined, because in my experience the more "complex" code you add to a method, the less chance it seems to have to get inlined. The inline heuristics favors simple methods in my experience. – Lasse V. Karlsen Jan 04 '17 at 09:45