Caller Information with .NET 4.5 or „Who touched the function?“


Debugging and Logging Code are usually full of function names and so on just so you are able to find the right place in the code at the end. Of course there are several other reasons to find out who was the last one to open the code. The interface INotifyPropertyChanged is such an example because you need the names of the properties.

It’s possible to solve this static – but usually the refactoring doesn’t work out. .NET 4.5 offers a nice solution for this problem. I’m sure most of you have already heard about it but I just found out recently.

Caller Information

.NET 4.5 offers the option to decode three attribute parameter. These parameter are filled with “invocation” information’s.

CallerMemberName: shows the name of the chosen method or the property
CallerFilePath: shows the complete file path
CallerLineNumber: The line number of the invoke code

The attributes are situated in the System.Runtime CompilerServices namespace.


1: class Program 2: { 3: static void Main(string[] args) 4: { 5: Log("Hello World..."); 6: Console.ReadLine(); 7: } 8: 9: public static void Log(string text, [CallerMemberName] string callerMemberName = "", 10: [CallerFilePath] string callerPath = "", 11: [CallerLineNumber] int callerLineNumber = 0) 12: { 13: Console.WriteLine("Invoked with: " + text); 14: Console.WriteLine("Caller {0} from File {1} (Ln: {2})", callerMemberName, callerPath, callerLineNumber); 15: } 16: }

If you take a look on the code with ILSpy you are going to find the right information’s:

1: internal class Program 2: { 3: private static void Main(string[] args) 4: { 5: Program.Log("Hello World...", "Main", "c:\\Users\\Robert\\Documents\\Visual Studio 2013\\Projects\\CallerInformationDemo\\CallerInformationDemo\\Program.cs", 14); 6: Console.ReadLine(); 7: } 8: public static void Log(string text, [CallerMemberName] string callerMemberName = "", [CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLineNumber = 0) 9: { 10: Console.WriteLine("Invoked with: " + text); 11: Console.WriteLine("Caller {0} from File {1} (Ln: {2})", callerMemberName, callerPath, callerLineNumber); 12: } 13: }


Take a look at the implementation of the INotifyPropertyChanged example.

Additional information’s are available in this blogpost.

Demo-Code on GitHub

You can find the demo application on GitHub.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by

Learn more about our team.

One Response

Comment on this post

Recent Posts

  • image1929-570x143_thumb.png
    Create and validate own Json-Web-Tokens (JWTs)

    If you are interested in web authentication you probably have heard about JSON Web tokens (JWT). What is a JWT? Maybe I’m not using the correct security termination but however: JWTs are used to exchange claims between two systems. For example: You want to log on to a service (like Facebook, Twitter, etc.) and want […]

  • Micro-Optimization: how to shrink or „embed“ pictures

      I’m currently working on the “CodeInside Dashboard” and since the page structure isn’t that difficult it should be possible to fulfill all of Google Pagespeed or Yahoos YSlow recommendations. One of the rules was to optimize the 4 PNGs that are embedded on the page.   Before – without optimization: Below you can see […]

  • image1979-570x194.png
    Move to Windows Azure – VMs, Word Press Migration, DNS changes

    Since mid January this blogs runs on a WordPress installation in an Azure VM. Because I always thought that the subject is quite complicated this blogpost offers a view behind the scenes. Why this move? So far this blog (both German and English Version) runs on a hoster somewhere in Germany. The main problem with this […]

  • Windows Azure Active Directory – CRUD for users and groups

      Windows Azure Active Directory? If you are not informed about the subject I recommend you to have a look on this Azure Info site. Which resources are there? The Azure AD contains the following entities: - Users - Groups - Contacts - Roles Access to the directory or on the “directory graph” Although the […]

  • Introduction into SignalR 2.0 & Azure Website Websockets

      SignalR is an Open Source Framework for Real Time WebApps. The main problem with Real-Time in the web is the canal between Browser and Server. If you never had to deal with SignalR and this problem before here is a brief introduction:   The problem Traditionally the browser initiates the request to the server […]

Support us