<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="http://www.codeplex.com/rss.xsl"?><rss version="2.0"><channel><title>ActiveSharp Wiki &amp; Documentation Rss Feed</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Home</link><description>ActiveSharp Wiki Rss Description</description><item><title>Updated Wiki: Using ActiveSharp from VB</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Using ActiveSharp from VB&amp;version=3</link><description>&lt;div class="wikidoc"&gt;
This is a VB.NET translation of using ActiveSharp's PropertyChangeHelper from VB.NET.  It was kindly sent in by Andrew Backer.&lt;br /&gt; &lt;br /&gt;&lt;div style="color:#000000;background-color:#FFFFFF;"&gt;&lt;pre&gt;
    &lt;span style="color:#0000FF;"&gt;Private&lt;/span&gt; _propertyChangeHelper &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;New&lt;/span&gt; PropertyChangeHelper
    &lt;span style="color:#0000FF;"&gt;Public&lt;/span&gt; Custom &lt;span style="color:#0000FF;"&gt;Event&lt;/span&gt; PropertyChanged &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler _
        &lt;span style="color:#0000FF;"&gt;Implements&lt;/span&gt; INotifyPropertyChanged.PropertyChanged
        &lt;span style="color:#0000FF;"&gt;AddHandler&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler)
            _propertyChangeHelper.Add(value)
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;AddHandler&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;RemoveHandler&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler)
            _propertyChangeHelper.Remove(value)
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;RemoveHandler&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;RaiseEvent&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Object&lt;/span&gt;, &lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventArgs)
             &lt;span style="color:#008000;"&gt;'-- empty! (this is the key to doing it VB - you must write RaiseEvent, but since the helper raises&lt;/span&gt;
             &lt;span style="color:#008000;"&gt;' the event, you don't have to do anything here yourself)&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;RaiseEvent&lt;/span&gt;
    &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Event&lt;/span&gt;
    &lt;span style="color:#0000FF;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Sub&lt;/span&gt; SetValue(Of t)(&lt;span style="color:#0000FF;"&gt;ByRef&lt;/span&gt; field &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; t, &lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; t)
        _propertyChangeHelper.SetValue(&lt;span style="color:#0000FF;"&gt;Me&lt;/span&gt;, field, value)
    &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Sub&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Wed, 24 Dec 2008 04:28:51 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Using ActiveSharp from VB 20081224042851A</guid></item><item><title>Updated Wiki: Using ActiveSharp from VB</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Using ActiveSharp from VB&amp;version=2</link><description>&lt;div class="wikidoc"&gt;
This is a VB.NET translation of using ActiveSharp's PropertyChangeHelper from VB.NET.  It was kindly sent in by an ActiveSharp user.&lt;br /&gt; &lt;br /&gt;&lt;div style="color:#000000;background-color:#FFFFFF;"&gt;&lt;pre&gt;
    &lt;span style="color:#0000FF;"&gt;Private&lt;/span&gt; _propertyChangeHelper &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;New&lt;/span&gt; PropertyChangeHelper
    &lt;span style="color:#0000FF;"&gt;Public&lt;/span&gt; Custom &lt;span style="color:#0000FF;"&gt;Event&lt;/span&gt; PropertyChanged &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler _
        &lt;span style="color:#0000FF;"&gt;Implements&lt;/span&gt; INotifyPropertyChanged.PropertyChanged
        &lt;span style="color:#0000FF;"&gt;AddHandler&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler)
            _propertyChangeHelper.Add(value)
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;AddHandler&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;RemoveHandler&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler)
            _propertyChangeHelper.Remove(value)
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;RemoveHandler&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;RaiseEvent&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Object&lt;/span&gt;, &lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventArgs)
             &lt;span style="color:#008000;"&gt;'-- empty! (this is the key to doing it VB - you must write RaiseEvent, but since the helper raises&lt;/span&gt;
             &lt;span style="color:#008000;"&gt;' the event, you don't have to do anything here yourself)&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;RaiseEvent&lt;/span&gt;
    &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Event&lt;/span&gt;
    &lt;span style="color:#0000FF;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Sub&lt;/span&gt; SetValue(Of t)(&lt;span style="color:#0000FF;"&gt;ByRef&lt;/span&gt; field &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; t, &lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; t)
        _propertyChangeHelper.SetValue(&lt;span style="color:#0000FF;"&gt;Me&lt;/span&gt;, field, value)
    &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Sub&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Tue, 23 Dec 2008 07:37:35 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Using ActiveSharp from VB 20081223073735A</guid></item><item><title>Updated Wiki: Home</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Home&amp;version=15</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;Send property change notifications without specifying property name as a string.  Instead, write properties like this&lt;br /&gt;&lt;br /&gt;            public int Foo&lt;br /&gt;            &amp;#123;&lt;br /&gt;                get &amp;#123; return _foo&amp;#59; &amp;#125;&lt;br /&gt;                set &amp;#123; SetValue&amp;#40;ref _foo, value&amp;#41;&amp;#59; &amp;#125;  &amp;#60;-- no property name here&lt;br /&gt;            &amp;#125;&lt;br /&gt;&lt;br /&gt;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. &lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;Effectively ActiveSharp amounts to a brand-new technique for interception&amp;#47;AOP &amp;#40;for properties only&amp;#41;.   &lt;br /&gt;&lt;br /&gt;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 &amp;#39;by ref&amp;#39; parameter. &lt;br /&gt;&lt;br /&gt;&amp;#40;This apporach is much faster and more reliable than inspecting call stacks, which is a commonly-quoted, but flawed, alternative&amp;#41;.  &lt;br /&gt;&lt;br /&gt;It is implemented as a number of independent classes which you can mix and match for your own purposes &amp;#40;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&amp;#41;.&lt;br /&gt;&lt;br /&gt;Requires .NET 2.0 or later.&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Implementing INotifyPropertyChanged&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Option 1:&lt;/b&gt; The easiest way is to use ActiveSharp's PropertyChangeHelper:&lt;br /&gt; &lt;br /&gt;Add this code to your class or (preferrably) to a base class:&lt;br /&gt; &lt;br /&gt;&lt;div style="color:#000000;background-color:#FFFFFF;"&gt;&lt;pre&gt;
    &lt;span style="color:#0000FF;"&gt;readonly&lt;/span&gt; PropertyChangeHelper _propertyChangeHelper = &lt;span style="color:#0000FF;"&gt;new&lt;/span&gt; PropertyChangeHelper();
    &lt;span style="color:#0000FF;"&gt;public&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;event&lt;/span&gt; PropertyChangedEventHandler PropertyChanged
    {
        add { _propertyChangeHelper.Add(value); }
        remove { _propertyChangeHelper.Remove(value); }
    }
            
    &lt;span style="color:#0000FF;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;void&lt;/span&gt; SetValue&amp;lt;T&amp;gt;(&lt;span style="color:#0000FF;"&gt;ref&lt;/span&gt; T field, T value)
    {
        _propertyChangeHelper.SetValue(&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;, &lt;span style="color:#0000FF;"&gt;ref&lt;/span&gt; field, value);
    }
