Preventing JIT inlining on a method

后端 未结 2 1916
遥遥无期
遥遥无期 2020-11-29 06:15

I\'ve got sort of a unique situation. I\'ve been working on an open source library for sending email. In this library, I need a reliable way to get the calling method. I\

2条回答
  •  迷失自我
    2020-11-29 07:08

    You could use additional parameters marked with System.Runtime.CompilerServices.CallerMemberNameAttribute and it's siblings CallerFilePath and CallerLineNumber. If I understand it correctly this should get you the correct method name, no matter what's inlined an what's not. You will only get the method name however, I don't see anything to get the class name/assembly etc.

    It should go without saying, but just to be sure... something like this should not be used outside of logging/diagnostics.

    Proper ways to do this would probably be:

    • Pass the required information as a parameter
    • Temporarily store the required information in Thread.ExecutionContext while calling the function

    I realize that this probably won't be of any help to Scott after all this time, but maybe someone else can benefit from it.

提交回复
热议问题