Search notes:

.NET attributes for caller information

Classes that inherit from System.Attribute that provide information about a caller are:

Example

Class for logging

public static class logger {

   public static void write(
                                                                string txt     ,
     [System.Runtime.CompilerServices.CallerMemberName        ] string mbr = "",
     [System.Runtime.CompilerServices.CallerFilePath          ] string fil = "",
     [System.Runtime.CompilerServices.CallerLineNumber        ] int    lin =  0
//   [System.Runtime.CompilerServices.CallerArgumentExpression] string arg = ""  // Implemented in compiler for C# 10 and later
   )
   {

      System.Console.WriteLine("logged: "    + txt);
      System.Console.WriteLine("  member = " + mbr);
      System.Console.WriteLine("  file   = " + fil);
      System.Console.WriteLine("  line   = " + lin);

   }
}

main.cs

public static class M {
   public static void Main() {
      logger.write("Program is started");
      Obj obj = new Obj();
      obj.go("Hello world.");
   }
}

Another source file

class Obj {
   public void go(string txt) {
      logger.write("go was called, txt = " + txt);
   }
}

Compilation on the command line

csc main.cs logger.cs obj.cs

Index