&lt;/pre&gt;&lt;/div&gt; &lt;br /&gt;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.  &lt;br /&gt; &lt;br /&gt;In your derived classes, simply implement your properties like this:&lt;br /&gt; &lt;br /&gt;&lt;div style="color:#000000;background-color:#FFFFFF;"&gt;&lt;pre&gt;
    &lt;span style="color:#0000FF;"&gt;public&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;int&lt;/span&gt; Foo
    {
        get { &lt;span style="color:#0000FF;"&gt;return&lt;/span&gt; _foo; }
        set { SetValue(&lt;span style="color:#0000FF;"&gt;ref&lt;/span&gt; _foo, value); }   &lt;span style="color:#008000;"&gt;// assigns value and does prop change notification, all in one line&lt;/span&gt;
    }
&lt;/pre&gt;&lt;/div&gt; &lt;br /&gt;Note: a reader kindly wrote in with &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Using%20ActiveSharp%20from%20VB&amp;amp;referringTitle=Home"&gt;this&lt;/a&gt; translation of the code to VB.NET.&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Option 2:&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;If you want more control, you can write your own SetValue method, instead of delegating to the helper class.  Someting like this:&lt;br /&gt; &lt;br /&gt;&lt;div style="color:#000000;background-color:#FFFFFF;"&gt;&lt;pre&gt;
    &lt;span style="color:#0000FF;"&gt;protected&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;void&lt;/span&gt; SetValue&amp;lt;T&amp;gt;(&lt;span style="color:#0000FF;"&gt;ref&lt;/span&gt; T field, T value)
    {
        ...
        field = value;   &lt;span style="color:#008000;"&gt;//Actually assign the new value&lt;/span&gt;
        PropertyInfo changedProperty = 
               ActiveSharp.PropertyMapping.PropertyMap.GetProperty(&lt;span style="color:#0000FF;"&gt;this&lt;/span&gt;, &lt;span style="color:#0000FF;"&gt;ref&lt;/span&gt; field);
        &lt;span style="color:#008000;"&gt;// do whatever you like, now that you know which prop was changed&lt;/span&gt;
        ...
    }
