Today in this post we will learn about another attribute named “CallerFilePath” which has been newly included in C# 5. Tune in to know about it with a simple example to trace the development issues that one source file can generate.
What is CallerFilePath in C# 5.0?
[CallerFilePath] is an attribute introduced in C# 5.0, which allows you to obtain the full path of the source file that contains the caller. You can find this attribute named “CallerLineNumberAttribute” under the System.Runtime.CompilerServices namespace.
Remember that, this returns the file path stored at the time of compile. If you move the final output file to a different location, it will still show the file path of compilation time. So it means that, the file path depends on the build machine and to trace proper file path, you have to find out the relative path stored in your source control.
Also remember that, you can apply the [CallerFilePath] attribute to an optional parameter that has a default string value. You must specify an explicit default value for the optional parameter. You can't apply this attribute to parameters that aren't specified as optional.
To know more about C# Named and Optional Parameters, read this post: “C# 4.0 New Features – Named and Optional Parameters”. Also, don’t forget to check out the InfoGraphic of C# Evolution.
Play with the Code
I hope you understood the basic concepts of [CallerFilePath] attribute from the above section which has been introduced in C# 5.0. Let’s see it in action by following a simple console application code. We will use the same application that we used in our previous two posts “What’s New in C# 5.0 - Learn about CallerMemberName Attribute” and “What’s New in C# 5.0 - Learn about CallerLineNumber Attribute”. We will modify it accordingly as per our need.
For this post, we will modify the “Trace” class method named “Write()” by adding a new optional parameter “filePath” as string and mark it with the attribute [CallerFilePath]. We will also update the print command “Console.WriteLine()” to print out the file path formatted properly in the screen.
Here is the code snippet of the same, which you can understand very easily:
Nothing more with code. Now we need to run the console application to see the output of the above code. When you run it, you will see the following result where it will print out the file path of the source file from where we called it :
I hope that the functionality of the above attribute is clear to you now. You can now think of the use of this attribute in your code to trace the line number of the executed code especially the exceptions of the origins. If you have further question, just drop a line below and I will try to answer you as soon as possible.
Remember that, it will return you the source file path of compile time and if you move the output file to different location, it will still show you the compile time file path.
Last but not least, connect with me on Twitter and Facebook for technical updates and articles news. Also subscribe to my blog’s Newsletter to get all the updates delivered directly to your inbox. We won’t spam or share your email address as we respect your privacy.