Caller Information attributes are newly introduced in Framework 4.0. There are three attributes which are more helpful in tracing, debugging, and diagnosing the application.
These attributes will provide some information about the calling method to another method via parameters; however, these parameters should be declared as optional or default parameters.
Following are the caller information attributes,
- CallerMemberNameAttribute
The parameter with this attribute will be filled with calling method name. - CallerLineNumberAttribute
The parameter with this attribute will be filled with the line number on source file where this method is called. - CallerFilePathAttribute
The parameter with this attribute will be filled with a complete file path at compile time of the class where this method is called.
These attributes are defined in System.Runtime.CompilerServices namespace.
Enough theory. Let's see some real time examples.
Following is a sample console application where we are trying to print the data assigned to these parameters.
- class Program {
- static void Main(string[] args) {
- LogInfo();
- }
- static void LogInfo([System.Runtime.CompilerServices.CallerMemberName] string methodName = "", [System.Runtime.CompilerServices.CallerFilePath] string filepath = "", [System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) {
- Console.WriteLine("Welcome to c# Corener!");
- Console.WriteLine("Method Name: " + methodName);
- Console.WriteLine("FilePath: " + filepath);
- Console.WriteLine("Line Numer: " + lineNumber);
- Console.ReadLine();
- }
- }
In the above sample application, we created a new method called LogInfo method with three parameters and each assigned with three different caller information attributes we discussed above. Also, these parameters are assigned with default values.
Following is the output window for the above code.
- // Output:
- // Welcome to c# Corener!.
- // Method Name: Main
- //FilePath: D:\Sai\CallerInformation\CallerInformation\Program.cs
- // Line Number: 13
As observed in the output window.
- The method name is printed as "Main" because this LogInfo method is called by the Main method.
- A complete source file path at compile time of the calling method; i.e., the Main method, is printed. If this LogInfo method is in another class or component, the same file path will be printed as it will print the calling method.
- Line Number of the line where this LogInfo method called. As in this case, it is 13.
As these are optional parameters, we can override these values with our own data as below.
- LogInfo("MainMethod");
Now, the output will be as below.
- // Output:
- // Welcome to c# Corener!.
- // Method Name: MainMethod
- //FilePath: D:\Sai\CallerInformation\CallerInformation\Program.cs
- // Line Number: 13
As observed, now, the method Name is printed with the literal given.
Following are the different types of names to be displayed when using in special methods.
- If this LogInfo method is called in any class constructor, then the method name will be ".ctor".
- If this LogInfo method is called in any static constructor, then the method name will be ".cctor".
- If this LogInfo method is called in any destructor, then the method name will be "Finalize".
When the compiler finds these parameters with any of these attributes, it will assign the respective information and proceed only when these parameters are default parameters with the respective default value.
This attribute cannot be used on any properties, fields, or classes as these are targeted to parameters.
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
Values to these parameters will be assigned in Debug mode as well as in Release mode. So, these parameters can be used for any logging purpose too.
Hope this explanation cleared up this concept as I concentrated more on providing technical information instead of theoretical.
No comments:
Post a Comment