&lt;/pre&gt;&lt;/div&gt; &lt;br /&gt;As you can see, all the &amp;quot;magic&amp;quot; is in ActiveSharp.PropertyMapping.PropertyMap.GetProperty.  Given an object, and a field in that object, passed &amp;quot;by ref&amp;quot;, 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.&lt;br /&gt; &lt;br /&gt;For more details see:&lt;br /&gt; &lt;br /&gt;A page on: &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How%20it%20works&amp;amp;referringTitle=Home"&gt;How it works&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;Dicsussion thread &lt;a href="https://www.codeplex.com/Thread/View.aspx?ProjectName=ActiveSharp&amp;amp;ThreadId=22315" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; for discussion of other things you can do with ActiveSharp, and how it compares with the alternatives.&lt;br /&gt; &lt;br /&gt;Blog posts &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (including the comments) &lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Tue, 23 Dec 2008 07:35:33 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20081223073533A</guid></item><item><title>Updated Wiki: Using ActiveSharp from VB</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Using ActiveSharp from VB&amp;version=1</link><description>&lt;div class="wikidoc"&gt;
&lt;div style="color:#000000;background-color:#FFFFFF;"&gt;&lt;pre&gt;
    &lt;span style="color:#0000FF;"&gt;Private&lt;/span&gt; _propertyChangeHelper &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;New&lt;/span&gt; PropertyChangeHelper
    &lt;span style="color:#0000FF;"&gt;Public&lt;/span&gt; Custom &lt;span style="color:#0000FF;"&gt;Event&lt;/span&gt; PropertyChanged &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler _
        &lt;span style="color:#0000FF;"&gt;Implements&lt;/span&gt; INotifyPropertyChanged.PropertyChanged
        &lt;span style="color:#0000FF;"&gt;AddHandler&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler)
            _propertyChangeHelper.Add(value)
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;AddHandler&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;RemoveHandler&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventHandler)
            _propertyChangeHelper.Remove(value)
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;RemoveHandler&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;RaiseEvent&lt;/span&gt;(&lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Object&lt;/span&gt;, &lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; PropertyChangedEventArgs)
             &lt;span style="color:#008000;"&gt;'-- empty! (this is the key to doing it VB - you must write RaiseEvent, but since the helper raises&lt;/span&gt;
             &lt;span style="color:#008000;"&gt;' the event, you don't have to do anything here yourself)&lt;/span&gt;
        &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;RaiseEvent&lt;/span&gt;
    &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Event&lt;/span&gt;
    &lt;span style="color:#0000FF;"&gt;Public&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Sub&lt;/span&gt; SetValue(Of t)(&lt;span style="color:#0000FF;"&gt;ByRef&lt;/span&gt; field &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; t, &lt;span style="color:#0000FF;"&gt;ByVal&lt;/span&gt; value &lt;span style="color:#0000FF;"&gt;As&lt;/span&gt; t)
        _propertyChangeHelper.SetValue(&lt;span style="color:#0000FF;"&gt;Me&lt;/span&gt;, field, value)
    &lt;span style="color:#0000FF;"&gt;End&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;Sub&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Tue, 23 Dec 2008 07:34:22 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Using ActiveSharp from VB 20081223073422A</guid></item><item><title>Updated Wiki: Home</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Home&amp;version=14</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;Send property change notifications without specifying property name as a string.  Instead, write properties like this&lt;br /&gt;&lt;br /&gt;            public int Foo&lt;br /&gt;            &amp;#123;&lt;br /&gt;                get &amp;#123; return _foo&amp;#59; &amp;#125;&lt;br /&gt;                set &amp;#123; SetValue&amp;#40;ref _foo, value&amp;#41;&amp;#59; &amp;#125;  &amp;#60;-- no property name here&lt;br /&gt;            &amp;#125;&lt;br /&gt;&lt;br /&gt;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. &lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;Effectively ActiveSharp amounts to a brand-new technique for interception&amp;#47;AOP &amp;#40;for properties only&amp;#41;.   &lt;br /&gt;&lt;br /&gt;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 &amp;#39;by ref&amp;#39; parameter. &lt;br /&gt;&lt;br /&gt;&amp;#40;This apporach is much faster and more reliable than inspecting call stacks, which is a commonly-quoted, but flawed, alternative&amp;#41;.  &lt;br /&gt;&lt;br /&gt;It is implemented as a number of independent classes which you can mix and match for your own purposes &amp;#40;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&amp;#41;.&lt;br /&gt;&lt;br /&gt;Requires .NET 2.0 or later.&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Implementing INotifyPropertyChanged&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Option 1:&lt;/b&gt; The easiest way is to use ActiveSharp's PropertyChangeHelper:&lt;br /&gt; &lt;br /&gt;Add this code to your class or (preferrably) to a base class:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    readonly PropertyChangeHelper _propertyChangeHelper = new PropertyChangeHelper();
 
    public event PropertyChangedEventHandler PropertyChanged
    {
        add { _propertyChangeHelper.Add(value); }
        remove { _propertyChangeHelper.Remove(value); }
    }
            
    protected void SetValue&amp;lt;T&amp;gt;(ref T field, T value)
    {
        _propertyChangeHelper.SetValue(this, ref field, value);
    }
&lt;/pre&gt; &lt;br /&gt; &lt;br /&gt;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.  &lt;br /&gt; &lt;br /&gt;In your derived classes, simply implement your properties like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    public int Foo
    {
        get { return _foo; }
        set { SetValue(ref _foo, value); }   // assigns value and does prop change notification, all in one line
    }
&lt;/pre&gt; &lt;br /&gt; &lt;br /&gt;Note: a reader kindly wrote in with &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Using%20ActiveSharp%20from%20VB&amp;amp;referringTitle=Home"&gt;this&lt;/a&gt; translation of the code to VB.NET.&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Option 2:&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;If you want more control, you can write your own SetValue method, instead of delegating to the helper class.  Someting like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    protected void SetValue&amp;lt;T&amp;gt;(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
        ...
    }
