简体   繁体   中英

using CallerMemberName and MethodImplOptions.NoInlining - Is it necessary?

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM