1

I have a question about the observation of properties of an object.

For example, I have a class like this:

public class MyProperties
{
  public string Prop1 { get; set; }
  public int Prop2 { get; set; } 
}

My wish is to get a notification if one of the properties of MyProperties are changed. My vision is something like this:

//I have an instance
MyProperties Prop = new Myproperties();
//And add a listener to the properties
Prop.Prop1.AddNotification(APropertyChanged);
Prop.Prop2.AddNotification(APropertyChanged);

//I have a eventhandler in my code
public void (object Sender, EventArgs e)
{
  //Sender is the Property
}

I don't want to change any code in MyProperties. Is there any way to achive something like this .. or is this idea totally idiotic?

Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49

4 Answers4

1

If your class MyProperties implements the INotifyPropertyChanged interface, you can raise the PropertyChanged event. Otherwise you will have to look at some frameworks (i guess aspect oriented programing framework can help).

romain-aga
  • 1,441
  • 9
  • 14
0

you need to implement INotifyPropertyChanged

have a look at https://msdn.microsoft.com/fr-fr/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

Eidivandi
  • 1
  • 1
  • Thank you for your answer. I am not able to change the code of my class. So I don#t can implement the INotifyPropertyChanged. Ist there an other way? – Fruchtzwerg May 22 '16 at 17:56
  • the most simple way is to create an other class that derive from your Class MyProperty and implement INotifyPropertyChanged on it – Eidivandi May 29 '16 at 13:13
0

You can implement INotifyPropertyChanged if you want to,

Its commonly used in WPF MVVM.

(You can just write an event handler yourself)

Anyway.. I would do something like this:

private string _prop1;
private int _prop2;

public string Prop1
{
    get
    {
        return _prop1;
    }
    set
    {
        _prop1 = value;
        RiseEvent(nameof(Prop1),Prop1);
    }
}

public int Prop2
{
    get
    {
        return _prop2;
    }
    set
    {
        _prop2 = value;
        RiseEvent(nameof(Prop2),Prop2);
    }
}

protected void RiseEvent(string propertyName, object propertyValue)
{
    //---Code that uses the propertyName and propertyValue and rises an event as your choice
}

The nameof() is new syntax in C# 6 (supported in VS2015) If you are using an older version you can do that in other way (comment me and i'll share that)

orlevii
  • 427
  • 4
  • 10
  • Thank you for your answer. I am not able to change the code of my class. So I don#t can implement the INotifyPropertyChanged. Ist there an other way? – Fruchtzwerg May 22 '16 at 17:56
  • You need a class proxy, If your class has an interface of the properties you can do it dynamically with RealProxy class in .Net (Found already someone talking about it: http://stackoverflow.com/questions/15733900/dynamically-creating-a-proxy-class) Otherwise, you can just wrap your class yourself – orlevii May 22 '16 at 18:12
0

Here is my solution. Thanks to the tip of @romain-aga.

Fody PropertyChanged allows to change the code of properties during compilation. If I have the class:

public class MyProperties
{
    public string Prop1 { get; set; }
    ...
}

Fody would compile this code:

public class MyProperties
{
    string prop1;
    public string Prop1
    {
        get { return prop1; }
        set
        {
            if (value != prop1)
            {
                prop1 = value;
                OnPropertyChanged("Prop1");
            }
        }
    }
    ...
}

Via reflection, it is possible to add a method to the generated PropertyChanged event:

EventInfo Event = typeof(MyProperties).GetEvent("PropertyChanged");
MethodInfo Method = //The methode of the handler you want
Delegate Handler = Delegate.CreateDelegate(Event.EventHandlerType, this, Method);
Event.AddEventHandler(IntanceOfMyProperties, Handler);
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49