&lt;/pre&gt; &lt;br /&gt; &lt;br /&gt;As you can see, all the &amp;quot;magic&amp;quot; is in ActiveSharp.PropertyMapping.PropertyMap.GetProperty.  Given an object, and a field in that object, passed &amp;quot;by ref&amp;quot;, 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.&lt;br /&gt; &lt;br /&gt;For more details see:&lt;br /&gt; &lt;br /&gt;A page on: &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How%20it%20works&amp;amp;referringTitle=Home"&gt;How it works&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;Dicsussion thread &lt;a href="https://www.codeplex.com/Thread/View.aspx?ProjectName=ActiveSharp&amp;amp;ThreadId=22315" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; for discussion of other things you can do with ActiveSharp, and how it compares with the alternatives.&lt;br /&gt; &lt;br /&gt;Blog posts &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (including the comments) &lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Tue, 23 Dec 2008 07:31:05 GMT</pubDate><guid isPermaLink="false">Updated Wiki: Home 20081223073105A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=11</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, at last, here's a very quick summary of how ActiveSharp works.&lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;(Note: the two hashtable lookups at the end may be optimized away in a &lt;a href="http://www.codeplex.com/ActiveSharp/WorkItem/View.aspx?WorkItemId=2227"&gt;future code change&lt;/a&gt;)&lt;br /&gt;&lt;h1&gt;
One-off setup
&lt;/h1&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt;&lt;h2&gt;
1. The offset calculation routine
&lt;/h2&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;My first version of this code&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; used C++ / CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C++ can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt;&lt;h2&gt;
2. The hash table that maps offsets to fields
&lt;/h2&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed. (By the way, writing the lazy initialization support was interesting, due to threadsafety issues.  I wrote the classes LazyDictionary and LazyDictionaryOfTypes, and made them re-usable for other cases that need lazy initialization e.g. you can use them outside ActiveSharp).&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://blogs.msdn.com/joelpob/archive/2004/03/31/105282.aspx" class="externalLink"&gt;Lightweight Code Gen&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt;&lt;h2&gt;
3. The hash table that maps fields to properties
&lt;/h2&gt;For this, we inspect the MSIL of the property setter methods, to see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;----&lt;br /&gt;Any questions - leave a comment or &lt;a href="http://www.agilekiwi.com/contact.htm" class="externalLink"&gt;email me&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 21:55:05 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524095505P</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=10</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, at last, here's a very quick summary of how ActiveSharp works.&lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;(Note: the two hashtable lookups at the end may be optimized away in a &lt;a href="http://www.codeplex.com/ActiveSharp/WorkItem/View.aspx?WorkItemId=2227"&gt;future code change&lt;/a&gt;)&lt;br /&gt;&lt;h1&gt;
One-off setup
&lt;/h1&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;My first version of this code&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; used C++ / CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C++ can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed. (By the way, writing the lazy initialization support was interesting, due to threadsafety issues.  I wrote the classes LazyDictionary and LazyDictionaryOfTypes, and made them re-usable for other cases that need lazy initialization e.g. you can use them outside ActiveSharp).&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://blogs.msdn.com/joelpob/archive/2004/03/31/105282.aspx" class="externalLink"&gt;Lightweight Code Gen&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt;For this, we inspect the MSIL of the property setter methods, to see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;----&lt;br /&gt;Any questions - leave a comment or &lt;a href="http://www.agilekiwi.com/contact.htm" class="externalLink"&gt;email me&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 21:52:36 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524095236P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Home&amp;version=13</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;Send property change notifications without specifying property name as a string.  Instead, write properties like this&lt;br /&gt;&lt;br /&gt;            public int Foo&lt;br /&gt;            &amp;#123;&lt;br /&gt;                get &amp;#123; return _foo&amp;#59; &amp;#125;&lt;br /&gt;                set &amp;#123; SetValue&amp;#40;ref _foo, value&amp;#41;&amp;#59; &amp;#125;  &amp;#60;-- no property name here&lt;br /&gt;            &amp;#125;&lt;br /&gt;&lt;br /&gt;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. &lt;br /&gt;&lt;br /&gt;Also useful in OR Mapping and other frameworks where you need to reliably detect changes in property values &amp;#40;without resorting to runtime-sublassing or other traditional AOP techniques&amp;#41;.&lt;br /&gt;&lt;br /&gt;Effectively ActiveSharp amounts to a brand-new technique for interception&amp;#47;AOP &amp;#40;for properties only&amp;#41;.   &lt;br /&gt;&lt;br /&gt;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 &amp;#39;by ref&amp;#39; parameter. &lt;br /&gt;&lt;br /&gt;&amp;#40;This apporach is much faster and more reliable than inspecting call stacks, which is a commonly-quoted, but flawed, alternative&amp;#41;.  &lt;br /&gt;&lt;br /&gt;It is implemented as a number of independent classes which you can mix and match for your own purposes &amp;#40;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&amp;#41;.&lt;br /&gt;&lt;br /&gt;Requires .NET 2.0 or later.
&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Implementing INotifyPropertyChanged&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Option 1:&lt;/b&gt; The easiest way is to use ActiveSharp's PropertyChangeHelper:&lt;br /&gt; &lt;br /&gt;Add this code to your class or (preferrably) to a base class:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    readonly PropertyChangeHelper _propertyChangeHelper = new PropertyChangeHelper();
 
    public event PropertyChangedEventHandler PropertyChanged
    {
        add { _propertyChangeHelper.Add(value); }
        remove { _propertyChangeHelper.Remove(value); }
    }
            
    protected void SetValue&amp;lt;T&amp;gt;(ref T field, T value)
    {
        _propertyChangeHelper.SetValue(this, ref field, value);
    }
&lt;/pre&gt; &lt;br /&gt;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.  &lt;br /&gt; &lt;br /&gt;In your derived classes, simply implement your properties like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    public int Foo
    {
        get { return _foo; }
        set { SetValue(ref _foo, value); }   // assigns value and does prop change notification, all in one line
    }
&lt;/pre&gt; &lt;br /&gt;&lt;b&gt;Option 2:&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;If you want more control, you can write your own SetValue method, instead of delegating to the helper class.  Someting like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    protected void SetValue&amp;lt;T&amp;gt;(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
        ...
    }
