none
CallerMemberNameAttribute and Inlining

    Question

  • Hope I hadn't overseen the matching thread (found nothing about this topic in the docs)…

    When using CallerMemberNameAttribute, is it assured by the framework that even if the method is inlined by the optimizing compiler, the member name is set to the caller, and not to the caller's caller?

    Sample:

    void MethodNeedsCallersName([CallerMemberName] caller = null) 
    { 
      Console.WriteLine(caller); 
    }
    
    void CallerMethod() { MethodNeedsCallersName(); }
    
    void CallersCallerMethod() { CallerMethod(); } 
    
    

    In case MethodNeedsCallersName will be inlined by optimization, what will be the console output of MethodNeedsCallersName when CallersCallerMethod is invoked: "CallerMethod" or "CallersCallerMethod"?

    Or should I always use [MethodImpl(MethodImplOptions.NoInlining)] on methods using the CallerMemberNameAttribute to make sure the correct name will be passed?

    Thanks

    Stefan
    Monday, September 16, 2013 4:34 PM

Answers

  • The CallerMemberName is processed by the C# compiler.

    In the CallerMethod the call MethodNeedsCallerName() is replaced by MethodNeedsCallerName("CallerMethod"). Now the JIT compiler can inline all those functions into a single one (and it actually does it), that won't change the fact that the "CallerMethod" name is used as an argument.

    • Marked as answer by rstg Tuesday, September 17, 2013 7:37 AM
    Tuesday, September 17, 2013 5:34 AM
    Moderator

All replies

  • The CallerMemberName is processed by the C# compiler.

    In the CallerMethod the call MethodNeedsCallerName() is replaced by MethodNeedsCallerName("CallerMethod"). Now the JIT compiler can inline all those functions into a single one (and it actually does it), that won't change the fact that the "CallerMethod" name is used as an argument.

    • Marked as answer by rstg Tuesday, September 17, 2013 7:37 AM
    Tuesday, September 17, 2013 5:34 AM
    Moderator
  • Mike,

    Thanks for the explanation!

    Regards,

    Stefan

    Tuesday, September 17, 2013 7:41 AM