<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dmitry Shechtman&#039;s Blog</title>
	<atom:link href="http://shecht.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://shecht.wordpress.com</link>
	<description>A bit of dependency injection</description>
	<lastBuildDate>Thu, 05 Jan 2012 07:44:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='shecht.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Dmitry Shechtman&#039;s Blog</title>
		<link>http://shecht.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://shecht.wordpress.com/osd.xml" title="Dmitry Shechtman&#039;s Blog" />
	<atom:link rel='hub' href='http://shecht.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Blog Moved</title>
		<link>http://shecht.wordpress.com/2011/08/13/blog-moved/</link>
		<comments>http://shecht.wordpress.com/2011/08/13/blog-moved/#comments</comments>
		<pubDate>Sat, 13 Aug 2011 20:44:12 +0000</pubDate>
		<dc:creator>Dmitry Shechtman</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://shecht.wordpress.com/?p=228</guid>
		<description><![CDATA[My blog has moved to Microsoft Israel. Please update your bookmarks (RSS). Update: Moved the RSS to FeedBurner. Please re-update<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=228&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>My blog has moved to <a href="http://blogs.microsoft.co.il/blogs/shecht/" title="Dmitry Shechtman 2.0">Microsoft Israel</a>. Please update your bookmarks (<a href="http://feeds.feedburner.com/DmitryShechtman">RSS</a>).</p>
<p><strong>Update:</strong> Moved the RSS to FeedBurner. Please re-update <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shecht.wordpress.com/228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shecht.wordpress.com/228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shecht.wordpress.com/228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shecht.wordpress.com/228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shecht.wordpress.com/228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shecht.wordpress.com/228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shecht.wordpress.com/228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shecht.wordpress.com/228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shecht.wordpress.com/228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shecht.wordpress.com/228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shecht.wordpress.com/228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shecht.wordpress.com/228/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shecht.wordpress.com/228/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shecht.wordpress.com/228/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=228&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shecht.wordpress.com/2011/08/13/blog-moved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65b02c72404b4e3bad4a026e275f089e?s=96&#38;d=identicon&#38;r=R" medium="image">
			<media:title type="html">shecht</media:title>
		</media:content>
	</item>
		<item>
		<title>Developer Academy</title>
		<link>http://shecht.wordpress.com/2010/03/21/developer-academy/</link>
		<comments>http://shecht.wordpress.com/2010/03/21/developer-academy/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 19:02:34 +0000</pubDate>
		<dc:creator>Dmitry Shechtman</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[devacd4]]></category>

		<guid isPermaLink="false">http://shecht.wordpress.com/?p=222</guid>
		<description><![CDATA[Wow, it&#8217;s been a long time. Crazy deadlines and long commutes are to blame. Nothing to do with my laziness I&#8217;ll be attending Microsoft Developer Academy 4.0 (Hebrew-only site, sorry). I&#8217;ll take a stab at blogging live from the event (wireless coverage permitting). See you around! Update: Got myself a Twitter account @dudnet.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=222&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Wow, it&#8217;s been a long time. Crazy deadlines and long commutes are to blame. Nothing to do with my laziness <img src='http://s2.wp.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>I&#8217;ll be attending <a href="http://www.microsoft.com/israel/msdn/DevAcademy4/Default.aspx">Microsoft Developer Academy 4.0</a> (Hebrew-only site, sorry). I&#8217;ll take a stab at blogging live from the event (wireless coverage permitting). See you around!</p>
<p><strong>Update:</strong> Got myself a Twitter account <a href="http://twitter.com/dudnet">@dudnet</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shecht.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shecht.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shecht.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shecht.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shecht.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shecht.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shecht.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shecht.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shecht.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shecht.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shecht.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shecht.wordpress.com/222/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shecht.wordpress.com/222/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shecht.wordpress.com/222/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=222&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shecht.wordpress.com/2010/03/21/developer-academy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65b02c72404b4e3bad4a026e275f089e?s=96&#38;d=identicon&#38;r=R" medium="image">
			<media:title type="html">shecht</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking for Something?</title>
		<link>http://shecht.wordpress.com/2010/01/14/looking-for-something/</link>
		<comments>http://shecht.wordpress.com/2010/01/14/looking-for-something/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 23:18:26 +0000</pubDate>
		<dc:creator>Dmitry Shechtman</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[IDataErrorInfo]]></category>

		<guid isPermaLink="false">http://shecht.wordpress.com/?p=216</guid>
		<description><![CDATA[It looks like this blog just hit the front page of Google&#8217;s search results for IDataErrorInfo. I certainly hope this wasn&#8217;t caused by the Chinese attack on Google. P.S. I didn&#8217;t abandon the PropertyChange series; the third article is to be published soon. In the meantime, check out some derivaciónes<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=216&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It looks like this blog just hit the front page of Google&#8217;s search results for <a href="http://www.google.com/search?q=idataerrorinfo">IDataErrorInfo</a>. I certainly hope this wasn&#8217;t caused by <a href="http://googleblog.blogspot.com/2010/01/new-approach-to-china.html">the Chinese attack on Google</a>.</p>
<p><strong>P.S.</strong> I didn&#8217;t abandon the <a href="http://shecht.wordpress.com/category/propertychange/">PropertyChange</a> series; the third article is to be published soon. In the meantime, check out <a href="http://blogs.msdn.com/knowledgecast/archive/2009/12/31/wpf-starter-kit-automatic-change-notification-on-view-models.aspx">some</a> <a href="http://geeks.ms/blogs/etomas/archive/2010/01/13/objetos-que-notifican-sus-cambios-de-propiedades-1-3-la-intercepci-243-n.aspx">derivaciónes</a> <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shecht.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shecht.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shecht.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shecht.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shecht.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shecht.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shecht.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shecht.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shecht.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shecht.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shecht.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shecht.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shecht.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shecht.wordpress.com/216/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=216&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shecht.wordpress.com/2010/01/14/looking-for-something/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65b02c72404b4e3bad4a026e275f089e?s=96&#38;d=identicon&#38;r=R" medium="image">
			<media:title type="html">shecht</media:title>
		</media:content>
	</item>
		<item>
		<title>IDataErrorInfo with Validation Application Block and Unity Interception AOP</title>
		<link>http://shecht.wordpress.com/2009/12/19/idataerrorinfo-with-validation-application-block-and-unity-interception-aop/</link>
		<comments>http://shecht.wordpress.com/2009/12/19/idataerrorinfo-with-validation-application-block-and-unity-interception-aop/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 10:40:01 +0000</pubDate>
		<dc:creator>Dmitry Shechtman</dc:creator>
				<category><![CDATA[PropertyChange]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DataErrorInfo]]></category>
		<category><![CDATA[EntLib]]></category>
		<category><![CDATA[IDataErrorInfo]]></category>
		<category><![CDATA[IndexerMatchingRule]]></category>
		<category><![CDATA[Interception]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[PIAB]]></category>
		<category><![CDATA[Unity]]></category>
		<category><![CDATA[VAB]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://shecht.wordpress.com/?p=123</guid>
		<description><![CDATA[This is the second article in a planned series of articles concerning AOP with Unity Interception and Policy Injection: INotifyPropertyChanged AOP IDataErrorInfo AOP Policy Injection Performance and optimizations The final solution I hope you will find this useful in your work; all feedback will be greatly appreciated. The previous article discussed an implementation of the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=123&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the second article in a planned series of articles concerning AOP with Unity Interception and Policy Injection:</p>
<ol>
<li><a href="http://shecht.wordpress.com/2009/12/12/inotifypropertychanged-with-unity-interception-aop/">INotifyPropertyChanged AOP</a></li>
<li><strong>IDataErrorInfo AOP</strong></li>
<li>Policy Injection</li>
<li>Performance and optimizations</li>
<li>The final solution</li>
</ol>
<p>I hope you will find this useful in your work; all feedback will be greatly appreciated.</p>
<p><a href="http://shecht.wordpress.com/2009/12/12/inotifypropertychanged-with-unity-interception-aop/">The previous article</a> discussed an implementation of the <code>INotifyPropertyChanged</code> aspect using Unity Interception AOP. Today we will see how a similar approach can be applied to another interface commonly used by WPF (as well as by ASP.NET MVC).</p>
<p>The <code>IDataErrorInfo</code> interface consists of two methods:</p>
<ol>
<li>an indexer accepting a property name (<code>columnName</code>) parameter and</li>
<li>an <code>Error</code> property.</li>
</ol>
<p>We will solely focus on the indexer, which returns error messages on a per-property basis.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd140088.aspx">The Validation Application Block</a> allows developers to easily incorporate input validation via property attributes and/or application configuration, supporting a variety of provided validators, as well as custom ones.</p>
<p><code>IDataErrorInfo</code> and VAB seem to be a perfect match, and have indeed <a href="http://www.ademiller.com/blogs/tech/2008/12/using-the-entlib-validation-block-with-wpf-part-1-attribute-based-validation/">been</a> <a href="http://codebetter.com/blogs/david.hayden/archive/2009/01/31/asp-net-mvc-and-validation-using-idataerrorinfo-and-validation-application-block.aspx">integrated</a>. Adding Unity Interception will further simplify input validation, so that (presentation) models could be specified in the following manner:</p>
<p><pre class="brush: csharp; gutter: false;">[NotifyPropertyChanged, DataErrorInfo]
public class Contact : MarshalByRefObject, INotifyPropertyChanged, IDataErrorInfo
{
    [StringLengthValidator(1, 20, MessageTemplate=&quot;First name length must be between {3} and {5}.&quot;)]
    [ContainsCharactersValidator(&quot;0123456789&quot;, Negated = true, MessageTemplate = &quot;First name cannot contain digits.&quot;)]
    public string FirstName { get; set; }

    [StringLengthValidator(1, 20, MessageTemplate = &quot;Last name length must be between {3} and {5}.&quot;)]
    [ContainsCharactersValidator(&quot;0123456789&quot;, Negated = true, MessageTemplate = &quot;Last name cannot contain digits.&quot;)]
    public string LastName { get; set; }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public string Error
    {
        get { return null; }
    }

    public string this[string columnName]
    {
        get { return null; }
    }
}
</pre></p>
<p><strong>Note:</strong> The implementation presented below is limited to classes derived (possibly indirectly) from <code>MarshalByRefObject</code>.</p>
<p><span id="more-123"></span><a name="project"></a></p>
<h2>Project</h2>
<p>In addition to <code>Microsoft.Practices.Unity.Interception</code>, we will require a reference to <code>Microsoft.Practices.EnterpriseLibrary.Validation</code>, which is listed as <em>Enterprise Library Validation Application Block</em> under the .NET tab in Visual Studio (provided the Enterprise Library is installed).<br />
<a name="matchingrules"></a></p>
<h2>Matching Rules</h2>
<p>We will be intercepting</p>
<ol>
<li>getters of indexers accepting a single string parameter</li>
<li>defined in types decorated with <code>DataErrorInfoAttribute</code>:<br />
<pre class="brush: csharp;">[Serializable, AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class DataErrorInfoAttribute : Attribute { }</pre></li>
</ol>
<p>The second rule is best defined using <code><a href="http://shecht.wordpress.com/2009/12/12/inotifypropertychanged-with-unity-interception-aop/#matchingrules">CustomTypeAttributeMatchingRule</a><code>.</code></code></p>
<p>The first rule could be defined using <code>MethodSignatureMatchingRule</code> by specifying <code>get_Item</code> as the <code>methodName</code>. However, (yet another) custom matching rule seems more appropriate. This time, we will reuse some code from <code>MethodSignatureMatchingRule</code> and <code>PropertyMatchingRule</code>:</p>
<p><pre class="brush: csharp;">public class IndexerMatchingRule : IMatchingRule
{
    private readonly List&lt;Glob&gt; patterns = new List&lt;Glob&gt;();
    private readonly List&lt;TypeMatchingRule&gt; parameterRules = new List&lt;TypeMatchingRule&gt;();

    public IndexerMatchingRule(PropertyMatchingOption option, IEnumerable&lt;string&gt; parameterTypeNames)
        : this(option, parameterTypeNames, false)
    {
    }

    public IndexerMatchingRule(PropertyMatchingOption option, IEnumerable&lt;string&gt; parameterTypeNames, bool ignoreCase)
    {
        if (option != PropertyMatchingOption.Set)
            patterns.Add(new Glob(&quot;get_Item&quot;));
        if (option != PropertyMatchingOption.Get)
            patterns.Add(new Glob(&quot;set_Item&quot;));

        foreach (string parameterTypeName in parameterTypeNames)
            parameterRules.Add(new TypeMatchingRule(parameterTypeName, ignoreCase));
    }

    public bool Matches(MethodBase member)
    {
        if (!member.IsSpecialName || !patterns.Exists(delegate(Glob pattern) { return pattern.IsMatch(member.Name); }))
            return false;

        ParameterInfo[] parameters = member.GetParameters();
        if (parameters.Length != parameterRules.Count)
            return false;

        for (int i = 0; i &lt; parameters.Length; ++i)
            if (!parameterRules[i].Matches(parameters[i].ParameterType))
                return false;

        return true;
    }
}
</pre></p>
<p><a name="callhandler"></a></p>
<h2>Call Handler</h2>
<p>Our call handler will be pretty compact.</p>
<p><pre class="brush: csharp;">public class DataErrorInfoItemCallHandler : ICallHandler</pre></p>
<p><code>ICallHandler</code> members:</p>
<p><pre class="brush: csharp; first-line: 3;">    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        IMethodReturn res = getNext()(input, getNext);

        string propertyName = input.Arguments.Count &gt; 0 ?
            input.Arguments[0] as string :
            null;

        res.ReturnValue = GetError(input.Target, propertyName);

        return res;
    }

    public int Order { get; set; }
</pre></p>
<p>Lines 7-9 provide a sanity check, so that the handler won&#8217;t throw an exception when invoked incorretly (i.e. without a string argument).</p>
<p><pre class="brush: csharp; first-line: 18;">    private ValidationResults GetError(object target, string propertyName)
    {
        MethodInfo validateGen = validate.MakeGenericMethod(target.GetType());
        ValidationResults results = (ValidationResults)validateGen.Invoke(null, new object[] { target });
        if (results.IsValid)
            return string.Empty;

        return string.Join(&quot;\n&quot;, results.Where(r =&gt; r.Key == propertyName).Select(r =&gt; r.Message).ToArray());
    }

    private static readonly MethodInfo validate = typeof(Validation).GetMethods().First(m =&gt; m.Name == &quot;Validate&quot;);
</pre></p>
<p>While the reference to <code>Validation.Validate()</code> is constant, we need to obtain a type-specific (&#8220;generic&#8221;) version per target type. Multiple validation messages are concatenated with newlines inserted between them.</p>
<p>In the next article we will update our implementation to support <a href="http://msdn.microsoft.com/en-us/library/dd139982.aspx">the Policy Injection Application Block</a> and see several deployment and usage scenarios. Stay tuned!</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F19%2Fidataerrorinfo-with-validation-application-block-and-unity-interception-aop%2F"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F19%2Fidataerrorinfo-with-validation-application-block-and-unity-interception-aop%2F" border="0" alt="kick it on DotNetKicks.com" /></a> <a rev="vote-for" href="http://dotnetshoutout.com/IDataErrorInfo-with-Validation-Application-Block-and-Unity-Interception-AOP-Dmitry-Shechtmans-Blog"><img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F19%2Fidataerrorinfo-with-validation-application-block-and-unity-interception-aop%2F" style="border:0;height:18px;" height="19" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shecht.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shecht.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shecht.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shecht.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shecht.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shecht.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shecht.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shecht.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shecht.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shecht.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shecht.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shecht.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shecht.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shecht.wordpress.com/123/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=123&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shecht.wordpress.com/2009/12/19/idataerrorinfo-with-validation-application-block-and-unity-interception-aop/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65b02c72404b4e3bad4a026e275f089e?s=96&#38;d=identicon&#38;r=R" medium="image">
			<media:title type="html">shecht</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F19%2Fidataerrorinfo-with-validation-application-block-and-unity-interception-aop%2F" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>

		<media:content url="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F19%2Fidataerrorinfo-with-validation-application-block-and-unity-interception-aop%2F" medium="image">
			<media:title type="html">Shout it</media:title>
		</media:content>
	</item>
		<item>
		<title>Turn your project into WPF User Control Library in 10 seconds</title>
		<link>http://shecht.wordpress.com/2009/12/15/turn-your-project-into-wpf-user-control-library-in-10-seconds/</link>
		<comments>http://shecht.wordpress.com/2009/12/15/turn-your-project-into-wpf-user-control-library-in-10-seconds/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:29:49 +0000</pubDate>
		<dc:creator>Dmitry Shechtman</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ProjectTypeGuids]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[VS2010]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://shecht.wordpress.com/?p=97</guid>
		<description><![CDATA[I recently had to add a WPF resource dictionary to a project that was previously created as a plain Control Library. I learned that: all the WPF stuff (barring WPF User Control) is hidden in Add New Item menus/dialogs, no fine-tuning of project types is provided and TFS doesn&#8217;t make recreating a project an easy [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=97&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently had to add a WPF resource dictionary to a project that was previously created as a plain Control Library. I learned that:</p>
<ol>
<li>all the WPF stuff (barring WPF User Control) is hidden in Add New Item menus/dialogs,</li>
<li>no fine-tuning of project types is provided and
<li>TFS doesn&#8217;t make recreating a project an easy task,</li>
</ol>
<p>so I created a new WPF User Control Library project and compared between the two.</p>
<p>Now, if you need to add WPF capabilities to your Control Library, just follow these simple instructions:</p>
<ol>
<li>Open the .csproj file in your favorite text editor (no, VS won&#8217;t do <img src='http://s2.wp.com/wp-includes/images/smilies/icon_twisted.gif' alt=':twisted:' class='wp-smiley' /> )</li>
<li>Under the first <code>&lt;PropertyGroup&gt;</code> element add:
<p><pre class="brush: plain; gutter: false;">    &lt;ProjectTypeGuids&gt;{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}&lt;/ProjectTypeGuids&gt;</pre></li>
<li>If (VS.version &gt;= 2010) add:
<p><pre class="brush: plain; gutter: false;">    &lt;TargetFrameworkProfile&gt;Client&lt;/TargetFrameworkProfile&gt;</pre></li>
<li>Save changes.</li>
<li>Reload in VS.</li>
</ol>
<p>Hey VS team, great obfuscation job! <img src='http://s1.wp.com/wp-includes/images/smilies/icon_confused.gif' alt=':?' class='wp-smiley' /> </p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F15%2Fturn-your-project-into-wpf-user-control-library-in-10-seconds"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F15%2Fturn-your-project-into-wpf-user-control-library-in-10-seconds" border="0" alt="kick it on DotNetKicks.com" /></a> <a rev="vote-for" href="http://dotnetshoutout.com/Turn-your-project-into-WPF-User-Control-Library-in-10-seconds-Dmitry-Shechtmans-Blog"><img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F15%2Fturn-your-project-into-wpf-user-control-library-in-10-seconds%2F" style="border:0;height:18px;" height="19" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shecht.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shecht.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shecht.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shecht.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shecht.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shecht.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shecht.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shecht.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shecht.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shecht.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shecht.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shecht.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shecht.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shecht.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=97&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shecht.wordpress.com/2009/12/15/turn-your-project-into-wpf-user-control-library-in-10-seconds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65b02c72404b4e3bad4a026e275f089e?s=96&#38;d=identicon&#38;r=R" medium="image">
			<media:title type="html">shecht</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F15%2Fturn-your-project-into-wpf-user-control-library-in-10-seconds" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>

		<media:content url="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F15%2Fturn-your-project-into-wpf-user-control-library-in-10-seconds%2F" medium="image">
			<media:title type="html">Shout it</media:title>
		</media:content>
	</item>
		<item>
		<title>INotifyPropertyChanged with Unity Interception AOP</title>
		<link>http://shecht.wordpress.com/2009/12/12/inotifypropertychanged-with-unity-interception-aop/</link>
		<comments>http://shecht.wordpress.com/2009/12/12/inotifypropertychanged-with-unity-interception-aop/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 16:02:20 +0000</pubDate>
		<dc:creator>Dmitry Shechtman</dc:creator>
				<category><![CDATA[PropertyChange]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CustomTypeAttributeMatchingRule]]></category>
		<category><![CDATA[EntLib]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[Interception]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[NotifyPropertyChanged]]></category>
		<category><![CDATA[PIAB]]></category>
		<category><![CDATA[Unity]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://shecht.wordpress.com/?p=7</guid>
		<description><![CDATA[Background If you have ever developed a non-trivial application in WPF, you’ll probably find this familiar: (This is an excerpt from a real class I’ve been working on; the actual code is bigger and uglier.) Isn’t it sad that you have auto-implemented properties, but not the ability to use them? Wouldn’t you love to be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=7&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a name="background"></a></p>
<h2>Background</h2>
<p>If you have ever developed a non-trivial application in WPF, you’ll probably find this familiar:</p>
<p><pre class="brush: csharp; gutter: false;">    public class ProductFamilyViewModel : INotifyPropertyChanged
    {
        private bool isSelected;
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                if (isSelected != value)
                {
                    isSelected = value;
                    SendPropertyChanged(&quot;IsSelected&quot;);
                }
            }
        }

        private bool isExpanded;
        public bool IsExpanded
        {
            get { return isExpanded; }
            set
            {
                if (isExpanded != value)
                {
                    isExpanded = value;
                    SendPropertyChanged(&quot;IsExpanded&quot;);
                }
            }
        }

        private bool isActive;
        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if (isActive != value)
                {
                    isActive = value;
                    SendPropertyChanged(&quot;IsActive&quot;);
                }
            }
        }

        private void SendPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
</pre></p>
<p>(This is an excerpt from a real class I’ve been working on; the actual code is bigger and uglier.)</p>
<p>Isn’t it sad that you have auto-implemented properties, but not the ability to use them? Wouldn’t you love to be able to do something like that?</p>
<p><pre class="brush: csharp; gutter: false;">[NotifyPropertyChanged]
public class ProductFamilyViewModel : MarshalByRefObject, INotifyPropertyChanged
{
    public bool IsSelected { get; set; }
    public bool IsExpanded { get; set; }
    public bool IsActive { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
}</pre></p>
<p>This has been achieved (with varying levels of success) with <a href="http://serialseb.blogspot.com/2008/05/implementing-inotifypropertychanged.html">DynamicProxy2</a> and <a href="http://thetreeknowseverything.wordpress.com/2009/01/21/auto-implement-inotifypropertychanged-with-aspects/">repeatedly</a> <a href="http://richardsbraindump.blogspot.com/2009/02/aspect-oriented-programming.html">with</a> <a href="http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/">PostSharp</a> (reportedly <a href="http://www.postsharp.org/blog/introducing-postsharp-20-1-notifypropertychanged">refined</a> in PostSharp 2.0), but what about <a href="http://entlib.codeplex.com/">Microsoft Enterprise Library</a>?</p>
<p>At first, the Policy Injection Application Block might look like the perfect tool for the task<del datetime="2009-12-14T08:34:14+00:00">, but looks are often deceiving; quoting <a href="http://richardsbraindump.blogspot.com/2009/02/aspect-oriented-programming.html">Richard Banks</a>:</del></p>
<blockquote><p><del>Windsor and PIAB both have a shortfall that we can’t overcome.  Neither of them can alter the type of a class they are providing aspects for – they simply provide proxies that add hooks to intercept method calls.  We would still need to implement <code>INotifyPropertyChanged</code> in our class for binding to work.</del></p></blockquote>
<p><del datetime="2009-12-14T08:34:14+00:00">Not to fret! Luckily, </del>Enterprise Library 4.1 ships with the <a href="http://unity.codeplex.com/">Unity Application Block</a> (also available as a <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=2C8B79E7-AE56-4F90-822E-A1E43C49D12E">separate download</a>). Unity, in turn, contains the mighty Interception Extension, which seems to be getting undeservedly little attention from developers. I guess this has to do with its poor documentation and lack of UI configuration support (expected to be added in 5.0).</p>
<p>None of these, however, have prevented <a href="http://www.ctrl-shift-b.com/2009/03/implementing-inotifyproperychanged-with.html">Derek Greer</a> from trying. His post contains a great introduction to Unity Interception (Prolegomena is definitely recommended) and quite a few insights, but sadly falls short of achieving the ultimate goal.</p>
<p>This article details a complete user-friendly implementation of the <code>INotifyPropertyChanged</code> aspect using Unity Interception AOP. It is the first in a planned series of articles concerning AOP with Unity Interception and Policy Injection:</p>
<ol>
<li><strong>INotifyPropertyChanged AOP</strong></li>
<li><a href="http://shecht.wordpress.com/2009/12/19/idataerrorinfo-with-validation-application-block-and-unity-interception-aop/">IDataErrorInfo AOP</a></li>
<li>Policy Injection</li>
<li>Performance and optimizations</li>
<li>The final solution</li>
</ol>
<p>I hope you will find this useful in your work; all feedback will be greatly appreciated.</p>
<p><strong>Note:</strong> The implementation presented below is limited to classes derived (possibly indirectly) from <code>MarshalByRefObject</code>.</p>
<p><strong>Update 2009-12-14:</strong> <a href="http://il.linkedin.com/pub/dima-gershman/3/a23/4b1">Dima Gershman</a>, a truly knowledgeable colleague of mine, says go with the looks. Policy Injection wraps user objects with Interception&#8217;s transparent proxies, relieving us of the burden of manual configuration (and even improving performance!), although that comes at the price of some extra library dependencies. <strong>To be continued&#8230;</strong></p>
<p><span id="more-7"></span><a name="project"></a></p>
<h2>Project</h2>
<p>We’ll start by creating a Class Library project and adding the following reference to it:</p>
<ul>
<li><code>Microsoft.Practices.Unity.Interception</code></li>
</ul>
<p>Quite a small dependency toll, isn’t it? Using our library will require some additional references (depending on the configuration strategy), which will be detailed below.</p>
<p><strong>Note:</strong> Although the assembly name is <code>Microsoft.Practices.Unity.Interception.dll</code>, the namespace is <code>Microsoft.Practices.Unity.InterceptionExtension</code>. This significant source of confusion (especially considering the lack of configuration editor support) will hopefully be eliminated in EntLib 5.0.<br />
<a name="matchingrules"></a></p>
<p><strong>Update 2009-12-12:</strong> Removed unneeded reference to <code>Microsoft.Practices.Unity</code>.</p>
<h2>Matching Rules</h2>
<p>Let’s have another look at the desired syntax:</p>
<p><pre class="brush: csharp; gutter: false;">[NotifyPropertyChanged]
class Class1 : MarshalByRefObject, INotifyPropertyChanged
{
    public int MyProperty { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
}
</pre></p>
<p>NotifyPropertyChangedAttribute is the easiest part:</p>
<p><pre class="brush: csharp;">[Serializable, AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class NotifyPropertyChangedAttribute : Attribute { }
</pre></p>
<p><strong>Update 2009-12-19:</strong> Enabled inheritance.</p>
<p>Next, we’ll need to specify the rules to be used to intercept the methods (or setters, in our case), namely</p>
<ol>
<li>public setters</li>
<li>defined in types decorated with <code>NotifyPropertyChangedAttribute</code>.</li>
</ol>
<p>The following matching rule types are provided out of the box:</p>
<ul>
<li><code>AssemblyMatchingRule</code></li>
<li><code>AttributeDrivenPolicyMatchingRule</code></li>
<li><code>CustomAttributeMatchingRule</code></li>
<li><code>MemberNameMatchingRule</code></li>
<li><code>MethodSignatureMatchingRule</code></li>
<li><code>NamespaceMatchingRule</code></li>
<li><code>ParameterTypeMatchingRule</code></li>
<li><code>PropertyMatchingRule</code></li>
<li><code>ReturnTypeMatchingRule</code></li>
<li><code>TagAttributeMatchingRule</code></li>
<li><code>TypeMatchingRule</code></li>
</ul>
<p><code>PropertyMatchingRule</code> is perfect for the first rule; we’ll simply specify <code>*</code> as <code>propertyName</code> and <code>Set</code> as <code>option</code>.</p>
<p>The second rule is a bit trickier.</p>
<p>Let’s look at <code>TagAttributeMatchingRule</code>. This class matches <code>TagAttribute</code> (duh!):</p>
<p><pre class="brush: csharp; gutter: false;">[Tag(&quot;NotifyPropertyChanged&quot;)]
class Class1 : MarshalByRefObject, INotifyPropertyChanged
</pre></p>
<p>However, such approach is prone to typos, since constants cannot be used as attribute parameters.</p>
<p><code>AttributeDrivenPolicyMatchingRule</code> matches method parameters:</p>
<p><pre class="brush: csharp; gutter: false;">public int MyProperty
{
    get;
    [Notify] set;
}
</pre></p>
<p>which is somewhat closer to our requirement, but is still error-prone. Ideally, we&#8217;d decorate the entire class with our custom attribute and leave the auto-implemented properties, well, auto-implemented.</p>
<p>This calls for a mix between <code>CustomAttributeMatchingRule</code>, <code>TagAttributeMatchingRule</code> and <code>TypeMatchingRule</code>. How does one interbreed between three different species? Luckily, we’re not in the field of biology, so this is fairly easy:</p>
<p><pre class="brush: csharp;">public class CustomTypeAttributeMatchingRule : IMatchingRule
{
    private readonly Type attributeType;
    private readonly bool inherited;

    public CustomTypeAttributeMatchingRule(Type attributeType, bool inherited)
    {
        if (attributeType == null)
            throw new ArgumentNullException(&quot;attributeType&quot;);
        if (!attributeType.IsSubclassOf(typeof(Attribute)))
            throw new ArgumentException(&quot;The specified type is not a subclass of Attribute&quot;, &quot;attributeType&quot;);

        this.attributeType = attributeType;
        this.inherited = inherited;
    }

    public bool Matches(MethodBase member)
    {
        if (member == null)
            throw new ArgumentNullException(&quot;member&quot;);

        object[] attributes = member.ReflectedType.GetCustomAttributes(attributeType, inherited);
        return attributes != null &amp;&amp; attributes.Length &gt; 0;
    }
}
</pre></p>
<p>The most surprising to me was the fact that nobody (starting with Microsoft) had implemented this beforehand. This is mostly a copy-and-paste job, so I can’t really claim copyright for the implementation, just for the idea <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<a name="callhandler"></a></p>
<h2>Call Handler</h2>
<p>Now that we have the matching rules defined, let’s proceed to the call handler:</p>
<p><pre class="brush: csharp;">public class NotifyPropertyChangedCallHandler : ICallHandler</pre></p>
<p>Implementing the interface methods:</p>
<p><pre class="brush: csharp; first-line: 3;">public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
    bool shouldRaise = ShouldRaiseEvent(input);
    IMethodReturn res = getNext()(input, getNext);
    if (res.Exception == null &amp;&amp; shouldRaise)
        RaiseEvent(input);
    return res;
}

public int Order { get; set; }
</pre></p>
<p>The event isn’t raised if an exception has been thrown by the setter.</p>
<p><pre class="brush: csharp; first-line: 14;">private bool ShouldRaiseEvent(IMethodInvocation input)
{
    MethodBase methodBase = input.MethodBase;
    if (!methodBase.IsSpecialName || !methodBase.Name.StartsWith(&quot;set_&quot;))
        return false;

    string propertyName = methodBase.Name.Substring(4);
    PropertyInfo property = methodBase.ReflectedType.GetProperty(propertyName);
    MethodInfo getMethod = property.GetGetMethod();
    if (getMethod == null)
        return false;
    object oldValue = getMethod.Invoke(input.Target, null);
    object value = input.Arguments[0];

    return value == null ?
        value != oldValue :
        !value.Equals(oldValue);
}</pre></p>
<p>First, we make sure that we are dealing with a setter (this part is borrowed from <a href="http://www.ctrl-shift-b.com/2009/03/implementing-inotifyproperychanged-with.html">Derek</a>’s class). Then, we use reflection to check that the new value is accessible via a public getter (check added 2009-12-14) and is actually different from the old one. The ternary operator takes care of unboxing.</p>
<p><pre class="brush: csharp; first-line: 33;">private void RaiseEvent(IMethodInvocation input)
{
    FieldInfo field = null;
    for (Type type = input.methodBase.ReflectedType;
        (field = type.GetField(&quot;PropertyChanged&quot;, BindingFlags.Instance | BindingFlags.NonPublic)) == null;
        type = type.BaseType)
        ;

    MulticastDelegate evt = field.GetValue(input.Target) as MulticastDelegate;
    if (evt != null)
    {
        string propertyName = input.MethodBase.Name.Substring(4);
        evt.DynamicInvoke(input.Target, new PropertyChangedEventArgs(propertyName));
    }
}
</pre></p>
<p>In order to raise the event, we walk up the inheritance chain in search for the event’s field, cast it to <code>MulticastDelegate</code> and dynamically invoke the delegates.</p>
<p>This concludes our multicast for today. Since design-time configuration calls for a lengthy explanation, we’ll deal with usage in a separate article.</p>
<p><strong>Update 2009-12-14:</strong> Added check for public getter.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fshecht.wordpress.com%2f2009%2f12%2f12%2finotifypropertychanged-with-unity-interception-aop%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fshecht.wordpress.com%2f2009%2f12%2f12%2finotifypropertychanged-with-unity-interception-aop%2f" border="0" alt="kick it on DotNetKicks.com" /></a><a rev="vote-for" href="http://dotnetshoutout.com/INotifyPropertyChanged-with-Unity-Interception-AOP-Dmitry-Shechtmans-Blog"> <img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F12%2Finotifypropertychanged-with-unity-interception-aop%2F" style="border:0;height:18px;" height="19" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shecht.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shecht.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shecht.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shecht.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shecht.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shecht.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shecht.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shecht.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shecht.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shecht.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shecht.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shecht.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shecht.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shecht.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=7&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shecht.wordpress.com/2009/12/12/inotifypropertychanged-with-unity-interception-aop/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65b02c72404b4e3bad4a026e275f089e?s=96&#38;d=identicon&#38;r=R" medium="image">
			<media:title type="html">shecht</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fshecht.wordpress.com%2f2009%2f12%2f12%2finotifypropertychanged-with-unity-interception-aop%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>

		<media:content url="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fshecht.wordpress.com%2F2009%2F12%2F12%2Finotifypropertychanged-with-unity-interception-aop%2F" medium="image">
			<media:title type="html">Shout it</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello, World!</title>
		<link>http://shecht.wordpress.com/2009/12/12/hello-world/</link>
		<comments>http://shecht.wordpress.com/2009/12/12/hello-world/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 12:00:46 +0000</pubDate>
		<dc:creator>Dmitry Shechtman</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Welcome to my blog. I hope you find it useful. Let me know anyway.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=1&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Welcome to my blog. I hope you find it useful. Let me know anyway.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shecht.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shecht.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shecht.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shecht.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/shecht.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/shecht.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/shecht.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/shecht.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shecht.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shecht.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shecht.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shecht.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shecht.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shecht.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shecht.wordpress.com&amp;blog=10922844&amp;post=1&amp;subd=shecht&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://shecht.wordpress.com/2009/12/12/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/65b02c72404b4e3bad4a026e275f089e?s=96&#38;d=identicon&#38;r=R" medium="image">
			<media:title type="html">shecht</media:title>
		</media:content>
	</item>
	</channel>
</rss>
