How do you get the name of a property using the property itself

Lykeuhfox

I've run into an issue where I need to get the name of a property for logging purposes. I'm sure there is a way to do this in VB.Net using some amalgam of reflection and lambda expressions, but I've been unsuccessful so far.

What I'm trying to do is convert this:

objAddress.AddressLine

to this:

"AddressLine"
Obsidian Phoenix

In the past, I've used a method I found online for doing this with INotifyPropertyChanged. I can't remember exactly where, but this details the same resolution:

http://paulstovell.com/blog/strong-property-names

C#

public class Test : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            RaisePropertyChanged(() => Name);
        }
    }

    private void RaisePropertyChanged(Expression<Func<object>> property)
    {
        MemberExpression exp = property.Body as MemberExpression;

        if (exp != null)
        {
                PropertyChanged(this, new PropertyChangedEventArgs(exp.Member.Name));
        }
    }


}

VB

Public Class Test
    Implements INotifyPropertyChanged

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    Private _Name As String

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(value As String)
            _Name = value
            RaisePropertyChanged(Function() Me.Name)
        End Set
    End Property

    Private Sub RaisePropertyChanged(Of T)(propertyName As Expression(Of Func(Of T)))
        Dim exp As MemberExpression = TryCast(propertyName.Body, MemberExpression)

        If exp IsNot Nothing Then
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(exp.Member.Name))
        End If
    End Sub



End Class

The main benefit of doing this is refactoring. If I ever rename my property, the Lambda (and by extension NotifyPropertyChanged event) changes automatically.


Update (2015)

It may be worth mentioning that the new features in Visual Studio 2015 make this even easier. Below is the same code shown above, but using the new nameof feature (Details of this, and of other new features can be found Here).

C#

public class Test : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    string _Name;

    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            RaisePropertyChanged(nameof(Name));
        }
    }

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

VB

Public Class Test
    Implements INotifyPropertyChanged

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    Private _Name As String

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(value As String)
            _Name = value
            RaisePropertyChanged(NameOf(Name))
        End Set
    End Property

    Private Sub RaisePropertyChanged(propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

End Class

You can even use nameof on the subscriber side, in order to determine if the property is the one you care about:

    private static void PropChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(Test.Name))
        {
            Console.WriteLine("The Property I care about changed");
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do you get the name of a property using the property itself

From Dev

How do you get the default binding mode of a dependency property?

From Dev

How do you get the Value of a property from PropertyInfo?

From Dev

Golang - How do you decode json array and get the root property

From Dev

How to get the property name as a string?

From Dev

How do I retrieve an attribute's name using the nodeName property?

From Dev

How to get the name of a property sent as a parameter using a lambda

From Dev

How to get a property name and its value using Swift 2.0, and reflection?

From Dev

How to get the name of a property sent as a parameter using a lambda

From Dev

How to get a name of a css property using selenium-python?

From Dev

How to get property name desending order in every record using Linq

From Dev

How do I write a method that fills a property using the property's name?

From Dev

How to get data from this objects num property using the name property as a selector

From Dev

How do you have an attribute called "property" and use the @property decorator?

From Dev

How do you search object for a property within property?

From Dev

Using lambda expression to get property OR type name

From Dev

How do you validate if a property is accessible in php?

From Dev

how do you implement classes with property subsets?

From Dev

How do you create a JSON property in TMongoWire

From Dev

How do you check if a property is undefined in qml?

From Dev

How do you replace an Object property in Javascript?

From Dev

how do you filter over property of object

From Dev

How do I get the type of a property of a class in Dart using mirrors?

From Dev

How do I get the type of a property of a class in Dart using mirrors?

From Dev

How to get an object property value when property name is in a variable?

From Dev

Scala how to get object property value given name of property in string?

From Dev

How do you get the name of the program using argparse?

From Dev

Can you get the property name through which a function was called?

From Dev

Can you get the property name through which a function was called?

Related Related

  1. 1

    How do you get the name of a property using the property itself

  2. 2

    How do you get the default binding mode of a dependency property?

  3. 3

    How do you get the Value of a property from PropertyInfo?

  4. 4

    Golang - How do you decode json array and get the root property

  5. 5

    How to get the property name as a string?

  6. 6

    How do I retrieve an attribute's name using the nodeName property?

  7. 7

    How to get the name of a property sent as a parameter using a lambda

  8. 8

    How to get a property name and its value using Swift 2.0, and reflection?

  9. 9

    How to get the name of a property sent as a parameter using a lambda

  10. 10

    How to get a name of a css property using selenium-python?

  11. 11

    How to get property name desending order in every record using Linq

  12. 12

    How do I write a method that fills a property using the property's name?

  13. 13

    How to get data from this objects num property using the name property as a selector

  14. 14

    How do you have an attribute called "property" and use the @property decorator?

  15. 15

    How do you search object for a property within property?

  16. 16

    Using lambda expression to get property OR type name

  17. 17

    How do you validate if a property is accessible in php?

  18. 18

    how do you implement classes with property subsets?

  19. 19

    How do you create a JSON property in TMongoWire

  20. 20

    How do you check if a property is undefined in qml?

  21. 21

    How do you replace an Object property in Javascript?

  22. 22

    how do you filter over property of object

  23. 23

    How do I get the type of a property of a class in Dart using mirrors?

  24. 24

    How do I get the type of a property of a class in Dart using mirrors?

  25. 25

    How to get an object property value when property name is in a variable?

  26. 26

    Scala how to get object property value given name of property in string?

  27. 27

    How do you get the name of the program using argparse?

  28. 28

    Can you get the property name through which a function was called?

  29. 29

    Can you get the property name through which a function was called?

HotTag

Archive