Getting the name of the method or property in C# code
Tags: programming, picture, c, net
Published: 2020-02-16 21:05:31

---
title: Getting the name of the method or property in C# code
published: true
date: 2020-02-16 18:56:50 UTC
tags: programming,picture,c#,.net
canonical_url: https://siderite.dev/blog/getting-name-of-method-or-property-in-c-sharp/
---

![Know all the details about your caller](https://siderite.dev/Posts/files/WhosCalling_637174772810870188.png)  This is something that appeared in C# 5, so a long time ago, with .NET 4.5, but I only found out about it recently. Remember when you wanted to know the name of a property when doing INotifyPropertyChanged? Or when you wanted to log the name of the method that was calling? Or you wanted to know which line in which source file is responsible for calling a certain piece of code? All of this can be done with the [Caller Information](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/caller-information "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.") feature.

  And it is easy enough to use, just decorate a method parameter with an explicit default value with any of these three attributes:

- [CallerMemberNameAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callermembernameattribute "Allows you to obtain the method or property name of the caller to the method.")
- [CallerFilePathAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerfilepathattribute "Allows you to obtain the full path of the source file that contains the caller. This is the file path at the time of compile.")
- [CallerLineNumberAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callerlinenumberattribute "llows you to obtain the line number in the source file at which the method is called.")

The parameter value, if not set when calling the method, will be filled in with the member name or file name or line number. It's something that the compiler does, so no overhead from reflection. Even better, it works on the caller of the method, not the interior of the method. Imagine you had to write a piece of code to do the same. How would you reference the name of the method calling the method you are in?

Example from Microsoft's site:

```
public void DoProcessing()
{
    TraceMessage("Something happened.");
}

public void TraceMessage(string message,
        [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
        [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
        [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
    System.Diagnostics.Trace.WriteLine("message: " + message);
    System.Diagnostics.Trace.WriteLine("member name: " + memberName);
    System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
    System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}

// Sample Output:
// message: Something happened.
// member name: DoProcessing
// source file path: c:\Visual Studio Projects\CallerInfoCS\CallerInfoCS\Form1.cs
// source line number: 31
```