You are on page 1of 4

Caller Information (C# and Visual Basic)

Page 1 of 4

Caller Information (C# and Visual Basic)


Visual Studio 2012 [This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.] By using Caller Info attributes, you can obtain information about the caller to a method. You can obtain file path of the source code, the line number in the source code, and the member name of the caller. This information is helpful for tracing, debugging, and creating diagnostic tools. To obtain this information, you use attributes that are applied to optional parameters, each of which has a default value. The following table lists the Caller Info attributes that are defined in the System.Runtime.CompilerServices1 namespace:

Attribute

Description Full path of the source file that contains the caller. This is CallerFilePathAttribute2 the file path at compile time. Line number in the source file at which the method is CallerLineNumberAttribute3 called. Method or property name of the caller. See Member CallerMemberNameAttribute4 Names later in this topic.

Type String Integer String

Example
The following example shows how to use Caller Info attributes. On each call to the Tr aceM essage method, the caller information is substituted as arguments to the optional parameters. C#

http://msdn.microsoft.com/en-us/library/hh534540(d=printer,v=vs.110).aspx

5/25/2012

Caller Information (C# and Visual Basic)

Page 2 of 4

Remarks
You must specify an explicit default value for each optional parameter. You can't apply Caller Info attributes to parameters that aren't specified as optional. The Caller Info attributes don't make a parameter optional. Instead, they affect the default value that's passed in when the argument is omitted. Caller Info values are emitted as literals into the Intermediate Language (IL) at compile time. Unlike the results of the StackTrace5 property for exceptions, the results aren't affected by obfuscation.

Member Names
You can use the CallerMemberName attribute to avoid specifying the member name as a String argument to the called method. By using this technique, you avoid the problem that Rename Refactoring doesn't change the String values. This benefit is especially useful for the following tasks: Using tracing and diagnostic routines. Implementing the INotifyPropertyChanged6 interface when binding data. This interface allows the property of an object to notify a bound control that the property has changed,

http://msdn.microsoft.com/en-us/library/hh534540(d=printer,v=vs.110).aspx

5/25/2012

Caller Information (C# and Visual Basic)

Page 3 of 4

so that the control can display the updated information. Without the CallerMemberName attribute, you must specify the property name as a literal. The following chart shows the member names that are returned when you use the CallerMemberName attribute.

Calls occurs within

Member name result The name of the method, property, or event from which the call Method, property, or event originated. Constructor Static constructor Destructor User-defined operators or conversions Attribute constructor The string ".ctor" The string ".cctor" The string "Finalize" The generated name for the member, for example, "op_Addition". The name of the member to which the attribute is applied. If the attribute is any element within a member (such as a parameter, a return value, or a generic type parameter), this result is the name of the member that's associated with that element.

No containing member (for example, assemblyThe default value of the optional parameter. level or attributes that are applied to types)

See Also
Reference Attributes (C# and Visual Basic)7 Concepts Common Attributes (C# and Visual Basic)8 Optional Parameters (Visual Basic)9 Named and Optional Arguments (C# Programming Guide)10 Other Resources Programming Concepts11

Links Table
1http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices(v=vs.110).aspx 2

http://msdn.microsoft.com/enus/library/system.runtime.compilerservices.callerfilepathattribute(v=vs.110).aspx
3http://msdn.microsoft.com/en4

us/library/system.runtime.compilerservices.callerlinenumberattribute(v=vs.110).aspx http://msdn.microsoft.com/enus/library/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx
5

http://msdn.microsoft.com/en-us/library/system.exception.stacktrace(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/hh534540(d=printer,v=vs.110).aspx

5/25/2012

Caller Information (C# and Visual Basic)

Page 4 of 4

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged (v=vs.110).aspx
7http://msdn.microsoft.com/en-us/library/z0w1kczw(v=vs.110).aspx 8

http://msdn.microsoft.com/en-us/library/z371wyft(v=vs.110).aspx

9http://msdn.microsoft.com/en-us/library/f25e2b6b(v=vs.110).aspx 10

http://msdn.microsoft.com/en-us/library/dd264739(v=vs.110).aspx

11http://msdn.microsoft.com/en-us/library/dd460655(v=vs.110).aspx

Community Content
2012 Microsoft. All rights reserved.

http://msdn.microsoft.com/en-us/library/hh534540(d=printer,v=vs.110).aspx

5/25/2012

You might also like