<?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/"
	>

<channel>
	<title>Ticklish Techs &#187; example</title>
	<atom:link href="http://www.ticklishtechs.net/tag/example/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ticklishtechs.net</link>
	<description>a mostly .NET but also some other cool techs blog</description>
	<lastBuildDate>Wed, 05 Jan 2011 21:44:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Windows Live Writer Plugin for &lt;code&gt; in WordPress</title>
		<link>http://www.ticklishtechs.net/2008/02/21/windows-live-writer-plugin-for-code-in-wordpress/</link>
		<comments>http://www.ticklishtechs.net/2008/02/21/windows-live-writer-plugin-for-code-in-wordpress/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 11:55:17 +0000</pubDate>
		<dc:creator>Benjamin Schröter</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[wlw]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2008/02/21/windows-live-writer-plugin-for-code-in-wordpress/</guid>
		<description><![CDATA[I wrote the last posts (and also this one) with Windows Live Writer (aka. WLW). It is a very powerful offline client for writing posts to many different blog systems. Although it is a Microsoft software you can post to WordPress blogs. But I was missing one function I&#8217;m using very often when I write [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote the last posts (and also this one) with <a href="http://windowslivewriter.spaces.live.com">Windows Live Writer</a> (aka. WLW). It is a very powerful offline client for writing posts to many different blog systems. Although it is a Microsoft software you can post to <a href="http://wordpress.org/">WordPress</a> blogs.</p>
<p>But I was missing one function I&#8217;m using very often when I write posts on programming. There is no way in the WYSIWYG editor of WLW to format Text <code>like this</code>. In WordPress you can use the &lt;<code>code&gt;&lt;/code&gt; </code>tags for code-like formatting. But in WLW I had to switch to the <em>HTML Code</em> view to add these tags.</p>
<p>Since there is a good API to add features to WLW I tried to write my first Windows Live Write Plugin:</p>
<pre class="code">[<span style="color: #2b91af">WriterPlugin</span>(<span style="color: #a31515">&quot;{52D29C0E-49E6-4353-B58C-458F86CA1E2B}&quot;</span>,
                                           <span style="color: #a31515">&quot;Inline Code Plugin&quot;</span>)]
[<span style="color: #2b91af">InsertableContentSource</span>(<span style="color: #a31515">&quot;Inline Code&quot;</span>)]
<span style="color: blue">public class </span><span style="color: #2b91af">WlwInlineCodePlugin </span>: <span style="color: #2b91af">ContentSource
</span>{
    <span style="color: blue">public override </span><span style="color: #2b91af">DialogResult </span>CreateContent
                (<span style="color: #2b91af">IWin32Window </span>dialogOwner, <span style="color: blue">ref string </span>newContent)
    {
        <span style="color: #2b91af">InputBox </span>box = <span style="color: blue">new </span><span style="color: #2b91af">InputBox</span>(<span style="color: #a31515">&quot;Insert inline code&quot;</span>,                          <span style="color: #a31515">&quot;Insert the following in a &lt;code&gt; block:&quot;</span>,<span style="color: #a31515">&quot;&quot;</span>);
        <span style="color: #2b91af">DialogResult </span>result = box.ShowDialog(dialogOwner);

        <span style="color: blue">if </span>(result == <span style="color: #2b91af">DialogResult</span>.OK)
            newContent = <span style="color: #a31515">&quot;&lt;code&gt;&quot; </span>+ box.Input + <span style="color: #a31515">&quot;&lt;/code&gt;&quot;</span>;

        <span style="color: blue">return </span>result;
    }
}</pre>
<p>That&#8217;s really all the code (besides the <code>InputBox</code>. Just add the two attributes to a class and override the <code>CreateContent()</code> method. The code in this method is straight forward: Show some dialog, create the new HTML content and return the <code>DialogResult</code>. </p>
<p>After compiling you have to put the dll into the <code>Plugin</code> directory of your WLW installation and you will see a new entry in the <em>Insert </em>menu and on the sidebar. This entry is called &quot;Insert Inline Code&#8230;&quot; (ah, the text from the attribute goes there). When you click it a small dialog asks for the text and if you exit the dialog with the OK button this text is added to your document inside of &lt;<code>Code&gt; </code>tags.</p>
<p>In the zip file you will find the source code and the compiled dll.</p>
<div class="wlWriterSmartContent" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:e23385e6-bfd8-4d82-9d42-62b38f1a2af0" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<p> <a href="http://www.ticklishtechs.net/wp-content/uploads/2008/02/wlwcodeplugin.zip">The source code and the plugin</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2008/02/21/windows-live-writer-plugin-for-code-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First steps on aspect oriented programming</title>
		<link>http://www.ticklishtechs.net/2008/01/25/first-steps-on-aspect-oriented-programming/</link>
		<comments>http://www.ticklishtechs.net/2008/01/25/first-steps-on-aspect-oriented-programming/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 23:43:55 +0000</pubDate>
		<dc:creator>Benjamin Schröter</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AOP]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[PostSharp]]></category>

		<guid isPermaLink="false">http://www.ticklishtechs.net/2008/01/25/first-steps-on-aspect-oriented-programming/</guid>
		<description><![CDATA[Since I saw the first implementation of aspect oriented programming (AOP) frameworks for java I became a fan of AOP but it took a long time till now before I could start using AOP in my projects. The main reason was that simply no good and easy to use AOP framework for .net existed. There [...]]]></description>
			<content:encoded><![CDATA[<p>Since I saw the first implementation of aspect oriented programming (<a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">AOP</a>) frameworks for java I became a fan of AOP but it took a long time till now before I could start using AOP in my projects. The main reason was that simply no good and easy to use AOP framework for .net existed. There was one research project but you weren’t allowed to use their utilities in project other then research projects.<br />
But a few weeks ago I discovered <a href="http://www.postsharp.org/">PostSharp</a> and it is really great and very simple. If you have no idea what’s all about AOP you should read the <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">Wikipedia article</a> or the <a href="http://www.postsharp.org/">start page</a> of the PostSharp project. There you find a short example showing the usage of an aspect in C# code.<br />
I also want to show you my first experience with some aspect oriented programming.</p>
<p><strong>First: the problem I want to address</strong><br />
I sounds very simple: I’ve got a base-class and some sub-classes. All provide a method <code>GetName()</code>. All sub-classes within the same type should return the same name that is given at compile time. It&#8217;s just a const string for each class. The following code addresses these needs in a straight forward way:<br />
<!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 class\cf0  \cf4 BaseClass\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 virtual\cf0  \cf1 string\cf0  GetName()\par ??    \{\par ??        \cf1 return\cf0  \cf5 "-"\cf0 ;\par ??    \}\par ??\}\par ??\par ??\cf1 class\cf0  \cf4 ClassA\cf0  : \cf4 BaseClass\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 override\cf0  \cf1 string\cf0  GetName()\par ??    \{\par ??        \cf1 return\cf0  \cf5 "A"\cf0 ;\par ??    \}\par ??\}\par ??\par ??\cf1 class\cf0  \cf4 ClassB\cf0  : \cf4 BaseClass\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 override\cf0  \cf1 string\cf0  GetName()\par ??    \{\par ??        \cf1 return\cf0  \cf5 "b"\cf0 ;\par ??    \}\par ??\}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">BaseClass</span></p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> GetName()</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">&#8220;-&#8221;</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">}</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">ClassA</span> : <span style="color: #2b91af;">BaseClass</span></p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">string</span> GetName()</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">&#8220;A&#8221;</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">}</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">ClassB</span> : <span style="color: #2b91af;">BaseClass</span></p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">string</span> GetName()</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: #a31515;">&#8220;b&#8221;</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">}</p>
</div>
<p><strong>Second: what I don’t like with this solution</strong><br />
This seems good, but I’ve got some more special needs. All classes are generated by the <a href="http://msdn2.microsoft.com/en-us/vstudio/aa718368.aspx">DSL Tools</a> that means they contain much other code and no line written by me. In my use case these classes are <a href="http://msdn2.microsoft.com/en-us/library/microsoft.visualstudio.modeling.diagrams.shapeelement.aspx">shape-objects</a>.<br />
Thanks to the <code>partial</code> keyword in C# I could extend every class within its own .cs-file, but I didn’t like this. It would mean I have to create many additional .cs-files only for providing a single string to the class. But all other definition is stored in the DSL-model described by the DSL Tools and some part of the shape declaration is located in the .cs-files.<br />
In the DSL-model I can provide each class with a custom attribute, so I’m looking for a solution with no additional code in the sub-classes besides a custom attribute.<br />
With the use of reflection it could be the following:<br />
<!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 class\cf0  \cf4 NameTextAttribute\cf0  : \cf4 Attribute\par ??\cf0 \{\par ??    \cf1 public\cf0  NameTextAttribute(\cf1 string\cf0  name)\par ??    \{\par ??        \cf1 this\cf0 .Name = name;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  Name \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\}\par ??\par ??\cf1 class\cf0  \cf4 BaseClass\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 string\cf0  name = \cf1 null\cf0 ;\par ??\par ??    \cf1 public\cf0  \cf1 virtual\cf0  \cf1 string\cf0  GetName()\par ??    \{\par ??        \cf1 if\cf0  (name == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 object\cf0 [] a = \cf1 this\cf0 .GetType().GetCustomAttributes(\par ??                            \cf1 typeof\cf0 (\cf4 NameTextAttribute\cf0 ), \par ??                            \cf1 true\cf0 );\par ??\par ??            \cf1 if\cf0  (a.Length &gt; 0)\par ??                name = ((\cf4 NameTextAttribute\cf0 )a[0]).Name;\par ??            \cf1 else\par ??\cf0                 name = \cf5 "-"\cf0 ;\par ??        \}\par ??\par ??        \cf1 return\cf0  name;\par ??    \}\par ??\}\par ??\par ??[\cf4 NameTextAttribute\cf0 (\cf5 "A"\cf0 )]\par ??\cf1 class\cf0  \cf4 ClassA\cf0  : \cf4 BaseClass\cf0  \{\}\par ??\par ??[\cf4 NameTextAttribute\cf0 (\cf5 "b"\cf0 )]\par ??\cf1 class\cf0  \cf4 ClassB\cf0  : \cf4 BaseClass\cf0  \{\}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">NameTextAttribute</span> : <span style="color: #2b91af;">Attribute</span></p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> NameTextAttribute(<span style="color: blue;">string</span> name)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.Name = name;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px; padding:0px;">}</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">BaseClass</span></p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> name = <span style="color: blue;">null</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> GetName()</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (name == <span style="color: blue;">null</span>)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">object</span>[] a = <span style="color: blue;">this</span>.GetType().GetCustomAttributes(</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">typeof</span>(<span style="color: #2b91af;">NameTextAttribute</span>), </p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">true</span>);</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (a.Length &gt; 0)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; name = ((<span style="color: #2b91af;">NameTextAttribute</span>)a[0]).Name;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span></p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; name = <span style="color: #a31515;">&#8220;-&#8221;</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> name;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">}</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">[<span style="color: #2b91af;">NameTextAttribute</span>(<span style="color: #a31515;">"A"</span>)]</p>
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">ClassA</span> : <span style="color: #2b91af;">BaseClass</span> {}</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">[<span style="color: #2b91af;">NameTextAttribute</span>(<span style="color: #a31515;">"b"</span>)]</p>
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">ClassB</span> : <span style="color: #2b91af;">BaseClass</span> {}</p>
</div>
<p>This is working and as you see, no additional code is needed in the sub-classes. But nevertheless I would like to avoid using reflection. Reflection seems to me inappropriate in this case.</p>
<p><strong>Third: Using AOP</strong><br />
The idea of AOP is to add code to the class that you can write in another position, in the aspect. The aspect weaver (in my case the PostSharp Laos library) adds this code on compiletime (not runtime!) to the class.<br />
My strategy is to add some code after the constructor execution that will initialize the name-property:<br />
<!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 class\cf0  \cf4 BaseClass\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 virtual\cf0  \cf1 string\cf0  Name \{ \cf1 get\cf0 ; \cf1 internal\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf1 virtual\cf0  \cf1 string\cf0  GetName()\par ??    \{\par ??        \cf1 return\cf0  \cf1 this\cf0 .Name;\par ??    \}\par ??\}\par ??\par ??[\cf4 NameAspect\cf0 (\cf5 "a"\cf0 )]\par ??\cf1 class\cf0  \cf4 ClassA\cf0  : \cf4 BaseClass\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf1 void\cf0  someMethodInA()   \{ \}\par ??\}\par ??\par ??[\cf4 NameAspect\cf0 (\cf5 "b"\cf0 )]\par ??\cf1 class\cf0  \cf4 ClassB\cf0  : \cf4 BaseClass\cf0  \{\}\par ??\par ??[\cf4 Serializable\cf0 ]\par ??\cf1 class\cf0  \cf4 NameAspect\cf0  : \cf4 OnMethodBoundaryAspect\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 string\cf0  name = \cf5 "-"\cf0 ;\par ??    \cf1 public\cf0  NameAspect(\cf1 string\cf0  name)\par ??    \{\par ??        \cf1 this\cf0 .name = name;\par ??    \}\par ??\par ??    \cf1 override\cf0  \cf1 bool\cf0  CompileTimeValidate(\cf4 MethodBase\cf0  m)\par ??    \{\par ??        \cf1 return\cf0  m.IsConstructor;\par ??    \}\par ??\par ??    \cf1 override\cf0  \cf1 void\cf0  OnSuccess(\cf4 MethodExecutionEventArgs\cf0  args)\par ??    \{\par ??        \cf4 BaseClass\cf0  obj = args.Instance \cf1 as\cf0  \cf4 BaseClass\cf0 ;\par ??        \cf1 if\cf0  (obj != \cf1 null\cf0 )\par ??            obj.Name = name;\par ??    \}\par ??\}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">BaseClass</span></p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> Name { <span style="color: blue;">get</span>; <span style="color: blue;">internal</span> <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> GetName()</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>.Name;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">}</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">[<span style="color: #2b91af;">NameAspect</span>(<span style="color: #a31515;">"a"</span>)]</p>
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">ClassA</span> : <span style="color: #2b91af;">BaseClass</span></p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> someMethodInA()&nbsp;&nbsp; { }</p>
<p style="margin: 0px; padding:0px;">}</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">[<span style="color: #2b91af;">NameAspect</span>(<span style="color: #a31515;">"b"</span>)]</p>
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">ClassB</span> : <span style="color: #2b91af;">BaseClass</span> {}</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">[<span style="color: #2b91af;">Serializable</span>]</p>
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">NameAspect</span> : <span style="color: #2b91af;">OnMethodBoundaryAspect</span></p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> name = <span style="color: #a31515;">&#8220;-&#8221;</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> NameAspect(<span style="color: blue;">string</span> name)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">this</span>.name = name;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">override</span> <span style="color: blue;">bool</span> CompileTimeValidate(<span style="color: #2b91af;">MethodBase</span> m)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> m.IsConstructor;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">override</span> <span style="color: blue;">void</span> OnSuccess(<span style="color: #2b91af;">MethodExecutionEventArgs</span> args)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">BaseClass</span> obj = args.Instance <span style="color: blue;">as</span> <span style="color: #2b91af;">BaseClass</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (obj != <span style="color: blue;">null</span>)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; obj.Name = name;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">}</p>
</div>
<p>The base-class now provides a property <code>name</code> (declared with the new C# 3.0 syntax) and the <code>GetName()</code> method returning the property value. As in the last example the sub-classes have the attribute declared and contain no other code.<br />
The magic happens in the attribute (or, if you like, somewhere in the PostSharp.dll). It is derived from <code>OnMethodBoundaryAspect</code>, an aspect base type where I can add code to a method in another class <em>before</em> and <em>after</em> the method call and <em>on exception</em> and <em>on success</em> of the method call.<br />
My constructor of this aspect gets only the name string and stores it in a variable.<br />
The code that should be executed every time a constructor of a class with this aspect is calls can be find in the <code>OnSuccess()</code>-method. Here only the property of the <code>BaseClass</code> will be set.<br />
With the <code>CompileTimeValidate()</code>-method I can specify at compiletime for witch methods the code should be injected. For my needs this is only the constructor. All other methods of my class will not be changes by this aspect.</p>
<p><strong>Fourth: behind the scene</strong><br />
With adding the PostSharp references to my project the PostSharp postcompiler is called automatically after compiling my assembly and the code is added. If you take a look with the <a href="http://www.aisto.com/roeder/dotnet/">Reflector</a> you will see the following:<br />
<!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 class\cf0  \cf4 ClassA\cf0  : BaseClass\par ??\{\par ??    \cf1 static\cf0  ClassA()\par ??    \{\par ??        \cf1 if\cf0  (!~PostSharp~Laos~Implementation.initialized)\par ??            \cf4 LaosNotInitializedException\cf0 .Throw();\par ??        ~PostSharp~Laos~Implementation.~targetMethod~3 = \par ??                methodof(\cf4 ClassA\cf0 ..ctor);\par ??        ~PostSharp~Laos~Implementation.NameAspect~3.RuntimeInitialize\par ??                (~PostSharp~Laos~Implementation.~targetMethod~3);\par ??    \}\par ??\par ??    \cf1 public\cf0  ClassA()\par ??    \{\par ??        \cf4 MethodExecutionEventArgs\cf0  ~laosEventArgs~1;\par ??        \cf1 try\par ??\cf0         \{\par ??            ~laosEventArgs~1 = \cf1 new\cf0  \cf4 MethodExecutionEventArgs\cf0 (\par ??                    methodof(\cf4 ClassA\cf0 ..ctor, \cf4 ClassA\cf0 ), \cf1 this\cf0 , \cf1 null\cf0 );\par ??            ~PostSharp~Laos~Implementation.NameAspect~3.OnEntry(~laosEventArgs~1);\par ??            \cf1 if\cf0  (~laosEventArgs~1.FlowBehavior != \cf4 FlowBehavior\cf0 .Return)\par ??                ~PostSharp~Laos~Implementation.NameAspect~3.OnSuccess(~laosEventArgs~1);\par ??        \}\par ??        \cf1 catch\cf0  (\cf4 Exception\cf0  ~exception~0)\par ??        \{\par ??            ~laosEventArgs~1.Exception = ~exception~0;\par ??            ~PostSharp~Laos~Implementation.NameAspect~3.OnException(~laosEventArgs~1);\par ??            \cf1 switch\cf0  (~laosEventArgs~1.FlowBehavior)\par ??            \{\par ??                \cf1 case\cf0  \cf4 FlowBehavior\cf0 .Continue:\par ??                \cf1 case\cf0  \cf4 FlowBehavior\cf0 .Return:\par ??                    \cf1 return\cf0 ;\par ??            \}\par ??            \cf1 throw\cf0 ;\par ??        \}\par ??        \cf1 finally\par ??\cf0         \{\par ??            ~PostSharp~Laos~Implementation.NameAspect~3.OnExit(~laosEventArgs~1);\par ??        \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  someMethodInA() \{\}\par ??\}}<br />
--></p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px; padding:0px;"><span style="color: blue;">class</span> <span style="color: #2b91af;">ClassA</span> : BaseClass</p>
<p style="margin: 0px; padding:0px;">{</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">static</span> ClassA()</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (!~PostSharp~Laos~Implementation.initialized)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">LaosNotInitializedException</span>.Throw();</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ~PostSharp~Laos~Implementation.~targetMethod~3 = </p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; methodof(<span style="color: #2b91af;">ClassA</span>..ctor);</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ~PostSharp~Laos~Implementation.NameAspect~3.RuntimeInitialize</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; (~PostSharp~Laos~Implementation.~targetMethod~3);</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> ClassA()</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">MethodExecutionEventArgs</span> ~laosEventArgs~1;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">try</span></p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ~laosEventArgs~1 = <span style="color: blue;">new</span> <span style="color: #2b91af;">MethodExecutionEventArgs</span>(</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; methodof(<span style="color: #2b91af;">ClassA</span>..ctor, <span style="color: #2b91af;">ClassA</span>), <span style="color: blue;">this</span>, <span style="color: blue;">null</span>);</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ~PostSharp~Laos~Implementation.NameAspect~3.OnEntry(~laosEventArgs~1);</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (~laosEventArgs~1.FlowBehavior != <span style="color: #2b91af;">FlowBehavior</span>.Return)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ~PostSharp~Laos~Implementation.NameAspect~3.OnSuccess(~laosEventArgs~1);</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">catch</span> (<span style="color: #2b91af;">Exception</span> ~exception~0)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ~laosEventArgs~1.Exception = ~exception~0;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ~PostSharp~Laos~Implementation.NameAspect~3.OnException(~laosEventArgs~1);</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">switch</span> (~laosEventArgs~1.FlowBehavior)</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> <span style="color: #2b91af;">FlowBehavior</span>.Continue:</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">case</span> <span style="color: #2b91af;">FlowBehavior</span>.Return:</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">throw</span>;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">finally</span></p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ~PostSharp~Laos~Implementation.NameAspect~3.OnExit(~laosEventArgs~1);</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px; padding:0px;">&nbsp;</p>
<p style="margin: 0px; padding:0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> someMethodInA() {}</p>
<p style="margin: 0px; padding:0px;">}</p>
</div>
<p>I really don’t know what’s that all about but I can see that there is code added only to the constructor, someMethed() is empty as in my given source code. </p>
<p>In the next weeks I will try to cover more examples and solutions with PostSharp and AOP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ticklishtechs.net/2008/01/25/first-steps-on-aspect-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