&lt;/pre&gt; &lt;br /&gt;As you can see, all the &amp;quot;magic&amp;quot; is in ActiveSharp.PropertyMapping.PropertyMap.GetProperty.  Given an object, and a field in that object, passed &amp;quot;by ref&amp;quot;, 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.&lt;br /&gt; &lt;br /&gt;For more details see:&lt;br /&gt; &lt;br /&gt;A page on: &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How%20it%20works&amp;amp;referringTitle=Home"&gt;How it works&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;Dicsussion thread &lt;a href="https://www.codeplex.com/Thread/View.aspx?ProjectName=ActiveSharp&amp;amp;ThreadId=22315" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; for discussion of other things you can do with ActiveSharp, and how it compares with the alternatives.&lt;br /&gt; &lt;br /&gt;Blog posts &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (including the comments) &lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 04:05:25 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080524040525A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=9</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, at last, here's a very quick summary of how ActiveSharp works.&lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt;&lt;h1&gt;
One-off setup
&lt;/h1&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;My first version of this code&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; used C++ / CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C++ can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed. (By the way, writing the lazy initialization support was interesting, due to threadsafety issues.  I wrote the classes LazyDictionary and LazyDictionaryOfTypes, and made them re-usable for other cases that need lazy initialization e.g. you can use them outside ActiveSharp).&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://blogs.msdn.com/joelpob/archive/2004/03/31/105282.aspx" class="externalLink"&gt;Lightweight Code Gen&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt;For this, we inspect the MSIL of the property setter methods, to see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;----&lt;br /&gt;Any questions - leave a comment or &lt;a href="http://www.agilekiwi.com/contact.htm" class="externalLink"&gt;email me&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 04:04:30 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524040430A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=8</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, at last, here's a very quick summary of how ActiveSharp works.&lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt;&lt;h1&gt;
One-off setup
&lt;/h1&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;My first version of this code&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; used C++ / CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C++ can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed. (By the way, writing the lazy initialization support was interesting, due to threadsafety issues.  I wrote the classes LazyDictionary and LazyDictionaryOfTypes, and made them re-usable for other cases that need lazy initialization e.g. you can use them outside ActiveSharp).&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://blogs.msdn.com/joelpob/archive/2004/03/31/105282.aspx" class="externalLink"&gt;Lightweight Code Gen&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt;For this, we inspect the MSIL of the property setter methods, to see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;----&lt;br /&gt;Any questions - leave me a note on the Discussions tab, or &lt;a href="http://www.agilekiwi.com/contact.htm" class="externalLink"&gt;email me&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 04:03:48 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524040348A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=7</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, at last, here's a very quick summary of how ActiveSharp works.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt; &lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;&lt;h1&gt;
One-off setup
&lt;/h1&gt; &lt;br /&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt; &lt;br /&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;My first version of this code&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; used C++ / CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C++ can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt; &lt;br /&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed.&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://blogs.msdn.com/joelpob/archive/2004/03/31/105282.aspx" class="externalLink"&gt;Lightweight Code Gen&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt; &lt;br /&gt;For this, we inspect the MSIL of the property setter methods, so see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;----&lt;br /&gt;Any questions - leave me a note on the Discussions tab, or &lt;a href="http://www.agilekiwi.com/contact.htm" class="externalLink"&gt;email me&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:59:21 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524035921A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=6</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, at last, here's a very quick summary of how ActiveSharp works.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt; &lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;&lt;h1&gt;
One-off setup
&lt;/h1&gt; &lt;br /&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt; &lt;br /&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;My first version of this code&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; used C&lt;u&gt;+/CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C+&lt;/u&gt; can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt; &lt;br /&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed.&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://blogs.msdn.com/joelpob/archive/2004/03/31/105282.aspx" class="externalLink"&gt;Lightweight Code Gen&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt; &lt;br /&gt;For this, we inspect the MSIL of the property setter methods, so see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;----&lt;br /&gt;Any questions - leave me a note on the Discussions tab, or &lt;a href="http://www.agilekiwi.com/contact.htm" class="externalLink"&gt;email me&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:58:43 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524035843A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=5</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, at last, here's a very quick summary of how ActiveSharp works.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt; &lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;&lt;h1&gt;
One-off setup
&lt;/h1&gt; &lt;br /&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt; &lt;br /&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.htm" class="externalLink"&gt;My first version of this code&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; used C++ / CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C++ can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt; &lt;br /&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed.&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://blogs.msdn.com/joelpob/archive/2004/03/31/105282.aspx" class="externalLink"&gt;Lightweight Code Gen&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt; &lt;br /&gt;For this, we inspect the MSIL of the property setter methods, so see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;----&lt;br /&gt;Any questions - leave me a note on the Discussions tab, or &lt;a href="http://www.agilekiwi.com/contact.htm" class="externalLink"&gt;email me&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:56:49 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524035649A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=4</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, here's a very quick summary of how ActiveSharp works.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt; &lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;&lt;h1&gt;
One-off setup
&lt;/h1&gt; &lt;br /&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt; &lt;br /&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=http%3a%2f%2fdotnet.agilekiwi.com%2fblog%2f2007%2f07%2fclaytons-interception.htm&amp;amp;referringTitle=How%20it%20works"&gt;url=My first version of this code&lt;/a&gt; used C++ / CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C++ can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt; &lt;br /&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed.&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=http%3a%2f%2fblogs.msdn.com%2fjoelpob%2farchive%2f2004%2f03%2f31%2f105282.aspx&amp;amp;referringTitle=How%20it%20works"&gt;url=Lightweight Code Gen&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt; &lt;br /&gt;For this, we inspect the MSIL of the property setter methods, so see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;----&lt;br /&gt;Any questions - leave me a note on the Discussions tab, or &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=urlhttp%3a%2f%2fwww.agilekiwi.com%2fcontact.htm&amp;amp;referringTitle=How%20it%20works"&gt;email me&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:55:49 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524035549A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=3</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, here's a very quick summary of how ActiveSharp works.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt; &lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;!One-off setup&lt;br /&gt; &lt;br /&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt; &lt;br /&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=urlhttp%3a%2f%2fdotnet.agilekiwi.com%2fblog%2f2007%2f07%2fclaytons-interception.htm&amp;amp;referringTitle=How%20it%20works"&gt;My first version of this code&lt;/a&gt; used C&lt;u&gt;+/CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C+&lt;/u&gt; can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt; &lt;br /&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed.&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=urlhttp%3a%2f%2fblogs.msdn.com%2fjoelpob%2farchive%2f2004%2f03%2f31%2f105282.aspx&amp;amp;referringTitle=How%20it%20works"&gt;Lightweight Code Gen&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given appropriate reflection permissions).&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt; &lt;br /&gt;For this, we inspect the MSIL of the property setter methods, so see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;Any questions - leave me a note on the Discussions tab, or &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=urlhttp%3a%2f%2fwww.agilekiwi.com%2fcontact.htm&amp;amp;referringTitle=How%20it%20works"&gt;email me&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:53:46 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524035346A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=2</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, here's a very quick summary of how ActiveSharp works.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt; &lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;Now we have the property. DONE&lt;/li&gt;
&lt;/ol&gt; &lt;br /&gt;!One-off setup&lt;br /&gt; &lt;br /&gt;ActiveSharp automatically initializes itself at runtime.  There are three things to initialize:&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The offset calculation routine
&lt;/h2&gt; &lt;br /&gt;If you were thinking that its difficult to cast things to raw memory addresses in C#, you'd be right.  In fact, it's impossible.  &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=http%3a%2f%2fdotnet.agilekiwi.com%2fblog%2f2007%2f07%2fclaytons-interception.html&amp;amp;referringTitle=How%20it%20works"&gt;My first version of this code&lt;/a&gt; used C&lt;u&gt;+/CLI instead, but even that couldn't cast a &amp;quot;this&amp;quot; pointer (it could only do fields).  The older &amp;quot;Managed Extensions&amp;quot; for C+&lt;/u&gt; can cast the this pointer.  &lt;br /&gt; &lt;br /&gt;However, to save messing around with assemblies written in other languages, what ActiveSharp actually does is it generates the necessary routine at runtime, by creating the raw MSIL with Reflection.Emit.    That lets us use a pure C# solution to do something that actually can't be done in C# ;-)&lt;br /&gt; &lt;br /&gt;We make this routine once, the first time that ActiveSharp is used, and then we hold onto it for all future use.&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps offsets to fields
&lt;/h2&gt; &lt;br /&gt;We need one hash table for each class in which ActiveSharp is used (i.e. each different class which SetValue passes as the &amp;quot;this&amp;quot; pointer).  ActiveSharp intializes the hash table lazily, building the table for a given type the first time it is needed.&lt;br /&gt; &lt;br /&gt;We build it by passing every field in the object to our offset finder method and seeing what offset number it returns.  The returned number becomes the key in the hash table and the field name becomes the value.&lt;br /&gt; &lt;br /&gt;How do we pass every field, especially of they are private fields?  We use &lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=http%3a%2f%2fblogs.msdn.com%2fjoelpob%2farchive%2f2004%2f03%2f31%2f105282.aspx&amp;amp;referringTitle=How%20it%20works"&gt;Lightweight Code Gen&lt;/a&gt; since, unlike standard Reflection Emil, LGC can access private fields (given approprate reflection permissions).&lt;br /&gt; &lt;br /&gt;&lt;h2&gt;
The hash table that maps fields to properties
&lt;/h2&gt; &lt;br /&gt;For this, we inspect the MSIL of the property setter methods, so see which field is set by each one.  We can do this because, as of .NET 2.0, we can get method bodies at runtime in MSIL form.  We scan the MSIL instructions, with a very basic parsing algorithm, to find out what we need to know.&lt;br /&gt; &lt;br /&gt;Any questions - leave me a note on the Discussions tab, or email me.&lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:51:06 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524035106A</guid></item><item><title>UPDATED WIKI: How it works</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How it works&amp;version=1</link><description>&lt;div class="wikidoc"&gt;
I've been meaning to write the page for a while, but haven't found time.  So, here's a very quick summary of how ActiveSharp works.&lt;br /&gt; &lt;br /&gt;&lt;h1&gt;
When you set a property value
&lt;/h1&gt; &lt;br /&gt;&lt;ol&gt;
&lt;li&gt;Property setter method calls the SetValue method which you defined (typically on your base class).  By the way, it doesn't have to be called SetValue, you can call it anything.&lt;/li&gt;&lt;li&gt;SetValue passes &amp;quot;this&amp;quot; and the field being set (by ref) to ActiveSharp.PropertyMapping.PropertyMap.GetProperty.&lt;/li&gt;&lt;li&gt;GetProperty calls a special routine which computes the difference between the memory locations of the &amp;quot;this&amp;quot; pointer and the field, like so:&lt;/li&gt;&lt;ol&gt;
&lt;li&gt;Briefly pin the object in memory, so the garbage collector won't move it while we are working&lt;/li&gt;&lt;li&gt;Cast the &amp;quot;this&amp;quot; pointer to an untyped pointer (i.e. a raw memory address)&lt;/li&gt;&lt;li&gt;Cast the field's location to an untyped pointer (again, a raw memory address).  We can do this only because we recieved the field &amp;quot;by ref&amp;quot;&lt;/li&gt;&lt;li&gt;Subtract one raw memory address from the other, to give an &amp;quot;offset value&amp;quot; - i.e. the field's unique position within it's containing object&lt;/li&gt;&lt;li&gt;Unpin the object (total duration of the pinning is very short)&lt;/li&gt;
&lt;/ol&gt;&lt;li&gt;Use the offset value to look up the field name.  We do this lookup in a hashtable that we prepared earlier (see below).&lt;/li&gt;&lt;li&gt;Use the field name to look up the property.  Again, we do this in a hashtable that we prepared earlier (see below)&lt;/li&gt;&lt;li&gt;DONE&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:37:43 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: How it works 20080524033743A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Home&amp;version=12</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;Send property change notifications without specifying property name as a string.  Instead, write properties like this&lt;br /&gt;&lt;br /&gt;            public int Foo&lt;br /&gt;            &amp;#123;&lt;br /&gt;                get &amp;#123; return _foo&amp;#59; &amp;#125;&lt;br /&gt;                set &amp;#123; SetValue&amp;#40;ref _foo, value&amp;#41;&amp;#59; &amp;#125;  &amp;#60;-- no property name here&lt;br /&gt;            &amp;#125;&lt;br /&gt;&lt;br /&gt;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. &lt;br /&gt;&lt;br /&gt;Also useful in OR Mapping and other frameworks where you need to reliably detect changes in property values &amp;#40;without resorting to runtime-sublassing or other traditional AOP techniques&amp;#41;.&lt;br /&gt;&lt;br /&gt;Effectively ActiveSharp amounts to a brand-new technique for interception&amp;#47;AOP &amp;#40;for properties only&amp;#41;.   &lt;br /&gt;&lt;br /&gt;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 &amp;#39;by ref&amp;#39; parameter. &lt;br /&gt;&lt;br /&gt;&amp;#40;This apporach is much faster and more reliable than inspecting call stacks, which is a commonly-quoted, but flawed, alternative&amp;#41;.  &lt;br /&gt;&lt;br /&gt;It is implemented as a number of independent classes which you can mix and match for your own purposes &amp;#40;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&amp;#41;.&lt;br /&gt;&lt;br /&gt;Requires .NET 2.0 or later.
&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Implementing INotifyPropertyChanged&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Option 1:&lt;/b&gt; The easiest way is to use ActiveSharp's PropertyChangeHelper:&lt;br /&gt; &lt;br /&gt;Add this code to your class or (preferrably) to a base class:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    readonly PropertyChangeHelper _propertyChangeHelper = new PropertyChangeHelper();
 
    public event PropertyChangedEventHandler PropertyChanged
    {
        add { _propertyChangeHelper.Add(value); }
        remove { _propertyChangeHelper.Remove(value); }
    }
            
    protected void SetValue&amp;lt;T&amp;gt;(ref T field, T value)
    {
        _propertyChangeHelper.SetValue(this, ref field, value);
    }
