none
How to get the name of a property from within the property without using Lambda? RRS feed

  • Question

  • I'm trying to come up with a pattern that can be used to get the name of a property from within the property's setter so that the same line of code can be used to record new values being asserted to properties in a program.

    Here's an example of how it would be used. In the following code snippet there is a property named Message. Because this is a Prism application SetProperty() is used to transfer a value to a backing member. If the new value is different from the old value then a true is returned and in such a case the code calls a method that will record the new value to a log ile. The ClassName things comes from another property which uses reflection to acquire the class name. The property is, obviously, 'Message' and that's the bit that I want to change. As it stands, everytime that the WriteToLog line is added to a property's setter it has to be modified. I don't want to modify it. I want it to figure out what the name of the property is on its own. I can do this with a lambda expression but if I do that then that can break edit and continue.

    public string Message
    {
        get { return _message; }
        set
        {
            if (SetProperty(ref _message, value))
                WriteToLog.WriteLine($"{ClassName}.Message: {0}",
                    value);
        }
    }
    private string _message;
    
    So, any ideas?


    Richard Lewis Haggard

    Wednesday, September 18, 2019 9:53 PM

Answers

  • Hi Richard.Haggard,

    Thank you for posting here.

    If you want to get the name of a property in property without suing lambda, you could try the following code to do it.

      public string Age
            {
                get
                {
                    return age;
                }
                set
                {
                    var propertyName = MethodBase.GetCurrentMethod().Name.Substring(4);
                    Console.WriteLine(propertyName);
                    value = propertyName;
                }
            
            
            }
            public string age;

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 19, 2019 2:37 AM
    Moderator

All replies

  • It's hard to excited about a little inconvenience for one's debug methods, but this should work:

                WriteToLog.WriteLine( "{0}.{1} {2}",
                    this.GetType().FullName,
                    System.Reflection.MethodBase.GetCurrentMethod(),
                    value
                );
    

    It generated the property name as "set_Message", because that's what's actually generated.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, September 19, 2019 12:26 AM
  • Hi Richard.Haggard,

    Thank you for posting here.

    If you want to get the name of a property in property without suing lambda, you could try the following code to do it.

      public string Age
            {
                get
                {
                    return age;
                }
                set
                {
                    var propertyName = MethodBase.GetCurrentMethod().Name.Substring(4);
                    Console.WriteLine(propertyName);
                    value = propertyName;
                }
            
            
            }
            public string age;

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 19, 2019 2:37 AM
    Moderator
  • Isn't this sort of thing the reason "nameof" was added to the language?

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    • Proposed as answer by EckiS Thursday, September 19, 2019 5:10 PM
    Thursday, September 19, 2019 2:24 PM
  • Try this too:

       if( SetProperty( ref _message, value )) Log( value );

     

    where Log is:

       void Log(object value, [CallerMemberName] string caller = null)

       {

          WriteToLog.WriteLine($"{ClassName}.{caller}: {value}");

       }

    Thursday, September 19, 2019 3:01 PM
  • No, it's not, and whoever proposed this as an answer should probably undo it.  "nameof" tells you the basic name of an object.  What object are you going to pass?  There's no simple object that represents "the current function".  GetCurrentMethod() returns a string that has no name.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, September 19, 2019 5:56 PM