23

I am trying to use CallerMemberName attribute in .NET 4.0 via BCL portability pack. It is always returning an empty string instead of the member name. What am I doing wrong?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        MessageBox.Show(new class2().CallMe);
    }
}

public class class2
{
    public string CallMe 
    {
        get
        {
            return HelpMe();
        }
    }

    private string HelpMe([CallerMemberName] string param = "")
    {
        return param;
    }
}
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Pradeep
  • 433
  • 1
  • 3
  • 10
  • Which version of the compiler are you using? This feature is implemented by the compiler not the runtime. – Mike Zboray Sep 17 '13 at 04:18
  • If this helps :C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>csc /? Microsoft (R) Visual C# Compiler version 4.0.30319.17929 – Pradeep Sep 17 '13 at 04:51
  • If the compiler does not support it it would cause a compiler error not an empty message – Samuel Sep 17 '13 at 05:49
  • Anyway [CallerMemberName] was introduced in 4.5 so you won't be able to use it in 4.0. Tested with VS2012 targeting 4.0 with C# 5.0 – Samuel Sep 17 '13 at 05:54
  • 6
    Samuel - That is the exact purpose of Microsoft BCL Portability. To be able to use selected 4.5 features in 4.0. http://www.nuget.org/packages/Microsoft.Bcl/ – Pradeep Oct 09 '13 at 22:54

3 Answers3

33

Targeting 4.0 works just fine if you add:

namespace System.Runtime.CompilerServices {
    sealed class CallerMemberNameAttribute : Attribute { }
}
user3734274
  • 351
  • 1
  • 3
  • 5
  • 6
    https://www.thomaslevesque.com/2012/06/13/using-c-5-caller-info-attributes-when-targeting-earlier-versions-of-the-net-framework – A Khudairy Jul 24 '17 at 09:11
16

I found the solution, though it's not useful to me. You need to install KB2468871 on top of .NET Framework 4 to be able to use caller info attributes. Unfortunately, I can't ask each developer to remember to install it when they setup development environment.

Palec
  • 12,743
  • 8
  • 69
  • 138
Pradeep
  • 433
  • 1
  • 3
  • 10
  • 1
    It seems you can fake it in your own source tree instead of requiring each each developer to install that (although there might issues to address if some had installed it). Please see [this](http://stackoverflow.com/questions/13381917/is-the-callermembername-attribute-in-4-5-able-to-be-faked) and [this](http://www.thomaslevesque.com/2012/06/13/using-c-5-caller-info-attributes-when-targeting-earlier-versions-of-the-net-framework/) – Giles Jan 30 '14 at 17:47
  • 2
    there is easier way. check anser from user373274 ... or this https://www.thomaslevesque.com/2012/06/13/using-c-5-caller-info-attributes-when-targeting-earlier-versions-of-the-net-framework – A Khudairy Jul 24 '17 at 09:11
8

As I know, CallerMemberName is supported from .Net 4.5 You should not use it in .Net 4.0

Someone implemented this in .Net 4.0 using StackTrace. for example: http://www.journeyintocode.com/2013/04/callermembername-net-40.html

BUT, I do NOT recommend you to use the StackTrace since there could be a performance hit. Using StackTrace to get the caller name is very very slow. And this works in Debug, in release you cannot be sure whether StackTrace is "correct" or not.

So, my suggestion is: Just use CallerMemberName in .Net 4.5 or later version. In the early version of .Net, there isn't any foolproof or fast way of doing this.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Yaping Xin
  • 117
  • 1
  • 1
  • 4
    https://www.thomaslevesque.com/2012/06/13/using-c-5-caller-info-attributes-when-targeting-earlier-versions-of-the-net-framework/ – A Khudairy Jul 24 '17 at 09:13