Project Description
Send property change notifications without specifying property name as a string. Instead, write properties like this

public int Foo
{
get { return _foo; }
set { SetValue(ref _foo, value); } <-- no property name here
}

Note that there is no need to include the name of the property as a string. ActiveSharp reliably and correctly figures that out for itself. You can easily use it to automate the implementation of INotifyPropertyChanged.

Also useful in OR Mapping and other frameworks where you need to reliably detect changes in property values for other reasons. It does so without resorting to runtime-sublassing or other traditional AOP techniques.

Effectively ActiveSharp amounts to a brand-new technique for interception/AOP (for properties only).

It works by one-off runtime inspection of the MSIL in property setter methods, and a special routine to identify a field simply by passing it as a 'by ref' parameter.

(This apporach is much faster and more reliable than inspecting call stacks, which is a commonly-quoted, but flawed, alternative).

It is implemented as a number of independent classes which you can mix and match for your own purposes (e.g. you could use the MSIL inspection engine for your own purposes, quite separately from the property change stuff. Or you could detect property changes for your own purposes, without necessarily raising change notification events).

Requires .NET 2.0 or later.

Implementing INotifyPropertyChanged

Option 1: The easiest way is to use ActiveSharp's PropertyChangeHelper:

Add this code to your class or (preferrably) to a base class:

    readonly PropertyChangeHelper _propertyChangeHelper = new PropertyChangeHelper();

    public event PropertyChangedEventHandler PropertyChanged
    {
        add { _propertyChangeHelper.Add(value); }
        remove { _propertyChangeHelper.Remove(value); }
    }
            
    protected void SetValue<T>(ref T field, T value)
    {
        _propertyChangeHelper.SetValue(this, ref field, value);
    }

Also, add INotifyPropertyChanged to your base class' list of ancestors. Now, you have a base class that provides all the support for derived classes to sent property changed notifications really easily.

In your derived classes, simply implement your properties like this:

    public int Foo
    {
        get { return _foo; }
        set { SetValue(ref _foo, value); }   // assigns value and does prop change notification, all in one line
    }

Note: a reader kindly wrote in with this translation of the code to VB.NET.

Option 2:

If you want more control, you can write your own SetValue method, instead of delegating to the helper class. Someting like this:

    protected void SetValue<T>(ref T field, T value)
    {
        ...
        field = value;   //Actually assign the new value
        PropertyInfo changedProperty = 
               ActiveSharp.PropertyMapping.PropertyMap.GetProperty(this, ref field);
        // do whatever you like, now that you know which prop was changed
        ...
    }

As you can see, all the "magic" is in ActiveSharp.PropertyMapping.PropertyMap.GetProperty. Given an object, and a field in that object, passed "by ref", the routine finds the property that sets the field. It returns a standard .NET PropertyInfo object. Just read its Name property if all you want is the property name.

For more details see:

A page on: How it works

Dicsussion thread here for discussion of other things you can do with ActiveSharp, and how it compares with the alternatives.

Blog posts here and here (including the comments)
Last edited Dec 23 2008 at 6:35 AM by johnrusk, version 15

 

Want to leave feedback?
Please use Discussions or Reviews instead.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Version 2009.6.1.15196