Questions tagged [callermembername]

`CallerMemberName` provides useful information about the caller within the called method.

Caller information attributes like CallerMemberName (from the System.Runtime.CompilerServices namespace) provide information about the caller within the called method, e.g.:

public void Log(string message, [CallerMemberName] string memberName = "")
{
    Trace.WriteLine("Log has been called by {0}", memberName);
}

Other caller information attributes are CallerFilePath and CallerLineNumber. More information about that topic can be found at http://msdn.microsoft.com/en-us/library/hh534540.aspx

27 questions
114
votes
1 answer

Is [CallerMemberName] slow compared to alternatives when implementing INotifyPropertyChanged?

There are good articles that suggest different ways for implementing INotifyPropertyChanged. Consider the following basic implementation: class BasicClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; …
JYL
  • 8,228
  • 5
  • 39
  • 63
24
votes
2 answers

Is there any benefit of using the nameof operator instead of the CallerMemberNameAttribute to notify property changes in .NET 4.5.3?

With the advent of .NET 4.5.3, WPF developers now have three (or more) ways to notify the INotifyPropertyChanged Interface of property changes. Basically, my question is Which of the two methods introduced from.NET 4.5 onwards is the more efficient…
Sheridan
  • 68,826
  • 24
  • 143
  • 183
23
votes
3 answers

CallerMemberName in .NET 4.0 not working

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() { …
Pradeep
  • 433
  • 1
  • 3
  • 10
18
votes
5 answers

Mixing optional parameters and params when can't simply overload

Similar to this question, I want to mix optional parameters with the params keyword, which of course creates ambiguity. Unfortunately, the answer of creating overloads does not work, as I want to take advantage of caller info attributes, like…
Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
13
votes
4 answers

Why do the C# Caller Info Attributes need a default value?

I just came across the C# 5 Caller Info Attributes (http://msdn.microsoft.com/en-us/library/hh534540.aspx). This seems like a very useful feature, and I've read up some documentation…
Omaer
  • 817
  • 1
  • 7
  • 22
13
votes
3 answers

Suppress "Member is never assigned to" warning in C#

I have the following code: ViewPortViewModel _Trochoid; public ViewPortViewModel Trochoid { get { return _Trochoid; } set { this.RaiseAndSetIfChanged(value); } } using ReactiveUI INPC support. The compiler is always warning me that…
11
votes
4 answers

How to use params keyword along with caller Information in C#?

I am trying to combine the C# 5.0 Caller Information along with the C# params keyword. The intention is to create a wrapper for a logging framework, and we want the logger to format the text like String.Format. In previous versions, the method…
Moby Disk
  • 3,761
  • 1
  • 19
  • 38
5
votes
2 answers

WPF Notify PropertyChanged for a Get Property

I have the INotifyPropertyChanged implemented using CallerMemberName public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { if (PropertyChanged != null) …
Carbine
  • 7,849
  • 4
  • 30
  • 54
4
votes
1 answer

CallerMemberName doesn't work for Attribute constructor on a field

I'm producing a serializer in C# (.NET 4.5, VS 2013), and I'm using an attribute to control serialization metadata, such as the name to store a member under for reading and writing. Since I don't want to write out the member name as an argument for…
Isaac van Bakel
  • 1,772
  • 10
  • 22
3
votes
1 answer

Caller Information in Java

In C# we have Caller Information public void DoProcessing() { TraceMessage("Something happened."); } public void TraceMessage(string message, [System.Runtime.CompilerServices.CallerMemberName] string memberName = "", …
HamedFathi
  • 3,667
  • 5
  • 32
  • 72
2
votes
1 answer

How to use InterpolatedStringHandlerArgumentin conjunction with CallerMemberName, CallerFilePath, and CallerLineNumber

Good day! I'm experimenting with some logging features, in particular adding caller information for logging using interpolated strings. This is in Visual Studio 2022 using C# & .NET 6.0. I'm trying to add the caller information (name, path, and line…
2
votes
2 answers

CallerMemberName is empty if used from base class constructor and not initialized

I have two classes like this: public class test1: BaseClass { public test1() : base() { } ... public class BaseClass { public BaseClass( [CallerMemberName]string membername ="", [CallerFilePath] string path = "") …
TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62
2
votes
1 answer

Get caller info for injected service in .net core

I'm trying to figure out which CallerMemberName or CallerFilePath value will be used if I'll apply these attributes to some service and then inject it into DI. For example: public class MyService: IMyService { public MyService([CallerMemberName]…
anatol
  • 1,680
  • 2
  • 24
  • 47
2
votes
2 answers

Should I provide a method with a member name with nameof, or should I depend on CallerMemberName to do it for me?

How is CallerMemberName implemented? I get what it does - it allows us to keep magic strings out of our code - but should it be used over nameof and what is more performant? Whats the difference/how does CallerMemberName exactly work?
Mafii
  • 7,227
  • 1
  • 35
  • 55
2
votes
1 answer

C++/CLI: How do I use the CallerMemberNameAttribute in C++/CLI?

In C# and VB.Net I can use the CallerMemberNameAttribute to get the name of the invoker as a string: public void Caller([CallerMemberName]string memberName = "") { Debug.Print(memberName); } I would like to do the same in C++/CLI but somehow I…
cklam
  • 156
  • 5
1
2