C# language feature:

  • Caller info attributes

applies to

  • methods

benefit

  • Use Caller Info attributes to obtain information about the caller to a method i.e. member name, source code line number and file path of the caller. This information can be used for tracing, debugging, and creating diagnostic tools.

how to use

  • Apply the attribute System.Runtime.CompilerServices.? to optional parameters for a method. See example below.

example:

        public void DoProcessing()
        {
            TraceMessage("Something happened.");
        }

        public void TraceMessage(string message,
                [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
                [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
                [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
        {
            System.Console.WriteLine("message: " + message);
            System.Console.WriteLine("member name: " + memberName);
            System.Console.WriteLine("source file path: " + sourceFilePath);
            System.Console.WriteLine("source line number: " + sourceLineNumber);
        }