2

Quick background: Trying to create a method that will return a pre-defined string based on the method that calls it. (bit glossed over but near enough).

I am intending to use the [CallerMemberName] Attribute, but my question is:

=> Do I need to use the MethodImplOptions.NoInlining with this attribute, or is that implicitly applied by the compiler?

There are many examples of both the CallerMemberName attribute, and the older version for pre .net4.5 frameworks which use MethodImplOptions.NoInlining method attribute but nothing which explicitly say that you dont need to use them together?

Thanks in advance, and for reading!

Chris Watts
  • 822
  • 1
  • 9
  • 27
  • "Do I need to use the MethodImplOptions.NoInlining with this attribute, or is that implicitly applied by the compiler?" Why? What problem are you trying to solve? What did you try to diagnose/solve that? – Patrick Hofman Nov 06 '17 at 13:34
  • What i identified was that the `NoInlining` Attribute would cause the compiler to deoptimise that section of code, so that you wouldnt get the caller of the caller name. What i thought was that the `CallerMemberName` attribute would have the same issue unless it had this inlining turned off implicitly? Just trying to clarify the situation on it. More preventative, rather than fixing a bug. – Chris Watts Nov 06 '17 at 13:37
  • Can you show a code sample that you think benefits of that attribute? – Patrick Hofman Nov 06 '17 at 13:40
  • It was a question on SO that made me consider it, see here: https://stackoverflow.com/questions/3095696/how-do-i-get-the-calling-method-name-and-type-using-reflection the comments mention using the attribute to prevent inlining. Seems like EvK has the answer though! – Chris Watts Nov 06 '17 at 13:44

1 Answers1

3

It's not necessary because CallerMemberName is resolved by compiler at compile time:

Caller Info values are emitted as literals into the Intermediate Language (IL) at compile time.

Inlining is done not by C# compiler but by JIT when compiling your IL code to assembly, so inlining cannot affect functionality provided by CallerMemberName.

Evk
  • 98,527
  • 8
  • 141
  • 191