There is a class consisting of a set of view properties.

public SomeType Property1 { get; set; } 

I can not edit this class, but I really want to follow the changes in its properties. How do I do this in real time?

  • "I can not edit this class" - it is possible to edit the class method table in runtime. So do some unit testing frameworks. I think you can try to use them, or implement it yourself. - Qwertiy
  • one
    Pattern "decorator" to help you. - ixSci
  • @ixSci, and if without a wrapper over a specific class? I would like a generic generic solution - iRumba
  • one
    Well, this is a universal solution, for working with old code that cannot be changed. Wrappers are made, everything is translated into interfaces, etc. - This is a normal practice. - ixSci

2 answers 2

Inherit your class from it and either override the property, or if it is sealed, create your property.

 class myClass : ParentClass { public SomeType myProperty1 { get{return base.Property1;} set{ base.Property1=value; onmyProperty1Changed(); } } public event EventHandler myProperty1Changed; public void onmyProperty1Changed() { if(myProperty1Changed!= null) myProperty1Changed(this,null); } } 
  • Too hard. I have a copy of the class, I will need to convert it to a new type .... and the type itself is completely duplicated ... - iRumba

You can like that

 TypeDescriptor.GetProperties(typeof(SomeClass))[nameof(theSomeClass.Prop1)].AddValueChanged(theSomeClass, eventHandler); 

In short, we obtain a PropertyDescriptionCollection for the type SomeClass , select its property Prop1, and add a handler to change it, specifying the instance of the desired class as the first parameter.

There is only one problem, I have not yet figured out how to use one handler for all properties, because EventArgs has no parameters for the event.