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.

Example

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: }

INotifyPropertyChanged

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.

Recent Posts

  • Time-controlled Azure WebJobs – Azure as easy as it get‘s

        While still in development the Azure WebJob SDK offers some cool features for procession and supply of information. A good example is the sample that observes the Azure Queue and processes an item as soon as it spots one.   Scenario: time-controlled activities – without queue and so on My scenario was quite […]

  • image2021-570x194_thumb.png
    HowTo: Create RSS Feeds with ASP.NET MVC

    I’ve already written about how to consume RSS or Atom Feeds with the SyndicationFeed (in German, sry) but todays post is about how to create or publish an own feed. Beside the age of the feed standard and the much defamation from Facebook, Twitter and Google on RSS/Atom I consider this easy to consume API […]

  • image1997-311x194_thumb.png
    How to access an Azure Website with the local IIS Manager

    Since the end of February it is possible to access an Azure Website with the IIS Manager. Although the Azure Management site offers some information there are more details visible at the IIS Manager. For the connection you will need an IIS Manager and the IIS Manager for Remote Administration Extension. It’s also possible to […]

  • 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 […]

Support us