&lt;/pre&gt; &lt;br /&gt;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.  &lt;br /&gt; &lt;br /&gt;In your derived classes, simply implement your properties like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    public int Foo
    {
        get { return _foo; }
        set { SetValue(ref _foo, value); }   // assigns value and does prop change notification, all in one line
    }
&lt;/pre&gt; &lt;br /&gt;&lt;b&gt;Option 2:&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;If you want more control, you can write your own SetValue method, instead of delegating to the helper class.  Someting like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    protected void SetValue&amp;lt;T&amp;gt;(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
        ...
    }
&lt;/pre&gt; &lt;br /&gt;As you can see, all the &amp;quot;magic&amp;quot; is in ActiveSharp.PropertyMapping.PropertyMap.GetProperty.  Given an object, and a field in that object, passed &amp;quot;by ref&amp;quot;, 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.&lt;br /&gt; &lt;br /&gt;For more details see:&lt;br /&gt; &lt;br /&gt;&lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How%20it%20works&amp;amp;referringTitle=Home"&gt;How it works&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;Dicsussion thread &lt;a href="https://www.codeplex.com/Thread/View.aspx?ProjectName=ActiveSharp&amp;amp;ThreadId=22315" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; for discussion of other things you can do with ActiveSharp, and how it compares with the alternatives.&lt;br /&gt; &lt;br /&gt;Blog posts &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (including the comments) &lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:30:00 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080524033000A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Home&amp;version=11</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;Send property change notifications without specifying property name as a string.  Instead, write properties like this&lt;br /&gt;&lt;br /&gt;            public int Foo&lt;br /&gt;            &amp;#123;&lt;br /&gt;                get &amp;#123; return _foo&amp;#59; &amp;#125;&lt;br /&gt;                set &amp;#123; SetValue&amp;#40;ref _foo, value&amp;#41;&amp;#59; &amp;#125;  &amp;#60;-- no property name here&lt;br /&gt;            &amp;#125;&lt;br /&gt;&lt;br /&gt;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. &lt;br /&gt;&lt;br /&gt;Also useful in OR Mapping and other frameworks where you need to reliably detect changes in property values &amp;#40;without resorting to runtime-sublassing or other traditional AOP techniques&amp;#41;.&lt;br /&gt;&lt;br /&gt;Effectively ActiveSharp amounts to a brand-new technique for interception&amp;#47;AOP &amp;#40;for properties only&amp;#41;.   &lt;br /&gt;&lt;br /&gt;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 &amp;#39;by ref&amp;#39; parameter. &lt;br /&gt;&lt;br /&gt;&amp;#40;This apporach is much faster and more reliable than inspecting call stacks, which is a commonly-quoted, but flawed, alternative&amp;#41;.  &lt;br /&gt;&lt;br /&gt;It is implemented as a number of independent classes which you can mix and match for your own purposes &amp;#40;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&amp;#41;.&lt;br /&gt;&lt;br /&gt;Requires .NET 2.0 or later.
&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Implementing INotifyPropertyChanged&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Option 1:&lt;/b&gt; The easiest way is to use ActiveSharp's PropertyChangeHelper:&lt;br /&gt; &lt;br /&gt;Add this code to your class or (preferrably) to a base class:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    readonly PropertyChangeHelper _propertyChangeHelper = new PropertyChangeHelper();
 
    public event PropertyChangedEventHandler PropertyChanged
    {
        add { _propertyChangeHelper.Add(value); }
        remove { _propertyChangeHelper.Remove(value); }
    }
            
    protected void SetValue&amp;lt;T&amp;gt;(ref T field, T value)
    {
        _propertyChangeHelper.SetValue(this, ref field, value);
    }
