<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tracelight.ch - Leuchtspur im Internet &#187; Programming</title>
	<atom:link href="http://www.tracelight.ch/category/informationtechnology/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tracelight.ch</link>
	<description></description>
	<lastBuildDate>Tue, 20 Jul 2010 19:26:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>StarCraft Bot in F#</title>
		<link>http://www.tracelight.ch/2010/03/21/starcraft-bot-in-f/</link>
		<comments>http://www.tracelight.ch/2010/03/21/starcraft-bot-in-f/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 19:29:49 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/?p=665</guid>
		<description><![CDATA[Shure you remember the game StarCraft! I know the game is really old but still played in the community. Ever wondered how to write a StarCraft bot (especially in F#)? Go check out this link: StarCraft Bot in F#]]></description>
			<content:encoded><![CDATA[<p>Shure you remember the game StarCraft! I know the game is really old but still played in the community. Ever wondered how to write a StarCraft bot (especially in F#)? Go check out this link:</p>
<p><a href="http://blogs.msdn.com/chrsmith/archive/2010/03/18/writing-a-starcraft-bot-in-f.aspx">StarCraft Bot in F#</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2010/03/21/starcraft-bot-in-f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Part II: Mimic SynchronizationContext behaviour on .NET CF</title>
		<link>http://www.tracelight.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/</link>
		<comments>http://www.tracelight.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 20:18:34 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Mobile Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[CF]]></category>
		<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[Delegate]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[SendOrPostCallback]]></category>
		<category><![CDATA[SynchronizationContext]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/</guid>
		<description><![CDATA[I just posted the second part of the article Part I: Mimic SynchronizationContext behaviour on .NET CF on planetgeek.ch! On the article I try to show how the basic behavior of the SynchronizationContext can be achieved on the .NET compact framework platform. Please refer to the article under: http://www.planetgeek.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/]]></description>
			<content:encoded><![CDATA[<p>I just posted the second part of the article <em>Part I: Mimic SynchronizationContext behaviour on .NET CF</em> on planetgeek.ch! On the article I try to show how the basic behavior of the SynchronizationContext can be achieved on the .NET compact framework platform. Please refer to the article under:</p>
<p><a title="http://www.planetgeek.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/" href="http://www.planetgeek.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/">http://www.planetgeek.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Part I: Mimic SynchronizationContext behaviour on .NET CF</title>
		<link>http://www.tracelight.ch/2009/02/08/part-i-mimic-synchronizationcontext-behaviour-on-net-cf/</link>
		<comments>http://www.tracelight.ch/2009/02/08/part-i-mimic-synchronizationcontext-behaviour-on-net-cf/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 17:42:57 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Mobile Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET CF]]></category>
		<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[Delegate]]></category>
		<category><![CDATA[SendOrPostCallback]]></category>
		<category><![CDATA[SynchronizationContext]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2009/02/08/part-i-mimic-synchronizationcontext-behaviour-on-net-cf/</guid>
		<description><![CDATA[Before I got into the details of the problem I want to briefly describe what the SynchronizationContext class really does and what it’s main purpose really is in the first part of the article. From that perspective I&#8217;m going to show how the basic functionality of the SynchronizationContext class can be implemented for the .NET [...]]]></description>
			<content:encoded><![CDATA[<p>Before I got into the details of the problem I want to briefly describe what the <em>SynchronizationContext</em> class really does and what it’s main purpose really is in the first part of the article. From that perspective I&#8217;m going to show how the basic functionality of the <em>SynchronizationContext</em> class can be implemented for the .NET compact framework in the second part of the article..</p>
<p> The msdn library documentation states:
</p>
<blockquote><p>Provides the basic functionality for propagating a synchronization context in various synchronization models.</p>
</blockquote>
<p>I must admit the first time when I read this definition I didn’t really get the key point behind the SynchronizationContext class. Detailed look into the implementation of SynchronizationContext and its base classes provided me the following information:</p>
<blockquote><p><span style="background-color: #ffffff">The <em>SynchronizationContext</em> class is a class belonging to the System.Threading namespace. The <em>SynchronizationContext</em> provides a model to make the communication between threads easier and more robust especially if multiple threading contexts/apartments such as “UI threading context” etc. are present.</span></p>
</blockquote>
<p>To get a deeper understanding of the definition above I want to give you a short example. Imagine if you have a separate thread performing an intense calculation such as calculating the n-th Fibonacci number. When the separate thread has finished its long running operation you want to display the n-th Fibonacci number on a user interface. Normally (without using the <em>SynchronizationContext</em> class) you would need to do the following (or at least something similar):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> FibonacciResultDelegate<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> fibonacciResult<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> MethodCalledByTheFibonacciThread<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> fibonacciNumber<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> fibonacciResultTextBox.<span style="color: #0000FF;">InvokeRequired</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      FibonacciResultDelegate fibonacciDelegate <span style="color: #008000;">=</span>
         MethodCalledByTheFibonacciThread<span style="color: #008000;">;</span>
      fibonacciDelegate.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> fibonacciNumber <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      return<span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   fibonacciResultTextBox.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> fibonacciNumber.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
  <br />With the <em>SynchronizationContext</em> class we can invoke delegates in the context of a different thread. For the example above we could do the following: </p>
<p></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> FibonacciPresenter
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> Thread workerThread<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> SynchronizationContext context<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IFibonacciView fibonacciView<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> FibonacciWorker<span style="color: #000000;">&#40;</span>IFibonacciView view<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            fibonacciView <span style="color: #008000;">=</span> view<span style="color: #008000;">;</span>
&nbsp;
            context <span style="color: #008000;">=</span> SynchronizationContext.<span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span>
&nbsp;
            workerThread <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ThreadStart<span style="color: #000000;">&#40;</span>FibonacciCalc<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            workerThread.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> FibonacciCalc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">long</span> result <span style="color: #008000;">=</span> CalculateFibonacciNumber<span style="color: #000000;">&#40;</span> ... <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            context.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> SendOrPostCallback<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> state<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
               fibonacciView.<span style="color: #0000FF;">DisplayResult</span><span style="color: #000000;">&#40;</span>result<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// details omitted...</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In code line 13 we can see how the <em>SynchronizationContext</em> is retrieved. The <em>SynchronizationContex.Current</em> property points to the <em>SynchronizationContext</em> of the thread where the <em>FibonacciWorker</em> was created (in my example the <em>FibonacciWorker</em> would be created in a control). Then the <em>FibonacciCalc</em> method can <em>post</em> (asynchronous) or <em>send</em> (synchronous) a <em>SendOrPostCallback</em> delegate containing the “job” which needs to be marshaled over the <em>SynchronizationContext</em>. Therefore the line <em>fibonacciView.DisplayResult(result)</em> would be invoked on the UI thread which allows us to remove the “invoke required” code parts and directly set the fibonacci calculation result to the textbox <em>text</em> property.</p>
<p>We can briefly summarize that the purpose of the <em>SynchronizationContext</em> is to post (asynchronous) or send (synchronous) <em>SendOrPostCallback</em> delegates in the correct threading context which simplifies marshaling.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2009/02/08/part-i-mimic-synchronizationcontext-behaviour-on-net-cf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delegate.DynamicInvoke for .NET Compact Framework</title>
		<link>http://www.tracelight.ch/2009/01/18/delegatedynamicinvoke-for-net-compact-framework/</link>
		<comments>http://www.tracelight.ch/2009/01/18/delegatedynamicinvoke-for-net-compact-framework/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 00:35:14 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Mobile Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[Compact Framework]]></category>
		<category><![CDATA[Delegate]]></category>
		<category><![CDATA[DynamicInvoke]]></category>
		<category><![CDATA[ExtensionMethod]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/?p=600</guid>
		<description><![CDATA[As you might already know I&#8217;m a certified windows mobile application developer. My speciality is hybrid application development for applications which target both the full .NET framework platform and also the mobile platform. Of course nobody wants to write the same code for each platform again so you have to come up with some tricks [...]]]></description>
			<content:encoded><![CDATA[<p>As you might already know I&#8217;m a certified windows mobile application developer. My speciality is hybrid application development for applications which target both the full .NET framework platform and also the mobile platform. Of course nobody wants to write the same code for each platform again so you have to come up with some tricks and solutions to overcome some limitations on the compact framework.</p>
<p>One such limitation is the missing Delegate.DynamicInvoke method. The Delegate.DynamicInvoke method allows to dynamically invoke delegates late-bound. That means normally when you are invoking a method via a delegate you actually need to have knowledge about the target type where the delegate gets executed. With Delegate.DynamicInvoke this is not longer necessary. The beauty of this is, that you can have base code like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Elsewhere</span>
RegisterDelegate<span style="color: #000000;">&#40;</span>SomeClass.<span style="color: #0000FF;">SomeMethod</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
RegisterDelegate<span style="color: #000000;">&#40;</span>SomeOtherClass.<span style="color: #0000FF;">SomeOtherMethod</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
FireForAllWith<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Code in some utility</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> FireForAllWith<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">params</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   someGenericCollection.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>dlg <span style="color: #008000;">=&gt;</span> dlg.<span style="color: #0000FF;">DynamicInvoke</span><span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> RegisterDelegate<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">Delegate</span> dlg<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   someGenericCollection.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>dlg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But if you try to use Delegate.DynamicInvoke in the compact framework your infrastructure code will not compile because for some obscure reasons microsoft decided not to implement Delegate.DynamicInvoke for .NET compact framework. Here is my solution to this problem:</p>
<p>I created an extension method for the delegate class with the name DynamicInvoke. This extension method uses a small trick to implement the DynamicInvoke behaviour of the full framework platform.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> DelegateExtensions
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> DynamicInvoke<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> <span style="color: #FF0000;">Delegate</span> dlg, <span style="color: #0600FF;">params</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> dlg.<span style="color: #0000FF;">Method</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>dlg.<span style="color: #0000FF;">Target</span>, BindingFlags.<span style="color: #0600FF;">Default</span>, <span style="color: #0600FF;">null</span>, args, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see I&#8217;m using the delegates method property which returns a MethodInfo object. On the MethodInfo I&#8217;m able to call Invoke and pass the arguments to the bound method. But the problem here is that Invoke requires a target where the method gets executed. This is where the delegates target property comes into play. That&#8217;s the whole magic and you&#8217;re able to dynamically invoke late bound methods via Delegate.DynamicInvoke.</p>
<p>Download <a href='http://www.tracelight.ch/wp-content/uploads/2009/01/delegateextensions.cs'>DelegateExtensions</a> for the source code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2009/01/18/delegatedynamicinvoke-for-net-compact-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Linq to SQL is announced dead&#8230;</title>
		<link>http://www.tracelight.ch/2008/12/03/linq-to-sql-is-announced-dead/</link>
		<comments>http://www.tracelight.ch/2008/12/03/linq-to-sql-is-announced-dead/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 09:21:39 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Linq to Entities]]></category>
		<category><![CDATA[Linq to SQL]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2008/12/03/linq-to-sql-is-announced-dead/</guid>
		<description><![CDATA[Now it&#8217;s almost official: [...] We’re making significant investments in the Entity Framework such that as of .NET 4.0 the Entity Framework will be our recommended data access solution for LINQ to relational scenarios.&#160; We are listening to customers regarding LINQ to SQL and will continue to evolve the product based on feedback we receive [...]]]></description>
			<content:encoded><![CDATA[<p>Now it&#8217;s almost official:</p>
<blockquote><p>[...]
<p>We’re making significant investments in the Entity Framework such that as of .NET 4.0 the Entity Framework will be our recommended data access solution for LINQ to relational scenarios.&nbsp; We are listening to customers regarding LINQ to SQL and will continue to evolve the product based on feedback we receive from the community as well.
<p>Tim Mallalieu<br />Program Manager, LINQ to SQL and Entity Framework</p>
</blockquote>
<p><a title="http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to-entities-roadmap.aspx" href="http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to-entities-roadmap.aspx">http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to-entities-roadmap.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2008/12/03/linq-to-sql-is-announced-dead/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ConfigurationSectionHandler the easy way</title>
		<link>http://www.tracelight.ch/2008/12/01/configurationsectionhandler-the-easy-way/</link>
		<comments>http://www.tracelight.ch/2008/12/01/configurationsectionhandler-the-easy-way/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 18:14:03 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ConfigurationManager]]></category>
		<category><![CDATA[ConfigurationSectionHandler]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2008/12/03/configurationsectionhandler-the-easy-way/</guid>
		<description><![CDATA[Have you ever had the need to develop your own configuration section handler for application configuration files (app.config) in C#. Everybody that has been doing this lately knows how tedious this can be. But there is a quick and easy way to implement simple configuration section handlers in C#. This post shows you how&#8230; Sample [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever had the need to develop your own configuration section handler for application configuration files (app.config) in C#. Everybody that has been doing this lately knows how tedious this can be. But there is a quick and easy way to implement simple configuration section handlers in C#. This post shows you how&#8230;</p>
<h2>Sample</h2>
<p>In the sample we are going to implement a simple configuration section handler which will input an enumeration and a simple string constant. I quickly define the interface:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> ITracelightConfigurationSectionHandler <span style="color: #008000;">:</span> 
IConfigurationSectionHandler
   <span style="color: #000000;">&#123;</span>
      LanguageEnumeration Language <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
      <span style="color: #FF0000;">string</span> SomeInput <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And the enumeration. Now here comes the first trick into the play. We are going to define our enumeration as a DataContract with the DataContractAttribute from the System.Runtime.Serialization namespace. Every enumeration member that we want to be able to serialize must be marked with the EnumMemberAttribute. This allows us later to transparently deserialize the enumeration from the application configuration file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.Serialization</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>DataContract<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> LanguageEnumeration <span style="color: #000000;">&#123;</span>
   <span style="color: #000000;">&#91;</span>EnumMember<span style="color: #000000;">&#93;</span>
   German,
   <span style="color: #000000;">&#91;</span>EnumMember<span style="color: #000000;">&#93;</span>
   English,
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Next we need the concrete class which implements our configuration section handler interface. The class itself are we going to mark as DataContract with the DataContractAttribute. Important here is the Namspace property in the DataContractAttribute must be set to &#8220;&#8221; (see Line 7). In the Create Method from the IConfigurationSectionHandler interface we are going to use the DataContractSerializer and instantiate it with our concrete class (which implements the ITracelightConfigurationSectionHandler interface). In Line 19 we specify that we want to read the object out of the serializer and must also specify false as second parameter (this surpresses name checking while deserialization). Then we can read the properties out of the configSection instance. Set it on the properties on the instance and return a reference to ourself (this). And we are done!</p>
<p>You only have to remember to set the DataMemberAttribute on every property you want to serialize or deserialize. With the datamember property you can also define how the name looks like in the configuration. If you don&#8217;t define a name, the configuration file section must be exactly named as the property.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">...
    <span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Configuration</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.Serialization</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Xml</span><span style="color: #008000;">;</span>
...
&nbsp;
<span style="color: #000000;">&#91;</span>DataContract<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">Namespace</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TracelightConfigurationSectionHandler <span style="color: #008000;">:</span>
ITracelightConfigurationSectionHandler
   <span style="color: #000000;">&#123;</span>
         <span style="color: #008080; font-style: italic;">// From IConfigurationSectionHandler</span>
         <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> Create<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> parent, <span style="color: #FF0000;">object</span> configContext, XmlNode section<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var xs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DataContractSerializer<span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>TracelightConfigurationSectionHandler <span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            XmlNodeReader xnr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> XmlNodeReader<span style="color: #000000;">&#40;</span>section<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                var configSection <span style="color: #008000;">=</span> xs.<span style="color: #0000FF;">ReadObject</span><span style="color: #000000;">&#40;</span>xnr, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> TracelightConfigurationSectionHandler<span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>configSection <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    Language <span style="color: #008000;">=</span> configSection.<span style="color: #0000FF;">Language</span><span style="color: #008000;">;</span>
                    SomeInput<span style="color: #008000;">=</span> configSection.<span style="color: #0000FF;">SomeInput</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">return</span> this<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> s <span style="color: #008000;">=</span> ex.<span style="color: #0000FF;">Message</span><span style="color: #008000;">;</span>
                Exception iex <span style="color: #008000;">=</span> ex.<span style="color: #0000FF;">InnerException</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>iex <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    s <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;; &quot;</span> <span style="color: #008000;">+</span> iex.<span style="color: #0000FF;">Message</span><span style="color: #008000;">;</span>
                    iex <span style="color: #008000;">=</span> iex.<span style="color: #0000FF;">InnerException</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ConfigurationErrorsException<span style="color: #000000;">&#40;</span>
                    <span style="color: #666666;">&quot;Unable to deserialize an object of type <span style="color: #008080; font-weight: bold;">\'</span>&quot;</span> <span style="color: #008000;">+</span> GetType<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FullName</span> <span style="color: #008000;">+</span>
                    <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\'</span> from  the &lt;&quot;</span> <span style="color: #008000;">+</span> section.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&gt; configuration section: &quot;</span> <span style="color: #008000;">+</span>
                    s,
                    ex,
                    section<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Automatic property for the language</span>
        <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;language&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> LanguageEnumeration Language 
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Automatic property for the input, which can be omitted.</span>
        <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;input&quot;</span>, Order <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>, IsRequired <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> SomeInput
        <span style="color: #000000;">&#123;</span>
            get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And how looks the App.config file?</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configSections<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;tracelightsection&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;Fully.Qualified.Namespace.TracelightConfigurationSectionHandler, MyAssemblyNameWithoutDllExtension&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configSections<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tracelightsection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;language<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>German<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/language<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SomeTextualInput<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tracelightsection<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>And how can the data be read from the configuration file? That&#8217;s pretty easy&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var configSection <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ITracelightConfigurationSectionHandler<span style="color: #000000;">&#41;</span>ConfigurationManager.<span style="color: #0000FF;">GetSection</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;tracelightsection&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This works even with complex collections. All you need to now is how to handle the data with the DataContractSerializer. But this is fearly simple to learn.</p>
<p><strong>Remarks</strong>:<br />
When you see strange behaviour such as the data is not deserialized althoug set in the configuration file you need to specify the order property (especially if you are using non required properties or default values with EmitDefaultValue&#8230;).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2008/12/01/configurationsectionhandler-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interesting thoughts about static methods on interfaces</title>
		<link>http://www.tracelight.ch/2008/11/30/interesting-thoughts-about-static-methods-on-interfaces/</link>
		<comments>http://www.tracelight.ch/2008/11/30/interesting-thoughts-about-static-methods-on-interfaces/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 09:44:29 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Static methods]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2008/11/30/interesting-thoughts-about-static-methods-on-interfaces/</guid>
		<description><![CDATA[Jon Sket is arguing on his blog about having static methods in interfaces. I think his article is really good and worth reading. What do you think about this topic? http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx]]></description>
			<content:encoded><![CDATA[<p>Jon Sket is arguing on his blog about having static methods in interfaces. I think his article is really good and worth reading. What do you think about this topic?</p>
<p><a href="http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx">http://msmvps.com/blogs/jon_skeet/archive/2008/08/29/lessons-learned-from-protocol-buffers-part-4-static-interfaces.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2008/11/30/interesting-thoughts-about-static-methods-on-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extension Methoden unter Compact Framework 2.0</title>
		<link>http://www.tracelight.ch/2008/11/23/extension-methoden-unter-compact-framework-20/</link>
		<comments>http://www.tracelight.ch/2008/11/23/extension-methoden-unter-compact-framework-20/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 18:42:13 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Information Technology]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2008/11/23/extension-methoden-unter-compact-framework-20/</guid>
		<description><![CDATA[Interessanter Artikel: http://www.simonrhart.com/2008/11/extension-method-support-in-compact.html Have fun]]></description>
			<content:encoded><![CDATA[<p>Interessanter Artikel:</p>
<p><a title="http://www.simonrhart.com/2008/11/extension-method-support-in-compact.html" href="http://www.simonrhart.com/2008/11/extension-method-support-in-compact.html">http://www.simonrhart.com/2008/11/extension-method-support-in-compact.html</a></p>
<p>Have fun</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2008/11/23/extension-methoden-unter-compact-framework-20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vernichtende Aussage &#252;ber Entwickler</title>
		<link>http://www.tracelight.ch/2008/11/07/vernichtende-aussage-ber-entwickler/</link>
		<comments>http://www.tracelight.ch/2008/11/07/vernichtende-aussage-ber-entwickler/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 09:39:04 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Developers are Cheap]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2008/11/07/vernichtende-aussage-ber-entwickler/</guid>
		<description><![CDATA[Und leider könnte er recht haben&#8230; http://blog.opennetcf.com/ctacke/PermaLink,guid,38c27aaf-38b7-4e48-8dac-929a523f9d25.aspx]]></description>
			<content:encoded><![CDATA[<p>Und leider könnte er recht haben&#8230;</p>
<p><a title="http://blog.opennetcf.com/ctacke/PermaLink,guid,38c27aaf-38b7-4e48-8dac-929a523f9d25.aspx" href="http://blog.opennetcf.com/ctacke/PermaLink,guid,38c27aaf-38b7-4e48-8dac-929a523f9d25.aspx">http://blog.opennetcf.com/ctacke/PermaLink,guid,38c27aaf-38b7-4e48-8dac-929a523f9d25.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2008/11/07/vernichtende-aussage-ber-entwickler/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Die Zukunft von C#</title>
		<link>http://www.tracelight.ch/2008/11/06/die-zukunft-von-c/</link>
		<comments>http://www.tracelight.ch/2008/11/06/die-zukunft-von-c/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 22:21:22 +0000</pubDate>
		<dc:creator>Daniel Marbach</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Compiler as a Service]]></category>
		<category><![CDATA[Zukunft von C#]]></category>

		<guid isPermaLink="false">http://www.tracelight.ch/2008/11/06/die-zukunft-von-c/</guid>
		<description><![CDATA[Ich muss sagen ich war immer etwas skeptisch über die Entwicklung von C# in Hinblick auf die Dynamic Language Runtime. Anders Hejlsberg zeigt aber gerade im Hinblick auf COM Interoperabilität und zum Beispiel die Integration von Javascript in C# Code die wirklichen Vorzüge der dynamischen Aspekte von C# 4.0. Ich kann nur sagen: That rocks! [...]]]></description>
			<content:encoded><![CDATA[<p>Ich muss sagen ich war immer etwas skeptisch über die Entwicklung von C# in Hinblick auf die Dynamic Language Runtime. Anders Hejlsberg zeigt aber gerade im Hinblick auf COM Interoperabilität und zum Beispiel die Integration von Javascript in C# Code die wirklichen Vorzüge der dynamischen Aspekte von C# 4.0. Ich kann nur sagen: That rocks!</p>
<p>Aber glatt umgehauen hat mich der Ausblick auf C# 5.0. Dort heisst das grosse Paradigma Compiler as a Service. Bis anhin war der C# Compiler in C++ geschrieben. Microsoft hat sich nun entschieden den Compiler völlig in Managed Code zu entwickeln. Dies bedeutet, dass es in Zukunft eine Compilerklasse geben wird, deren man zum Beispiel einfach C# Code als String übergeben kann der dann zur Laufzeit kompiliert und evaluiert wird. Anders zeigt dies mit einer ultimativen Konsolenapplikation in deren man Live Code eintippen kann der dann ausgeführt wird. So instanziert er Live direkt in der Konsole ein Windows Form und fügt Elemente drauf etc. Einfach krass!</p>
<p>Überzeugt euch selbst. Das Video ist etwas lang aber unglaublich spannend und gut erklärt:</p>
<p><a title="http://channel9.msdn.com/pdc2008/TL16/" href="http://channel9.msdn.com/pdc2008/TL16/">http://channel9.msdn.com/pdc2008/TL16/</a></p>
<p>Wie wurden einige der Probleme gelöst</p>
<p><a title="http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/" href="http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/">http://channel9.msdn.com/shows/Going+Deep/Inside-C-40-dynamic-type-optional-parameters-more-COM-friendly/</a></p>
<p>und das Design Team&#8230;</p>
<p><a title="http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/" href="http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/">http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tracelight.ch/2008/11/06/die-zukunft-von-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
