Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Intercepting property changes in subclasses

I'm in the process of creating a framework in which I provide the base class and the implementers of the framework will inherit from the base class and provide additional properties and methods. In the base class, I would like to have a way of observing when a property value is changed. The property can be from the base class or in any of the subclasses. I know that through reflection, I can determine the list of properties from any instance, but is there a way I can track the property changing value?

Here is a very simplistic example of what I am saying:

public class BaseClass
{
    public string BaseClassProperty { get; set; }

    public void DoSomethingWhenEitherPropertyGetsChanged()
    {

    }
}

public class SubClass : BaseClass
{
    public string SubClassProperty { get; set; }
}

What can I do to have DoSomethingWhenEitherPropertyGetsChanged get executed when either of the properties has it's value changed.

like image 823
Mark S. Avatar asked May 21 '11 00:05

Mark S.


1 Answers

You can use notifypropertyweaver for this purpose. It does exactly what you want. Here's a link:

  • notifypropertyweaver

From the open source home page:

Uses IL weaving (via http://www.mono-project.com/Cecil) to inject INotifyPropertyChanged code into properties.

  • No attributes required
  • No references required
  • No base class required
  • Supports .net 3.5, .net 4, Silverlight 3, Silverlight 4, Silverlight 5 and Windows Phone 7
  • Supports client profile mode
like image 54
Rick Sladkey Avatar answered Sep 24 '22 08:09

Rick Sladkey