Search code examples
c#propertyinfopropertychangedcallermembername

Is it possible, to evaluate the event of calling a set property?


Let's say, we have an ordinary C# class with one auto get/set property.

public class Entity
{
       public String SomeProperty {get;set;}

}

Is there any event, that is raised and that I can evaluate, when the set method of the SomeProperty is called?

Is something like this possible in any way, maybe reflection?:

Pseudo Code, NO REAL CODE:

Entity e = new Entity();
e.SomeProperty.SetterCalled += OnSetterCalled;

private void OnSetterCalled(Sender propertyinfo)
{
    propertyinfo pi = propertyinfo;
    Console.Write (pi.Name);
}

I know, I could use CallerMember, but then I had to change the auto property.


Solution

  • I would recommend looking into:

    INotifyPropertyChanged
    

    Here is a great walkthrough:

    Implementing INotifyPropertyChanged - does a better way exist?