&lt;/pre&gt; &lt;br /&gt;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.  &lt;br /&gt; &lt;br /&gt;In your derived classes, simply implement your properties like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    public int Foo
    {
        get { return _foo; }
        set { SetValue(ref _foo, value); }   // assigns value and does prop change notification, all in one line
    }
&lt;/pre&gt; &lt;br /&gt;&lt;b&gt;Option 2:&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;If you want more control, you can write your own SetValue method, instead of delegating to the helper class.  Someting like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    protected void SetValue&amp;lt;T&amp;gt;(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
        ...
    }
&lt;/pre&gt; &lt;br /&gt;As you can see, all the &amp;quot;magic&amp;quot; is in ActiveSharp.PropertyMapping.PropertyMap.GetProperty.  Given an object, and a field in that object, passed &amp;quot;by ref&amp;quot;, the routine finds the property that sets the field.  &lt;br /&gt; &lt;br /&gt;For more details see:&lt;br /&gt; &lt;br /&gt;&lt;a href="http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=How%20it%20works&amp;amp;referringTitle=Home"&gt;How it works&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;Dicsussion thread &lt;a href="https://www.codeplex.com/Thread/View.aspx?ProjectName=ActiveSharp&amp;amp;ThreadId=22315" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; for discussion of other things you can do with ActiveSharp, and how it compares with the alternatives.&lt;br /&gt; &lt;br /&gt;Blog posts &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (including the comments) &lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Sat, 24 May 2008 03:26:28 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080524032628A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://www.codeplex.com/ActiveSharp/Wiki/View.aspx?title=Home&amp;version=10</link><description>&lt;div class="wikidoc"&gt;
&lt;b&gt;Project Description&lt;/b&gt;&lt;br /&gt;Send property change notifications without specifying property name as a string.  Instead, write properties like this&lt;br /&gt;&lt;br /&gt;            public int Foo&lt;br /&gt;            &amp;#123;&lt;br /&gt;                get &amp;#123; return _foo&amp;#59; &amp;#125;&lt;br /&gt;                set &amp;#123; SetValue&amp;#40;ref _foo, value&amp;#41;&amp;#59; &amp;#125;  &amp;#60;-- no property name here&lt;br /&gt;            &amp;#125;&lt;br /&gt;&lt;br /&gt;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. &lt;br /&gt;&lt;br /&gt;Also useful in OR Mapping and other frameworks where you need to reliably detect changes in property values &amp;#40;without resorting to runtime-sublassing or other traditional AOP techniques&amp;#41;.&lt;br /&gt;&lt;br /&gt;Effectively ActiveSharp amounts to a brand-new technique for interception&amp;#47;AOP &amp;#40;for properties only&amp;#41;.   &lt;br /&gt;&lt;br /&gt;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 &amp;#39;by ref&amp;#39; parameter. &lt;br /&gt;&lt;br /&gt;&amp;#40;This apporach is much faster and more reliable than inspecting call stacks, which is a commonly-quoted, but flawed, alternative&amp;#41;.  &lt;br /&gt;&lt;br /&gt;It is implemented as a number of independent classes which you can mix and match for your own purposes &amp;#40;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&amp;#41;.&lt;br /&gt;&lt;br /&gt;Requires .NET 2.0 or later.
&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Implementing INotifyPropertyChanged&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Option 1:&lt;/b&gt; The easiest way is to use ActiveSharp's PropertyChangeHelper:&lt;br /&gt; &lt;br /&gt;Add this code to your class or (preferrably) to a base class:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    readonly PropertyChangeHelper _propertyChangeHelper = new PropertyChangeHelper();
 
    public event PropertyChangedEventHandler PropertyChanged
    {
        add { _propertyChangeHelper.Add(value); }
        remove { _propertyChangeHelper.Remove(value); }
    }
            
    protected void SetValue&amp;lt;T&amp;gt;(ref T field, T value)
    {
        _propertyChangeHelper.SetValue(this, ref field, value);
    }
