How-To: User Caller Info Attributes

Article translations Article translations
Article ID: 2778991 - View products that this article applies to.
Expand all | Collapse all

On This Page

About Author:

Collapse this tableExpand this table
Collapse this imageExpand this image
2401266
This article is provided by Sanchez Roman Canlas, a Senior Developer Consultant at US Technologies Global and a core member of the Phillipine Windows Developer Group

Caller Info Attributes

You can tag optional parameters in your method using one of the 3 caller info attributes and use that information for logging purposes
  • [CallerMemberInfoName]
  • [CallerFilePath]
  • [CallerLineNumber]
These attributes tells the compiler to deliver the information retrieved from the caller's source code into the parameter's default value

Example

using System;
using System.Runtime.CompilerServices;

namespace CallerInfoAttributesDemo
{
class Program
{
static void Main(string[] args)
{
LogCallerInfo();
}

private static void LogCallerInfo
(
[CallerMemberName] string callersMemberName = null,
[CallerFilePath] string callersSourceCodePath = null,
[CallerLineNumber] string callersSourceCodeLineNumber = 0
)
{
Console.WriteLine(callersMemberName); // displays the caller's member name
Console.WriteLine(callersSourceCodePath); // displays the caller's source code file
Console.WriteLine(callersSourceCodeLineNumber); // displays the line number in the caller's source code
Console.ReadLine();
}
}
}

Output

Main
C:\Test\Program.cs
10

Community Solutions Content Disclaimer

MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

Properties

Article ID: 2778991 - Last Review: October 31, 2012 - Revision: 2.0
Applies to
  • Microsoft Visual C# 2010 Express
Keywords: 
KB2778991

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com