CallerMemberName in .NET 4.0 not working

后端 未结 3 2065
悲哀的现实
悲哀的现实 2021-02-05 06:17

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 doi

3条回答
  •  攒了一身酷
    2021-02-05 06:51

    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.

提交回复
热议问题