&lt;/pre&gt; &lt;br /&gt;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.  &lt;br /&gt; &lt;br /&gt;In your derived classes, simply implement your properties like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    public int Foo
    {
        get { return _foo; }
        set { SetValue(ref _foo, value); }   // assigns value and does prop change notification, all in one line
    }
&lt;/pre&gt; &lt;br /&gt;&lt;b&gt;Option 2:&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;If you want more control, you can write your own SetValue method, instead of delegating to the helper class.  Someting like this:&lt;br /&gt; &lt;br /&gt;&lt;pre&gt;
    protected void SetValue&amp;lt;T&amp;gt;(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
        ...
    }
&lt;/pre&gt; &lt;br /&gt;For more details see:&lt;br /&gt; &lt;br /&gt;Dicsussion thread &lt;a href="https://www.codeplex.com/Thread/View.aspx?ProjectName=ActiveSharp&amp;amp;ThreadId=22315" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; for discussion of other things you can do with ActiveSharp, and how it compares with the alternatives.&lt;br /&gt; &lt;br /&gt;Blog posts &lt;a href="http://dotnet.agilekiwi.com/blog/2008/02/better-property-change-notification.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and &lt;a href="http://dotnet.agilekiwi.com/blog/2007/07/claytons-interception.html" class="externalLink"&gt;here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; (including the comments) &lt;br /&gt;
&lt;/div&gt;</description><author>johnrusk</author><pubDate>Fri, 11 Apr 2008 21:35:32 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080411093532P</guid></item></channel></rss>