TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Caller Info Attributes

C# Caller Info Attributes for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Caller Info Attributes

C# provides caller info attributes to get information about the caller method. By using Caller Info attributes, we can get following information.

  • Location of source file
  • Line number at which method is called
  • Caller method name

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.

Type Attribute Description
String CallerFilePathAttribute It is used to get full path of the source file that contains the caller.
Integer CallerLineNumberAttribute It is used to get line number in the source file at which the method is called.
String CallerMemberNameAttribute It is used to get caller method name.

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 Example

using 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





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf