Caller Information Attributes C# with Example



Caller Information Attributes C# with Example

C.I.A.s are intended as a simple way of getting attributes from whatever is calling the targeted method. There is 
really only 1 way to use them and there are only 3 attributes. 
Example: 
//This is the "calling method": the method that is calling the target method 
public void doProcess() 
{ 
GetMessageCallerAttributes("Show my attributes."); 
} 
//This is the target method 
//There are only 3 caller attributes 
public void GetMessageCallerAttributes(string message, 
//gets the name of what is calling this method 
[System.Runtime.CompilerServices.CallerMemberName] string memberName = "", 
//gets the path of the file in which the "calling method" is in 
[System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "", 
//gets the line number of the "calling method" 
[System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0) 
{ 
//Writes lines of all the attributes 
System.Diagnostics.Trace.WriteLine("Message: " + message); 
System.Diagnostics.Trace.WriteLine("Member: " + memberName); 
System.Diagnostics.Trace.WriteLine("Source File Path: " + sourceFilePath); 
System.Diagnostics.Trace.WriteLine("Line Number: " + sourceLineNumber); 
} 
Example Output: 
//Message: Show  my  attributes. 
//Member: doProcess 
//Source File Path: c:\Path\To\The\File 
//Line Number: 13 
 

0 Comment's

Comment Form

Submit Comment