When a log component is made, the caller information is often logged, usually through reflection to obtain the appropriate information. However,. Net 4.5 introduces three new features, Callerfilepathattribute,callerlinenumberattribute and Callermembernameattribute. These three attributes can only be applied to parameters and are only useful when applied with optional parameters. The idea is simple: if the calling point does not provide an argument, the compiler can use the current file, the number of rows, or the member name as the argument, instead of using the regular default value, and the compiler ignores these attributes if the caller provides the arguments.
1 classProgramcallerlinenumberattribute2 {3 Static voidMain (string[] args)4 {5Log.logwrite ("Test");6 Console.readkey ();7 }8 }9 Ten classLog One { A Public Static voidLogwrite (stringLoginfo, -[Callerfilepath]stringfile=NULL, -[Callerlinenumber]intLine=0, the[Callermembername]stringMember=NULL - ) - { - Console.WriteLine (loginfo); + Console.WriteLine (file); - Console.WriteLine (line); + Console.WriteLine (member); A at } -}
C # Caller Information acquisition