C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# Caller Info AttributesC# provides caller info attributes to get information about the caller method. By using Caller Info attributes, we can get following information.
This information is helpful for tracing and debugging of the source code. To implement this feature, we need to use System.Runtime.CompilerService namespace in our application. This namespace contains the following caller info attributes.
A method which we want to call must use optional parameters. These optional parameters are set to default value. C# compiler set caller info to these parameters during execution of the method. Let's see an example. C# Caller Method Info Exampleusing System; using System.Runtime.CompilerServices; namespace CSharpFeatures { class CallerInfoExample { static void Main(string[] args) { // Calling method show(); } // We must specify optional parameters to get caller info. static void show([CallerMemberName] string callerName = null, [CallerFilePath] string callerFilePath = null, [CallerLineNumber] int callerLine = -1) { Console.WriteLine("Caller method Name: {0}", callerName); Console.WriteLine("Caller method File location: {0}", callerFilePath); Console.WriteLine("Caller method Line number: {0}", callerLine); } } } Output Caller method Name: Main Caller method File location: f:\C#\C# Features\CSharpFeatures\CallerInfoExample.cs Caller method Line number: 10
Next TopicC# Using Static Directive
|