<?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>All about nothing</title>
	<atom:link href="http://jachman.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jachman.wordpress.com</link>
	<description>.NET C#, Oracle, SQL Server and all other stuff that gives live a sense.</description>
	<lastBuildDate>Mon, 07 Sep 2009 14:12:12 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='jachman.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/183cf6b2da9aa2736778aaad115163d1?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>All about nothing</title>
		<link>http://jachman.wordpress.com</link>
	</image>
			<item>
		<title>Asynchronous WinForm UI</title>
		<link>http://jachman.wordpress.com/2009/09/07/asynchronous-winform-ui/</link>
		<comments>http://jachman.wordpress.com/2009/09/07/asynchronous-winform-ui/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 14:08:13 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Winforms]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=328</guid>
		<description><![CDATA[When you&#8217;re trying to create a WinForm Application that is working asynchronously, you&#8217;ll probably in a common failure: 
System.InvalidOperationException occurred
  Message="Cross-thread operation not valid: Control 'MainForm' accessed from a thread other than the thread it was created on."
  Source="System.Windows.Forms"


You&#8217;ll get this failure, because it&#8217;s not allowed to update a UI control from another [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=328&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>When you&#8217;re trying to create a WinForm Application that is working asynchronously, you&#8217;ll probably in a common failure: </p>
<blockquote><p><code>System.InvalidOperationException occurred<br />
  Message="Cross-thread operation not valid: Control 'MainForm' accessed from a thread other than the thread it was created on."<br />
  Source="System.Windows.Forms"<br />
</code>
</p></blockquote>
<p>You&#8217;ll get this failure, because it&#8217;s not allowed to update a UI control from another thread than the thread that has created the control.</p>
<p>A fluent way around this is to use the MethodInvoker class.<br />
Let&#8217;s asume that you have a statement like the following one:</p>
<blockquote><p><code>            appForm.Text = appInfo; </code></p></blockquote>
<p>Using the MethodInvoker class would look like:</p>
<blockquote><p><code>appForm.Invoke(new MethodInvoker(() =&gt; { appForm.Text = appInfo; }));</code></p></blockquote>
<p>That&#8217;s still a one-liner;-) And with that small hint, you&#8217;ll get around such annoying exceptions.<br />
Hope you enoy it.</p>
<p>Cheers<br />
- Gerhard</p>
<p>PS: Here&#8217;s also a great article about the <a href="http://blog.hackedbrain.com/archive/2005/05/03/397.aspx">MethodInvoker</a>. </p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f09%2f07%2fasynchronous-winform-ui%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f09%2f07%2fasynchronous-winform-ui%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/328/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/328/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/328/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=328&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/09/07/asynchronous-winform-ui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f09%2f07%2fasynchronous-winform-ui%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Give me a Mock ;)</title>
		<link>http://jachman.wordpress.com/2009/07/20/give-me-a-mock/</link>
		<comments>http://jachman.wordpress.com/2009/07/20/give-me-a-mock/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 20:14:52 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Useful Hints]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=309</guid>
		<description><![CDATA[Last days I stumpled upon a great Mockup Tool that creates really stunning results in a few minutes.
Our .NET User Group in Frankfurt invited me to a workshop where a new Mockup Tool called &#8220;Balsamiq&#8221; should be shown to the developers. Unfortunatly I wasn&#8217;t able to come due to my daughters birthyday. But the mail [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=309&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>Last days I stumpled upon a great Mockup Tool that creates really stunning results in a few minutes.</strong></p>
<p>Our .NET User Group in Frankfurt invited me to a workshop where a new Mockup Tool called &#8220;Balsamiq&#8221; should be shown to the developers. Unfortunatly I wasn&#8217;t able to come due to my daughters birthyday. But the mail our User Group leader wrote let my interest awake for that tool. So I decided to test it by my own, and I was luckily suprised by the ease of creating UI mockups. </p>
<p>Have a look at the following mockup that I created with Balsamiq within a few minutes, and than have a look at the original screen.</p>
<table>
<tr>
<td><a href="http://jachman.files.wordpress.com/2009/07/tpclient.png"><img src="http://jachman.files.wordpress.com/2009/07/tpclient.png?w=300&#038;h=182" alt="TpClient Mockup" title="TpClient Mockup" width="300" height="182" class="alignnone size-medium wp-image-311" /></a></td>
<td><a href="http://jachman.files.wordpress.com/2009/07/tpclientorg.png"><img src="http://jachman.files.wordpress.com/2009/07/tpclientorg.png?w=300&#038;h=181" alt="TpClient Original" title="TpClientOrg" width="300" height="181" class="alignnone size-medium wp-image-314" /></a></tr>
</table>
<p>I think you can&#8217;t deny the affinity of both screens-) Ok. The Mockup was made out of an existing application, but that was only because of my testing purpose. </p>
<p>In my testing I only found one thing that anoyes a little. It was that I accidently changed the size of the wrong UI element sometimes. But I found a way to fix that issue. You simple have to pin new UI elements in Balsamiq when you&#8217;re fine with it. With that feature you can prevent moving or resizing elements you don&#8217;t want to change.</p>
<p>So give that tool a chance. For me, I&#8217;m pretty sure that I will use it in my next project.</p>
<p>Cheers<br />
- Gerhard</p>
<p>BTW: Here&#8217;s the link to Balsamiq (<a href="http://www.balsamiq.com">http://www.balsamiq.com</a>)</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f07%2f20%2fgive-me-a-mock%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f07%2f20%2fgive-me-a-mock%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/309/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=309&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/07/20/give-me-a-mock/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://jachman.files.wordpress.com/2009/07/tpclient.png?w=300" medium="image">
			<media:title type="html">TpClient Mockup</media:title>
		</media:content>

		<media:content url="http://jachman.files.wordpress.com/2009/07/tpclientorg.png?w=300" medium="image">
			<media:title type="html">TpClientOrg</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f07%2f20%2fgive-me-a-mock%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>How to authorize a WCF service in the anonymous internet</title>
		<link>http://jachman.wordpress.com/2009/06/24/how-to-authorize-a-wcf-service-in-the-anonymous-internet/</link>
		<comments>http://jachman.wordpress.com/2009/06/24/how-to-authorize-a-wcf-service-in-the-anonymous-internet/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 19:19:48 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Anonymous]]></category>
		<category><![CDATA[Authentication]]></category>
		<category><![CDATA[Authorization]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Service]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=286</guid>
		<description><![CDATA[Sometimes I feel like in the time of exploration where I&#8217;m a discoverer &#8230; But than I always realize that I&#8217;m only a developer who tries to understand the mysteries of the Microsoft Framework &#8230;
Introduction:
I had to program an authorization mechanism between a client application and a webservice that is hosted by a internet provider. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=286&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometimes I feel like in the time of exploration where I&#8217;m a discoverer &#8230; But than I always realize that I&#8217;m only a developer who tries to understand the mysteries of the Microsoft Framework &#8230;</p>
<p><strong>Introduction:</strong><br />
I had to program an authorization mechanism between a client application and a webservice that is hosted by a internet provider. In such a case, we have two different domains in place, no windows integrated authentication, no certificate or something else. The old fashioned way in that case is to use a SoapHeader in order to send informations about the user to the web service &#8230; That&#8217;s the point where the trail starts &#8230; </p>
<p><strong>SoapHeader in WCF Services:</strong><br />
The new Microsoft Technology is not in love with SoapHeaders. Actually it does not know them. But fortunately there is a project on CodePlex called &#8220;<a href="http://wcfextras.codeplex.com/">WCFExtras</a>&#8221; which re-implemented the [SoapHeader] Attribute for WCF. So I created an Authentication class in the following way.</p>
<blockquote><p>
 [DataContract(Name=<font color='maroon'>&quot;AuthenticationHeader&quot;</font>)]<br />
<font color='blue'>public</font> <font color='blue'>class</font> AuthenticationHeader<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;[DataMember]<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>string</font> TpUser { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;[DataMember]<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>string</font> TpPwdMd5 { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;[DataMember]<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>string</font> Culture { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
}
</p></blockquote>
<p>.. And tagged my methods within the [ServiceContract] with the new [SoapHeader] attribute.</p>
<blockquote><p>[OperationContract]<br />
[SoapHeader(<font color='maroon'>&quot;Authentication&quot;</font>, <font color='blue'>typeof</font>(AuthenticationHeader))]<br />
UserStub CreateUser(<font color='blue'>string</font> logonName);
</p></blockquote>
<p>In order to use the [SoapHeader] attribute, I had to references the WCFExtras.dll in my project and within my web.config file in the behaviorExtensions.</p>
<blockquote><p> &lt;behaviors&gt;<br />
&lt;endpointBehaviors&gt;<br />
&lt;behavior name=<font color='maroon'>&quot;UserServiceEndpointBehaviors&quot;</font>&gt;<br />
&lt;wsdlExtensions/&gt;<br />
&lt;/behavior&gt;<br />
&lt;/endpointBehaviors&gt;<br />
&lt;/behaviors&gt;</p>
<p>&lt;extensions&gt;<br />
&lt;behaviorExtensions&gt;<br />
&lt;!&#8211; Declare that we have an extension called WSDL Extras&#8211;&gt;<br />
&lt;add name=<font color='maroon'>&quot;wsdlExtensions&quot;</font> type=<font color='maroon'>&quot;WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null;</font>/&gt;<br />
&lt;/behaviorExtensions&gt;<br />
&lt;/extensions&gt;</p></blockquote>
<p>At server side everything had been fine now.</p>
<p><strong>How to add the SoapHeader at the client side:</strong><br />
But on the client side, I had to send the SoapHeader over the wire whenever a method will be called. For that you have to know, that a SoapHeader is bound to the EndpointAddress of the WCF Service. The binding on the EndpointAddress can be done when creating the WebService via the ChannelFactory.</p>
<blockquote><p> var authentication = <font color='blue'>new</font> Dao.Core.TpAuthentication<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;TpUser = connection.UserName,<br />
&nbsp;&nbsp;&nbsp;&nbsp;TpPwdMd5 = Md5Crypt.ComputeHash(connection.UserPassword),<br />
&nbsp;&nbsp;&nbsp;&nbsp;Culture = Thread.CurrentThread.CurrentUICulture.Name<br />
};</p>
<p>var authenticationHeader = AddressHeader.CreateAddressHeader(<font color='maroon'>&quot;Authentication&quot;</font>, <font color='maroon'>&quot;&quot;</font>, authentication);<br />
var serviceUri = <font color='blue'>new</font> Uri(<font color='maroon'>&quot;http://localhost:81/tphost/UserService.svc&quot;</font>));</p>
<p>var basicHttpBinding = <font color='blue'>new</font> BasicHttpBinding(BasicHttpSecurityMode.None);<br />
var endpointAddress = <font color='blue'>new</font> EndpointAddress(serviceUri, <font color='blue'>new</font> [] { authenticationHeader});<br />
wst = <font color='blue'>new</font> ChannelFactory&lt;WebServiceTemplate&gt;(basicHttpBinding, endpointAddress).CreateChannel();
</p></blockquote>
<p>At this point in the service host I was able to read user information which have been sent, by the client using the SoapHeader.</p>
<p><strong>But how could I manage the Authentication in my service host?</strong><br />
It was my goal to use declarative and imperative authentication by using the <strong>Thread.CurrentPrincipal.IsInRole</strong> functionality. In order to use this functionality I had to create my own implementation of <strong>IPrincipal</strong> and <strong>IIdentity</strong>. I assume that you know how to implement this interfaces, because the implementation is mainly driven by your business logic. That&#8217;s why I jump directly to the interessting part, the AuthorizationPolicy. At server side I had to create a new class that inherits the <strong>IAuthorizationPolicy</strong> interface.</p>
<blockquote><p><font color='blue'>public</font> <font color='blue'>class</font> AuthorisationPolicy : IAuthorizationPolicy<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>string</font> id = Guid.NewGuid().ToString();<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>string</font> Id<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>get</font> { <font color='blue'>return</font> id; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> ClaimSet Issuer<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>get</font> { <font color='blue'>return</font> ClaimSet.System; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>bool</font> Evaluate(EvaluationContext context, <font color='blue'>ref</font> <font color='blue'>object</font> state)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// Extract the authentication</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var authentication = SoapHeaderHelper&lt;Authentication&gt;.GetInputHeader(<font color='maroon'>&quot;Authentication&quot;</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context.Properties[<font color='maroon'>&quot;Principal&quot;</font>] = <font color='blue'>new</font> CustomPrincipal(webServiceUser, <font color='blue'>new</font> CustomIdentity(webServiceUser));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> <font color='blue'>true</font>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</p></blockquote>
<p>The interface has only three methods. The property <strong>Id </strong>is only a unique identifier of your AuthorizationPolicy &#8211; so I did not care about. The Claimset <strong>Issuer</strong> is only used to define, if the System claims the authorization, or if Windows itself claims it. In my case &#8211; it&#8217;s always the System. The real magic is implemented within the Evalute method. </p>
<p>The first method reads the SoapHeader that contains the user information.</p>
<blockquote><p>var authentication = SoapHeaderHelper&lt;Authentication&gt;.GetInputHeader(<font color='maroon'>&quot;Authentication&quot;</font>);</p></blockquote>
<p>With this information I had been be able to authenticate the user. If the authentication fails, the method must return <strong>false</strong>. If not, a new Principal must be set into the <strong>context.Properties["Principal"]</strong>. The key &#8220;Principal&#8221; is pre-defined by Microsoft and the new content will instantly placed into the Thread.CurrentPrincipal. </p>
<p>So, that&#8217;s the magic. </p>
<p><strong>But who calls the AuthorizationPolicy?</strong></p>
<p>In order to make the new AuthorizationPolicy available to the WCF Framework, I had to to publish it in my [ServiceBehaviors] within the <strong>web.config</strong> file.</p>
<blockquote><p> &lt;serviceBehaviors&gt;<br />
&lt;behavior name=<font color='maroon'>&quot;UserServiceBehaviors&quot;</font> &gt;<br />
&lt;serviceMetadata httpGetEnabled=<font color='maroon'>&quot;true&quot;</font> /&gt;<br />
&lt;serviceDebug includeExceptionDetailInFaults=<font color='maroon'>&quot;true&quot;</font> /&gt;<br />
&lt;serviceAuthorization principalPermissionMode=<font color='maroon'>&quot;Custom&quot;</font>&gt;<br />
&lt;authorizationPolicies&gt;<br />
&lt;add policyType=<font color='maroon'>&quot;WebServices.Core.AuthorisationPolicy, WebServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&quot;</font> /&gt;<br />
&lt;/authorizationPolicies&gt;<br />
&lt;/serviceAuthorization&gt;<br />
&lt;/behavior&gt;<br />
&lt;/serviceBehaviors&gt;
</p></blockquote>
<p>So, that&#8217;s it. Knowing this, you are also able to write your own authorization process, in case you need it.</p>
<p>So enjoy this piece of code<br />
- Gerhard</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f24%2fhow-to-authorize-a-wcf-service-in-the-anonymous-internet%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f24%2fhow-to-authorize-a-wcf-service-in-the-anonymous-internet%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/286/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=286&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/06/24/how-to-authorize-a-wcf-service-in-the-anonymous-internet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f24%2fhow-to-authorize-a-wcf-service-in-the-anonymous-internet%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>You can only add project references to other Silverlight projects in the solution.</title>
		<link>http://jachman.wordpress.com/2009/06/18/you-can-only-add-project-references-to-other-silverlight-projects-in-the-solution/</link>
		<comments>http://jachman.wordpress.com/2009/06/18/you-can-only-add-project-references-to-other-silverlight-projects-in-the-solution/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 11:32:16 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=255</guid>
		<description><![CDATA[If you start with silverlight, I bet with you that you&#8217;ll get that anoying message in any case. But it has a reason why Microsoft doesn&#8217;t allow that. 
It&#8217;s because Silverlight owns a very tiny implementation of the .NET framework in order to reduce the download size for the user. So it&#8217;s very likely that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=255&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>If you start with silverlight, I bet with you that you&#8217;ll get that anoying message in any case. But it has a reason why Microsoft doesn&#8217;t allow that. </p>
<p>It&#8217;s because Silverlight owns a very tiny implementation of the .NET framework in order to reduce the download size for the user. So it&#8217;s very likely that your project uses a method which is not supported by the .NET silverlight framework. But how can we get arround this irritating message? The question is easy, but although I&#8217;m working day by day with Visual Studio, I did not recognize the functionality until <a href="http://www.netfxharmonics.com/2008/12/Reusing-NET-Assemblies-in-Silverlight">Davit Betz</a> showed me that egg of Columbus. </p>
<p>First of all you have to create a silverlight project. In my case I named it like the project I wanted to use and added a &#8220;.Silverlight&#8221; at the end of the project name. (e.g. Utils.Silverlight). After that you create the folders as in your original project.</p>
<p>And now the magic: Add the files by clicking to &#8220;Add existing item&#8221; and use &#8220;Add Link&#8221; (instead of simply Add).<br />
<img src="http://jachman.files.wordpress.com/2009/06/addlink2.gif?w=578&#038;h=250" alt="AddLink" title="AddLink" width="578" height="250" class="alignnone size-full wp-image-260" /></p>
<p>As a result the files will only be referenced by the new project, but not copied into the folder physically. With that feature, you can build your silverlight assembly without duplicating the sources. And I think you&#8217;ll wonder how many changes you need &#8230; </p>
<p>So remember, with every change here &#8211; you&#8217;ll probably break your original assembly there. In order to prevent breaking the code, you can use the precompiler. In every Silverlight Assembly the SILVERLIGHT Symbol is predefined.</p>
<p>So use, #if SILVERLIGHT &#8230; #endif to encapsulate the code that is only interessting for your silverlight application.</p>
<p>May the code be with you<br />
- Gerhard</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f18%2fyou-can-only-add-project-references-to-other-silverlight-projects-in-the-solution%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f18%2fyou-can-only-add-project-references-to-other-silverlight-projects-in-the-solution%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/255/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=255&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/06/18/you-can-only-add-project-references-to-other-silverlight-projects-in-the-solution/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://jachman.files.wordpress.com/2009/06/addlink2.gif" medium="image">
			<media:title type="html">AddLink</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f18%2fyou-can-only-add-project-references-to-other-silverlight-projects-in-the-solution%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>How to manage Control States in Silverlight Applications</title>
		<link>http://jachman.wordpress.com/2009/06/13/how-to-manage-control-states-in-silverlight-applications/</link>
		<comments>http://jachman.wordpress.com/2009/06/13/how-to-manage-control-states-in-silverlight-applications/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 09:25:15 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[Controls]]></category>
		<category><![CDATA[Rules]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=219</guid>
		<description><![CDATA[In 2006 I developed a small piece of code in order to use a declarative control state handling in WinForm Applications. Three years later (2009) I thought declarative control state handling should be standard in Silverlight &#8211; but I failed. So I decided to migrate that piece of code to the new Silverlight plattform. 
Imagine [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=219&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>In 2006 I developed a small piece of code in order to use a declarative <a href="http://jachman.wordpress.com/2006/11/29/how-to-manage-control-states-in-winform-applications/">control state handling in WinForm Applications</a>. Three years later (2009) I thought declarative control state handling should be standard in Silverlight &#8211; but I failed. So I decided to migrate that piece of code to the new Silverlight plattform. </strong></p>
<p>Imagine you have a really big application &#8211; How would you ensure the control state of every control? In former times this had been done in endless &#8220;if &#8211; then &#8211; else&#8221; horror code blocks. That time is history now. A better solution is to use a configuration file and define rules to enable/disable and visible/collapse controls.</p>
<p>Before you read: <a href="http://www.ad-factum.de/wordpress/silverlightControlState/" target="_BLANK">Here&#8217;s an example application</a>.</p>
<p>Here&#8217;s the configuration file for the example:</p>
<blockquote><p>&lt;?xml version=<font color='maroon'>&quot;1.0&quot;</font> encoding=<font color='maroon'>&quot;utf-8&quot;</font>?&gt;<br />
&lt;uipConfig xmlns:xsi=<font color='maroon'>&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</font> xmlns:xsd=<font color='maroon'>&quot;http://www.w3.org/2001/XMLSchema&quot;</font>&gt;<br />
&lt;StateSections&gt;</p>
<p>&lt;section name=<font color='maroon'>&quot;Page&quot;</font>&gt;<br />
&lt;Elements&gt;<br />
&lt;add key=<font color='maroon'>&quot;txtAlwaysVisible&quot;</font> state=<font color='maroon'>&quot;True&quot;</font>/&gt;<br />
&lt;add key=<font color='maroon'>&quot;txtVisibleHidden&quot;</font> check=<font color='maroon'>&quot;IsVisible&quot;</font> changeVisibility=<font color='maroon'>&quot;true&quot;</font>/&gt;<br />
&lt;add key=<font color='maroon'>&quot;radEnabled&quot;</font> check=<font color='maroon'>&quot;IsRadioButtonEnabled&quot;</font> /&gt;<br />
&lt;add key=<font color='maroon'>&quot;radAdmin&quot;</font> roles=<font color='maroon'>&quot;Admin&quot;</font> /&gt;<br />
&lt;add key=<font color='maroon'>&quot;radEditor&quot;</font> roles=<font color='maroon'>&quot;Editor&quot;</font> /&gt;<br />
&lt;add key=<font color='maroon'>&quot;radBoth&quot;</font> roles=<font color='maroon'>&quot;Admin, Editor&quot;</font> /&gt;<br />
&lt;/Elements&gt;<br />
&lt;/section&gt;</p>
<p>&lt;/StateSections&gt;<br />
&lt;/uipConfig&gt;</p></blockquote>
<p>As you can see the uipConfig file is devided in Sections. A Section is a logical component that contains controls. In our example the Section is defined as the Silverlight Page. In the every section all controls that are interessting for us have a key that is used to define the rules. </p>
<p>Here are the allowed attributes:</p>
<table>
<tr>
<th>Attribute</th>
<th>Description</th>
</tr>
<tr>
<td><b>state</b></td>
<td>Can be set to true / false or can be left empty. If a state has been set, all other checks will be ignored.</td>
</tr>
<tr>
<td><b>roles</b></td>
<td>It&#8217;s a comma separated list with roles, the current principal must belong to. If the current principal does not belong to the role, the control will be disabled.</td>
</tr>
<tr>
<td><b>check</b></td>
<td>It&#8217;s a comma separated list with check routines that tells the State Manager if the control has to be disabled or enabled. Such a check routine can be implemented in a controller class or within the Page itself &#8211; and it&#8217;s more or less a property that returns true or false.</td>
</tr>
<tr>
<td><b>changeVisibility</b></td>
<td>This flag defines, if the visibility of the control shall be changed. If this flag has not been defined, the enabled/disabled state is used.</td>
</tr>
</table>
<p>All you have to do is to make the ControlStateExtender available to your silverlight application. Therefore you have to place the reference in your UserControl element of your page.</p>
<blockquote><p>&lt;UserControl x:Class=<font color='maroon'>&quot;SilverlightControlState.Page&quot;</font><br />
xmlns=<font color='maroon'>&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</font> <br />
xmlns:x=<font color='maroon'>&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</font> <br />
<strong>xmlns:state=<font color='maroon'>&quot;clr-namespace:SilverlightControlState.ControlState;assembly=SilverlightControlState&quot;</strong></font>
</p></blockquote>
<p>After that you can add the attribute <strong>sate:ControlStateExtender.ControlKey</strong> to any of your controls.</p>
<p>Example:</p>
<blockquote><p>&lt;TextBlock state:ControlStateExtender.ControlKey=<font color='maroon'>&quot;Page, txtVisibleHidden&quot;</font> &#8230;<br />
&lt;/TextBlock&gt;</p></blockquote>
<p>The first word defines the section, the second the key of the control within the section. Now &#8211; what do you have to change in code?</p>
<p>That&#8217;s pretty easy and also straight forward. You have to add a private readonly ControlStateExtender to your page. This object handles the state of your controls. Furthermore you have to initialize the ControlStateExtender object after the call of InitializeComponent().</p>
<blockquote><p><code><font color='blue'>public</font> <font color='blue'>partial</font> <font color='blue'>class</font> Page : UserControl<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>private</font> <font color='blue'>readonly</font> ControlStateExtender cse = ControlStateExtender.GetExtender(<font color='maroon'>&quot;Page&quot;</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;summary&gt; Constructor &lt;/summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> Page()<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InitializeComponent();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cse.Initialize(<font color='blue'>this</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
</code></p></blockquote>
<p>Now you can define the control state rules in your application.<br />
Example:</p>
<blockquote><p><code> #region Control State Rules</p>
<p><font color='blue'>public</font> <font color='blue'>bool</font> IsVisible<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>get</font><br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> radVisible.IsChecked.Value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</p>
<p><font color='blue'>private</font> <font color='blue'>bool</font> disableRadioButtons = <font color='blue'>false</font>;<br />
<font color='blue'>public</font> <font color='blue'>bool</font> IsRadioButtonEnabled<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>get</font><br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> !disableRadioButtons;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</p>
<p>#endregion<br />
</code></p></blockquote>
<p>The control state rules (business rules) will be invoked by the ControlStateManager when the Refresh() method is called. So, any time, you expect the control states to be changed you have to call the Refresh() method of the ControlStateManager to re-validate the rules.</p>
<p>Example:</p>
<blockquote><p><code> #region Handler that refrehes the control state</p>
<p><font color='blue'>private</font> <font color='blue'>void</font> OnRadChecked(<font color='blue'>object</font> sender, RoutedEventArgs e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;cse.Refresh();<br />
}</p>
<p><font color='blue'>private</font> <font color='blue'>void</font> OnEnableDisableClick(<font color='blue'>object</font> sender, RoutedEventArgs e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;disableRadioButtons = !disableRadioButtons;<br />
&nbsp;&nbsp;&nbsp;&nbsp;cse.Refresh();<br />
}</p>
<p><font color='blue'>private</font> <font color='blue'>void</font> SetAsEditor(<font color='blue'>object</font> sender, RoutedEventArgs e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;CurrentPrincipal.Principal = <font color='blue'>new</font> MyEditorPrincipal();<br />
&nbsp;&nbsp;&nbsp;&nbsp;cse.Refresh();<br />
}</p>
<p><font color='blue'>private</font> <font color='blue'>void</font> SetAsAdmin(<font color='blue'>object</font> sender, RoutedEventArgs e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;CurrentPrincipal.Principal = <font color='blue'>new</font> MyAdminPrincipal();<br />
&nbsp;&nbsp;&nbsp;&nbsp;cse.Refresh();<br />
}</p>
<p>#endregion</code></p></blockquote>
<p>If you write your own application, don&#8217;t forget to embedd the uiStates.config in your silverlight application. To to this, set the Build Acction to &#8220;Embedded Resource&#8221; for the uiStates.config.<br />
<img src="http://jachman.files.wordpress.com/2009/06/uistates.gif?w=291&#038;h=225" alt="uiStates" title="uiStates" width="291" height="225" /></p>
<p>Last but not least I want to say thanks for the <a href="http://blogs.msdn.com/vancem/archive/2006/03/28/563180.aspx">ReaderWriterLock Re-Implementation of Vance Morrison</a>. As an additional goodie the code uses a Cache Implementation to store the method, properties and config sections. This Cache Implementation is not dependend on the ControlStateExtender and can be used everywhere in your application.</p>
<p>Here&#8217;s the complete source code of the <a href="http://www.ad-factum.de/wordpress/SilverlightControlState.zip">ControlStateExtender example application</a>.</p>
<p>Hope you enjoy it.<br />
Cheers</p>
<p>- Gerhard</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f13%2fhow-to-manage-control-states-in-silverlight-applications%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f13%2fhow-to-manage-control-states-in-silverlight-applications%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/219/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=219&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/06/13/how-to-manage-control-states-in-silverlight-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://jachman.files.wordpress.com/2009/06/uistates.gif" medium="image">
			<media:title type="html">uiStates</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f13%2fhow-to-manage-control-states-in-silverlight-applications%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>How to localize Silverlight Applications the smart way</title>
		<link>http://jachman.wordpress.com/2009/06/10/how-to-localize-silverlight-applications-the-smart-way/</link>
		<comments>http://jachman.wordpress.com/2009/06/10/how-to-localize-silverlight-applications-the-smart-way/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 20:01:25 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=181</guid>
		<description><![CDATA[Introduction:
A very common task when developing business applications is to localize them for english, german and other languages. The most common approach is to use ResX files in order to create resource classes which can be used to bind the silverlight controls to. This approach is perfectly described by Sly (that is his nickname). Due [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=181&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>Introduction:</strong><br />
A very common task when developing business applications is to localize them for english, german and other languages. The most common approach is to use ResX files in order to create resource classes which can be used to bind the silverlight controls to. This approach is perfectly described by <a href="http://wpf-e.spaces.live.com/blog/cns!2B248D261D0E0035!203.entry">Sly </a>(that is his nickname). Due to a bug in the resource generator from Microsoft, you always have to change the created resource class from internal to public (look at point 17 on his  <a href="http://wpf-e.spaces.live.com/blog/cns!2B248D261D0E0035!203.entry">blog entry</a>), which is very annoying &#8230; That&#8217;s why I was searching for a better solution to localize silverlight applications &#8211; and I think I found a really smart way to do that task.</p>
<p><strong>Silverlight Localization (the smart way):</strong><br />
<strong>Step1:</strong><br />
<img src="http://jachman.files.wordpress.com/2009/06/resourcefiles1.gif?w=260&#038;h=146" alt="ResourceFiles" title="ResourceFiles" width="260" height="146" class="alignright size-full wp-image-188" />First of all you need a silverlight application;) If you already have one &#8211; fine. If not, create a new example application. After that you have to add a new resource file. First of all, add a fallback resource file which can be used as an alternativ, if the string can&#8217;t be found in one of the language dependend resource files. After that add a resource file for every language you want to add (and call it like the neutral resource file with the postfix of your designated language). </p>
<p><strong>Step2:</strong><br />
Because we go the smart way, it&#8217;s important to disable the code generation. In order to do that, open the neutral resource file with a double click in Visual Studio and switch the Access Modifier to &#8220;No code generation&#8221;.<br />
<img src="http://jachman.files.wordpress.com/2009/06/accessmodifier.gif?w=610&#038;h=151" alt="AccessModifier" title="AccessModifier" width="610" height="151" /> </p>
<p><strong>Step3:</strong><br />
Now add some string resources to the resources files &#8230; </p>
<p><strong>Step4:</strong><br />
Microsoft does not really very much to support the localization task, because by default, the resource files won&#8217;t be included in the created XAP file. To include the language depended resources into the XAP file, you have to open the project file (.csproj) in your favored text editor (e.g. Notepad++) and add the languages comma separated to the &lt;SupportedCultures&gt; Tag.</p>
<blockquote><p><code>   &lt;SupportedCultures&gt;de,en&lt;/SupportedCultures&gt; </code> </p></blockquote>
<p><strong>Step5: (We get it soon)</strong><br />
Now we can implement a new class that reads the resources and returns the localized strings. </p>
<blockquote><p><code> <font color='blue'>public</font> <font color='blue'>class</font> ApplicationResources : IValueConverter<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// The Resource Manager loads the resources out of the executing assembly (and the XAP File where there are embedded)</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>private</font> <font color='blue'>static</font> <font color='blue'>readonly</font> ResourceManager resourceManager =<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>new</font> ResourceManager(<font color='maroon'>&quot;SilverlightLocalization.Resources.Strings&quot;</font>,<br />
&nbsp;&nbsp;&nbsp;&nbsp;Assembly.GetExecutingAssembly());<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// Use this property to specify the culture</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>private</font> <font color='blue'>static</font> CultureInfo uiCulture = Thread.CurrentThread.CurrentUICulture;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>static</font> CultureInfo UiCulture<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>get</font> { <font color='blue'>return</font> uiCulture; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>set</font> { uiCulture = <font color='blue'>value</font>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// This method returns the localized string of the given resource.</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>string</font> Get(<font color='blue'>string</font> resource)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> resourceManager.GetString(resource, UiCulture);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;#region IValueConverter Members<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// This method is used to bind the silverlight component to the resource. </i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>object</font> Convert(<font color='blue'>object</font> <font color='blue'>value</font>, Type targetType, <font color='blue'>object</font> parameter, CultureInfo culture)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var reader = (ApplicationResources)<font color='blue'>value</font>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> reader.Get((<font color='blue'>string</font>)parameter);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>object</font> ConvertBack(<font color='blue'>object</font> <font color='blue'>value</font>, Type targetType, <font color='blue'>object</font> parameter, CultureInfo culture)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// ConvertBack is not used, because the Localization is only a One Way binding</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>throw</font> <font color='blue'>new</font> NotImplementedException();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;#endregion<br />
}<br /></code>
</p></blockquote>
<p><strong>Step6:</strong><br />
Now we have to publish the ApplicationResources class as a Application Resource in order to use it for our binding. To to this, open the <strong>App.xaml</strong> and extend the Application.Resources.</p>
<blockquote><p><code>&lt;Application.Resources&gt;<br />
&lt;SilverlightLocalization:ApplicationResources x:Key=<font color='maroon'>&quot;Localization&quot;</font>/&gt;<br />
&lt;/Application.Resources&gt;</code></p></blockquote>
<p><strong>Step7 (last step):</strong><br />
Yeahh &#8211; we can bind the silverlight components now. Therefore replace the content of the Text attribute of the component to localize with the following generic replacement: <em>{Binding ConverterParameter=<strong>Page_label01</strong>, Converter={StaticResource Localization}, Source={StaticResource Localization}}</em></p>
<p>e.g. &#8220;Page_label01&#8243; is the name of the label specified for that component within the resource file.</p>
<p>The complete example could look like this:</p>
<blockquote><p><code> &lt;TextBlock <br />
FontSize=<font color='maroon'>&quot;32&quot;</font><br />
TextAlignment=<font color='maroon'>&quot;Center&quot;</font><br />
Margin = <font color='maroon'>&quot;0,200,0,0&quot;</font><br />
Text=<font color='maroon'>&quot;{Binding ConverterParameter=Page_label01, Converter={StaticResource Localization}, Source={StaticResource Localization}}&quot;</font><br />
MouseLeftButtonUp=<font color='maroon'>&quot;OnClick&quot;</font><br />
/&gt;<br /></code></p></blockquote>
<p><strong>Advantages of this solution:</strong><br />
First of all, I think this solution is very smart, because you don&#8217;t need to update the class modifier from internal to public, everytime you change the resource strings.<br />
A second advantage is, that the XAP file is not as big, as if you would use code generation. Why? Because you simply don&#8217;t have to create tons of resource access properties. Every resource is loaded within the ApplicationResource class. </p>
<p><strong>Example:</strong><br />
With the following link you can try a working example: <a href="http://www.timepunch.de/wordpress/silverlightLocalization/">Click me Example Application</a><br />
And here&#8217;s the example source code to download: <a href="http://www.timepunch.de/wordpress/SilverlightLocalization.zip">Smart Silverlight Localization</a> (You have to set SilverlightLocalizationTestPage.aspx as the Start Page)</p>
<p>Hope you enjoy this solution.<br />
Cheers<br />
- Gerhard</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f10%2fhow-to-localize-silverlight-applications-the-smart-way%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f10%2fhow-to-localize-silverlight-applications-the-smart-way%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/181/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=181&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/06/10/how-to-localize-silverlight-applications-the-smart-way/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://jachman.files.wordpress.com/2009/06/resourcefiles1.gif" medium="image">
			<media:title type="html">ResourceFiles</media:title>
		</media:content>

		<media:content url="http://jachman.files.wordpress.com/2009/06/accessmodifier.gif" medium="image">
			<media:title type="html">AccessModifier</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f06%2f10%2fhow-to-localize-silverlight-applications-the-smart-way%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>How to serialize interface types and lists using the XmlSerializer</title>
		<link>http://jachman.wordpress.com/2009/04/08/how-to-serialize-interface-types-and-lists-using-the-xmlserializer/</link>
		<comments>http://jachman.wordpress.com/2009/04/08/how-to-serialize-interface-types-and-lists-using-the-xmlserializer/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 08:42:18 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[XmlSerializer]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=145</guid>
		<description><![CDATA[The XmlSerializer works fine as long as you work with concrete classes. But soon as you specify an interface within an object the serialization will fail with the message &#8220;Cannot serialize member &#8230;&#8221; But there&#8217;s a solution to hack this.
Have a look at the following example classes.
 public interface ISerializedItem
{
&#160;&#160;&#160;&#160;string Name { get; set; }
}
 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=145&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>The XmlSerializer works fine as long as you work with concrete classes. But soon as you specify an interface within an object the serialization will fail with the message &#8220;Cannot serialize member &#8230;&#8221; But there&#8217;s a solution to hack this.</strong></p>
<p>Have a look at the following example classes.</p>
<blockquote><p><code> <font color='blue'>public</font> <font color='blue'>interface</font> ISerializedItem<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>string</font> Name { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
}</code></p></blockquote>
<blockquote><p><code> <font color='blue'>public</font> <font color='blue'>class</font> ItemA : ISerializedItem { ... }<br />
<font color='blue'>public</font> <font color='blue'>class</font> ItemB : ISerializedItem { ... }</code></p></blockquote>
<blockquote><p><code><font color='blue'>public</font> <font color='blue'>class</font> ToSerialize <br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> List&lt;ISerializedItem&gt; Items { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
}<br /></code></p></blockquote>
<p>When you&#8217;re trying to serialize such objects, you will get an exception. The workaround is to implement the <strong>IXmlSerializable</strong> interface.</p>
<p>There&#8217;re three methods to implement:</p>
<blockquote><ul>
<li><font color='blue'>public</font> XmlSchema GetSchema()</li>
<li><font color='blue'>public</font> void ReadXml(XmlReader reader) </li>
<li><font color='blue'>public</font> void WriteXml(XmlWriter writer) </li>
</ul>
</blockquote>
<p>The GetSchema can return NULL &#8211; it&#8217;s not used in our case. The ReadXml and WriteXml Method must implement the serialization. As long this is not trivial here&#8217;s the complete code for the example above.</p>
<blockquote><p><code><font color='blue'>using</font> System;<br />
<font color='blue'>using</font> System.Collections.Generic;<br />
<font color='blue'>using</font> System.Diagnostics;<br />
<font color='blue'>using</font> System.Text;<br />
<font color='blue'>using</font> System.Xml;<br />
<font color='blue'>using</font> System.Xml.Schema;<br />
<font color='blue'>using</font> System.Xml.Serialization;<br />
<font color='blue'>using</font> System.Xml.XPath;</p>
<p><code><br />
<font color='blue'>namespace</font> GenericInterfaceXmlSerializer<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>interface</font> ISerializedItem<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>string</font> Name { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>class</font> ItemA : ISerializedItem<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>string</font> Name { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>class</font> ItemB : ISerializedItem<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>string</font> Name { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>class</font> ToSerialize : IXmlSerializable <br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> List&lt;ISerializedItem&gt; Items { <font color='blue'>get</font>; <font color='blue'>set</font>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> ToSerialize()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Items = <font color='blue'>new</font> List&lt;ISerializedItem&gt;();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#region IXmlSerializable implementation<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> XmlSchema GetSchema()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> <font color='blue'>null</font>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>void</font> ReadXml(XmlReader reader)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// Create a XPathDocument out of the Xml in order to navigate through</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XPathDocument xPath = <font color='blue'>new</font> XPathDocument(reader);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XPathNavigator navigator = xPath.CreateNavigator();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// Iterate through all elements of Items</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XPathNodeIterator iterator = navigator.Select(<font color='maroon'>&quot;//ToSerialize/Items/Element&quot;</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>while</font> (iterator.MoveNext())<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dictionary&lt;string, string&gt; values = ReadNodes(iterator.Current);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// Create element of the specified type</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ISerializedItem element = (ISerializedItem)Activator.CreateInstance(Type.GetType(values[<font color='maroon'>&quot;Type&quot;</font>]));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;element.Name = values[<font color='maroon'>&quot;Name&quot;</font>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Items.Add(element);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>void</font> WriteXml(XmlWriter writer)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// Create a new Xml Document</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XmlDocument xdoc = <font color='blue'>new</font> XmlDocument();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// Iterate trough all Items and append them</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XmlNode items = xdoc.AppendChild(CreateNode(xdoc, <font color='maroon'>&quot;Items&quot;</font>, <font color='blue'>string</font>.Empty));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>foreach</font> (ISerializedItem item <font color='blue'>in</font> Items)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XmlNode element = items.AppendChild(CreateNode(xdoc, <font color='maroon'>&quot;Element&quot;</font>, <font color='blue'>string</font>.Empty));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;element.AppendChild(CreateNode(xdoc, <font color='maroon'>&quot;Type&quot;</font>, item.GetType().FullName));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;element.AppendChild(CreateNode(xdoc, <font color='maroon'>&quot;Name&quot;</font>, item.Name));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// Output XML</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xdoc.WriteTo(writer);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>private</font> <font color='blue'>static</font> XmlNode CreateNode(XmlDocument doc, <font color='blue'>string</font> name, <font color='blue'>string</font> <font color='blue'>value</font>)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XmlNode node = doc.CreateNode(XmlNodeType.Element, name, <font color='blue'>string</font>.Empty);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>if</font> (!<font color='blue'>string</font>.IsNullOrEmpty(<font color='blue'>value</font>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.AppendChild(doc.CreateTextNode(<font color='blue'>value</font>));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> node;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>private</font> <font color='blue'>static</font> Dictionary&lt;string, string&gt; ReadNodes(XPathNavigator navigator)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dictionary&lt;string, string&gt; result = <font color='blue'>new</font> Dictionary&lt;string, string&gt;();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>if</font> (!navigator.MoveToFirstChild())<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> result;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>do</font> { result.Add(navigator.Name, navigator.Value); } <font color='blue'>while</font> (navigator.MoveToNext());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;navigator.MoveToParent();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> result;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#endregion<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>class</font> Program<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// Example Code</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>static</font> <font color='blue'>void</font> Main()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>try</font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ToSerialize toSerialize = <font color='blue'>new</font> ToSerialize();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;toSerialize.Items.Add(<font color='blue'>new</font> ItemA{Name=<font color='maroon'>&quot;Cat&quot;</font>});<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;toSerialize.Items.Add(<font color='blue'>new</font> ItemB{Name=<font color='maroon'>&quot;Dog&quot;</font>});<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XmlSerializer serializer = <font color='blue'>new</font> XmlSerializer(<font color='blue'>typeof</font>(ToSerialize));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XmlTextWriter writer = <font color='blue'>new</font> XmlTextWriter(<font color='maroon'>&quot;test.xml&quot;</font>, Encoding.UTF8);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serializer.Serialize(writer, toSerialize);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.Close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;XmlTextReader reader = <font color='blue'>new</font> XmlTextReader(<font color='maroon'>&quot;test.xml&quot;</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ToSerialize result = (ToSerialize) serializer.Deserialize(reader);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.Close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Debug.Assert(toSerialize.Items.Count == result.Items.Count, <font color='maroon'>&quot;Count does not match&quot;</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>catch</font> (Exception exc)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>do</font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.Error.WriteLine(exc.Message);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exc = exc.InnerException;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <font color='blue'>while</font> (exc != <font color='blue'>null</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br /></code>
</p></blockquote>
<p>The trick is, to serialize also the type of the element within the collection. As you can see the ReadXml method first reads the Type of the element and creates an object of that type by using the Activator. </p>
<p><code>ISerializedItem element = (ISerializedItem)Activator.CreateInstance(Type.GetType(values[<font color='maroon'>&quot;Type&quot;</font>]));</code></p>
<p>After that the interface properties can be set with the values of the de-serialization.</p>
<p>Pretty cool I think.<br />
Cheers<br />
- Gerhard</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f04%2f08%2fhow-to-serialize-interface-types-and-lists-using-the-xmlserializer%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f04%2f08%2fhow-to-serialize-interface-types-and-lists-using-the-xmlserializer%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/145/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=145&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/04/08/how-to-serialize-interface-types-and-lists-using-the-xmlserializer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f04%2f08%2fhow-to-serialize-interface-types-and-lists-using-the-xmlserializer%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Async Delegates instead of the Thread class</title>
		<link>http://jachman.wordpress.com/2009/04/03/using-async-delegates-instead-of-the-thread-class/</link>
		<comments>http://jachman.wordpress.com/2009/04/03/using-async-delegates-instead-of-the-thread-class/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 09:16:38 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Delegate]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=132</guid>
		<description><![CDATA[If you want to write some asnychronous code, the good old fashioned way is to create a new Thread. But this has several pitfalls, e.g. when you need to use parameters or you need to retrieve a return value. Then this way is getting tricky. 
Have a look at this solution that is using async [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=132&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>If you want to write some asnychronous code, the good old fashioned way is to create a new Thread. But this has several pitfalls, e.g. when you need to use parameters or you need to retrieve a return value. Then this way is getting tricky. </strong></p>
<p>Have a look at this solution that is using async delegates instead of the Thread class.</p>
<blockquote><p><code><font color='blue'>private</font> <font color='blue'>delegate</font> <font color='blue'>bool</font> ThreadDelegate(<font color='blue'>int</font> counter, <font color='blue'>int</font> sleep);</p>
<p><font color='green'><i>/// &lt;summary&gt;</i></font><br />
<font color='green'><i>/// Thread Method.</i></font><br />
<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
<font color='blue'>public</font> <font color='blue'>static</font> <font color='blue'>bool</font> ThreadMethod(<font color='blue'>int</font> counter, <font color='blue'>int</font> sleep)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>for</font> (<font color='blue'>int</font> x = counter; x &gt; 0; x--)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color='maroon'>&quot;Sleeping &quot;</font> + sleep + <font color='maroon'>&quot;ms&quot;</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.Sleep(sleep);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>return</font> <font color='blue'>true</font>;<br />
}</p>
<p><font color='green'><i>/// &lt;summary&gt;</i></font><br />
<font color='green'><i>/// Main Thread</i></font><br />
<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
<font color='blue'>static</font> <font color='blue'>void</font> Main()<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;ThreadDelegate threadDelegate = ThreadMethod;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;IAsyncResult result = threadDelegate.BeginInvoke(10, 200, <font color='blue'>null</font>, <font color='blue'>null</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='green'><i>// do something between BeginInvoke and EndInvoke</i></font><br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>bool</font> threadResult = threadDelegate.EndInvoke(result);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color='maroon'>&quot;Result &quot;</font> + threadResult);<br />
}<br /></code></p></blockquote>
<p><strong>And now the opposite, the solution that is using the Thread class. </strong></p>
<blockquote><p>
<code> <font color='green'><i>/// &lt;summary&gt;</i></font><br />
<font color='green'><i>/// Thread Arguments </i></font><br />
<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
<font color='blue'>class</font> ThreadArguments<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>int</font> counter;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>int</font> sleep;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>public</font> <font color='blue'>bool</font> result;<br />
}</p>
<p><font color='green'><i>/// &lt;summary&gt;</i></font><br />
<font color='green'><i>/// Thread method</i></font><br />
<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
<font color='blue'>public</font> <font color='blue'>static</font> <font color='blue'>void</font> ThreadMethod(<font color='blue'>object</font> argument)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;ThreadArguments ta = (ThreadArguments) argument;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue'>for</font> (<font color='blue'>int</font> x = ta.counter; x &gt; 0; x--)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color='maroon'>&quot;Sleeping &quot;</font> + ta.sleep + <font color='maroon'>&quot;ms&quot;</font>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.Sleep(ta.sleep);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;ta.result = <font color='blue'>true</font>;<br />
}</p>
<p><font color='green'><i>/// &lt;summary&gt;</i></font><br />
<font color='green'><i>/// Main Thread</i></font><br />
<font color='green'><i>/// &lt;/summary&gt;</i></font><br />
<font color='blue'>static</font> <font color='blue'>void</font> Main()<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;ThreadArguments ta = <font color='blue'>new</font> ThreadArguments();<br />
&nbsp;&nbsp;&nbsp;&nbsp;ta.counter = 10;<br />
&nbsp;&nbsp;&nbsp;&nbsp;ta.sleep = 200;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;Thread thread = <font color='blue'>new</font> Thread(ThreadMethod);<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread.Start(ta);<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread.Join();<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color='maroon'>&quot;Result &quot;</font> + ta.result);<br />
}<br />
</code></p></blockquote>
<p>As you can see, that&#8217;s not really a nice solution to do such things, because you have to create a separated class to pass the parameters to the thread function. I think the better solution is the first one that is using async delegates. </p>
<p>I hope that I could deliver you some new insights about async delegates and threads.<br />
- Gerhard</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f04%2f03%2fusing-async-delegates-instead-of-the-thread-class%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f04%2f03%2fusing-async-delegates-instead-of-the-thread-class%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/132/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=132&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/04/03/using-async-delegates-instead-of-the-thread-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f04%2f03%2fusing-async-delegates-instead-of-the-thread-class%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Code Analysis Tool VisualNDepend at a Glance</title>
		<link>http://jachman.wordpress.com/2009/03/18/code-analysis-tool-visualndepend-at-a-glance/</link>
		<comments>http://jachman.wordpress.com/2009/03/18/code-analysis-tool-visualndepend-at-a-glance/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 12:48:14 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=105</guid>
		<description><![CDATA[To admit I ever thought that code analysis is interessting at all, but not really necessary for my projects. Since I saw VisualNDepend I changed my opinion about that. But step by step.
Patrick Smacchia (C# MVP) asked me, if I would like to have a look at VisualNDepend. That was in december last year. I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=105&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>To admit I ever thought that code analysis is interessting at all, but not really necessary for my projects. Since I saw VisualNDepend I changed my opinion about that. But step by step.</strong></p>
<p>Patrick Smacchia (C# MVP) asked me, if I would like to have a look at VisualNDepend. That was in december last year. I was not really confident about the need of such a software, but I told him that I like to test it. I got a registration code. But I did not testet it. In January Patrick asked me, if I did have a look into VisualNDepend. So I had been caught. </p>
<p><a href="http://jachman.files.wordpress.com/2009/03/visualndepend.jpg"><img align="right" src="http://jachman.files.wordpress.com/2009/03/visualndepend.jpg?w=300&#038;h=182" alt="VisualNDepend" title="VisualNDepend" width="300" height="182" class="size-medium wp-image-110" /></a></p>
<p>I installed VisualNDepend on my machine, created a new project and added my solution of <a href="http://www.timepunch.de">TimePunch </a>to it. After analyzing the project, a report had been created and VisualNDepend opened the main window. </p>
<p>My first impression was that the screen was really overloaded with information &#8211; so that it nearly knocks me out. But I thought about Patrick and gave VisualNDepend a chance. </p>
<p>Today I like this Tool very much, and I would not like to work without it anymore. So what changed my mind? For me, it was the powerfull and impressive CQL (Code Query Language) of VisualNDepend that convinced me. </p>
<p>Have a look at the following example:</p>
<blockquote><p><code>// Methods that could be declared as 'private' in C#,  'Private' in VB.NET<br />
WARN IF Count &gt; 0 IN SELECT TOP 10 METHODS WHERE CouldBePrivate<br />
</code></p></blockquote>
<p>Here&#8217;s a link to the complete CQL Query Documentation. <a href="http://www.ndepend.com/CQL.htm">http://www.ndepend.com/CQL.htm</a></p>
<p>I love to have that quick overview of the parts of my software that needs to be refactored. For example, it tells you the methods that have the most cyclomatic complexity, or it can give you an overview of which methods uses boxing and unboxing for parameter values that can slow down your system.</p>
<p>Check out VisualNDepend and tell me your opinion: <a href="http://www.ndepend.com">http://www.ndepend.com</a></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f03%2f18%2fcode-analysis-tool-visualndepend-at-a-glance%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f03%2f18%2fcode-analysis-tool-visualndepend-at-a-glance%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=105&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2009/03/18/code-analysis-tool-visualndepend-at-a-glance/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://jachman.files.wordpress.com/2009/03/visualndepend.jpg?w=300" medium="image">
			<media:title type="html">VisualNDepend</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2009%2f03%2f18%2fcode-analysis-tool-visualndepend-at-a-glance%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>One-liner for splitting Pascal Casing &#8230;</title>
		<link>http://jachman.wordpress.com/2008/10/16/one-liner-for-splitting-pascal-casing/</link>
		<comments>http://jachman.wordpress.com/2008/10/16/one-liner-for-splitting-pascal-casing/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 12:27:21 +0000</pubDate>
		<dc:creator>Gerhard Stephan</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Pascal Casing]]></category>
		<category><![CDATA[Useful Hints]]></category>

		<guid isPermaLink="false">http://jachman.wordpress.com/?p=93</guid>
		<description><![CDATA[Today I had to write a method for splitting Pascal Cased property names to readable text strings. Because I found a small and stylish solution, I want to show you.
string outputString = System.Text.RegularExpressions.Regex.Replace(
inputString, &#34;([a-z])([A-Z])&#34;, &#34;$1 $2&#34;, System.Text.RegularExpressions.RegexOptions.Compiled);
Hope you like it
Cheers
- Gerhard

       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=93&subd=jachman&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Today I had to write a method for splitting Pascal Cased property names to readable text strings. Because I found a small and stylish solution, I want to show you.</p>
<blockquote><p><code><font color='blue'>string</font> outputString = System.Text.RegularExpressions.Regex.Replace(<br />
inputString, <font color='maroon'>&quot;([a-z])([A-Z])&quot;</font>, <font color='maroon'>&quot;$1 $2&quot;</font>, System.Text.RegularExpressions.RegexOptions.Compiled);</code></p></blockquote>
<p>Hope you like it<br />
Cheers<br />
- Gerhard</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fjachman.wordpress.com%2f2008%2f10%2f16%2fone-liner-for-splitting-pascal-casing%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2008%2f10%2f16%2fone-liner-for-splitting-pascal-casing%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jachman.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jachman.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jachman.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jachman.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jachman.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jachman.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jachman.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jachman.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jachman.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jachman.wordpress.com/93/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jachman.wordpress.com&blog=200378&post=93&subd=jachman&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://jachman.wordpress.com/2008/10/16/one-liner-for-splitting-pascal-casing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/95ece9eab681c373a240708f5cc5aee9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jachman</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fjachman.wordpress.com%2f2008%2f10%2f16%2fone-liner-for-splitting-pascal-casing%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
	</channel>
</rss>