<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Byteflux</title>
	
	<link>http://byteflux.me</link>
	<description>Sebastian Negomireanu's Weblog</description>
	<lastBuildDate>Sun, 25 Jul 2010 14:24:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<meta xmlns="http://www.w3.org/1999/xhtml" name="robots" content="noindex,follow" />
<cloud domain="byteflux.me" port="80" path="/?rsscloud=notify" registerProcedure="" protocol="http-post" />
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/byteflux/kync" /><feedburner:info uri="byteflux/kync" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Moles Isolation Framework – presentation from Ronua Sibiu meeting</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/rAz5en3DPxs/</link>
		<comments>http://byteflux.me/index.php/2010/07/23/moles-isolation-framework-presentation-from-ronua-sibiu-meeting/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 09:13:26 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[visualstudio]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2010/07/23/moles-isolation-framework-presentation-from-ronua-sibiu-meeting/</guid>
		<description><![CDATA[[Romana]
Puteti descarca de la adresele de mai jos prezentarea Moles si proiectul demo:

Prezentarea &#8211; http://byteflux.me/wp-content/uploads/2010/07/Moles-Isolation-Framework.pptx
Proiect demo: http://byteflux.me/wp-content/uploads/2010/07/MolesSample.zip

Cerinte minime pentru proiectul demo:
- Visual Studio 2010 Professional
- Moles plug-in (http://research.microsoft.com/en-us/projects/moles/)
Astept comentariile voastre si feedback.
Disclaimer: Nu lucrez pentru Microsoft si nici nu sunt implicat in vreun fel in dezvoltarea acestui proiect. Notele si proiectul demo reprezinta punctul meu [...]]]></description>
			<content:encoded><![CDATA[<p>[Romana]</p>
<p>Puteti descarca de la adresele de mai jos prezentarea Moles si proiectul demo:</p>
<ul>
<li>Prezentarea &#8211; <a title="http://byteflux.me/wp-content/uploads/2010/07/Moles-Isolation-Framework.pptx" href="http://byteflux.me/wp-content/uploads/2010/07/Moles-Isolation-Framework.pptx" target="_blank">http://byteflux.me/wp-content/uploads/2010/07/Moles-Isolation-Framework.pptx</a></li>
<li>Proiect demo: <a title="http://byteflux.me/wp-content/uploads/2010/07/MolesSample.zip" href="http://byteflux.me/wp-content/uploads/2010/07/MolesSample.zip" target="_blank">http://byteflux.me/wp-content/uploads/2010/07/MolesSample.zip</a></li>
</ul>
<p>Cerinte minime pentru proiectul demo:</p>
<p>- Visual Studio 2010 Professional</p>
<p>- Moles plug-in (<a title="http://research.microsoft.com/en-us/projects/moles/" href="http://research.microsoft.com/en-us/projects/moles/">http://research.microsoft.com/en-us/projects/moles/</a>)</p>
<p>Astept comentariile voastre si feedback.</p>
<p><em>Disclaimer: Nu lucrez pentru Microsoft si nici nu sunt implicat in vreun fel in dezvoltarea acestui proiect. Notele si proiectul demo reprezinta punctul meu personal de vedere.</em></p>
<p>[English]</p>
<p>You can download my Moles presentation and demo project from the links below. Unfortunately at this moment the presentation is only in Romanian but I will translate it and provide an English version the next few days.This is a translation of the original presentation in Romanian</p>
<ul>
<li>Slides &#8211; <a href="http://byteflux.me/wp-content/uploads/2010/07/Moles-Isolation-Framework-English.pptx">http://byteflux.me/wp-content/uploads/2010/07/Moles-Isolation-Framework-English.pptx</a></li>
<li>Demo project: <a title="http://byteflux.me/wp-content/uploads/2010/07/MolesSample.zip" href="http://byteflux.me/wp-content/uploads/2010/07/MolesSample.zip">http://byteflux.me/wp-content/uploads/2010/07/MolesSample.zip</a></li>
</ul>
<p>Minimum requirements for running the demo:</p>
<p>- Visual Studio 2010 Professional</p>
<p>- Moles plug-in (<a title="http://research.microsoft.com/en-us/projects/moles/" href="http://research.microsoft.com/en-us/projects/moles/">http://research.microsoft.com/en-us/projects/moles/</a>)</p>
<p>I am waiting for your comments and feedback.</p>
<p><em>Disclaimer: I am not working for Microsoft, nor am I involved in this project. The slides and demo project are strictly my point of view and I am doing this to share something that I feel can be useful.</em></p>
<p><a rev="vote-for" href="http://dotnetshoutout.com/Moles-Isolation-Framework-presentation-from-Ronua-Sibiu-meeting-Uncategorized-Byteflux"><img style="border:0px" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fbyteflux.me%2Findex.php%2F2010%2F07%2F23%2Fmoles-isolation-framework-presentation-from-ronua-sibiu-meeting%2F" alt="Shout it" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/2eX0l7HsgbQBJA4Z4V6MQZIU_Us/0/da"><img src="http://feedads.g.doubleclick.net/~a/2eX0l7HsgbQBJA4Z4V6MQZIU_Us/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/2eX0l7HsgbQBJA4Z4V6MQZIU_Us/1/da"><img src="http://feedads.g.doubleclick.net/~a/2eX0l7HsgbQBJA4Z4V6MQZIU_Us/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/rAz5en3DPxs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2010/07/23/moles-isolation-framework-presentation-from-ronua-sibiu-meeting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2010/07/23/moles-isolation-framework-presentation-from-ronua-sibiu-meeting/</feedburner:origLink></item>
		<item>
		<title>Evenimente pentru programatorii sibieni saptamana viitoare</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/cNRa59veFNo/</link>
		<comments>http://byteflux.me/index.php/2010/07/11/evenimente-pentru-programatorii-sibieni-saptamana-viitoare/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 09:24:27 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[Events]]></category>

		<guid isPermaLink="false">http://byteflux.me/?p=75</guid>
		<description><![CDATA[UPDATE! &#8211; Datorita numarului mare de inscrieri locatia pentru turneul RONUA s-a mutat la Facultatea de Stiinte (str. Ion Ratiu) &#8211; cam. A26 (etajul 2).
Prima jumatate a lunii iulie incepe cu doua evenimente apropiate pentru programatorii .NET sibieni:
Turneu RONUA.RO de lansare Visual Studio 2010
(de la organizatori:)
Începem pe 12 iulie la Brasov, apoi megem
in Sibiu, Cluj, [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>UPDATE! &#8211; Datorita numarului mare de inscrieri locatia pentru turneul RONUA s-a mutat la Facultatea de Stiinte (str. Ion Ratiu) &#8211; cam. A26 (etajul 2).</strong></span></p>
<p>Prima jumatate a lunii iulie incepe cu doua evenimente apropiate pentru programatorii .NET sibieni:</p>
<h1>Turneu RONUA.RO de lansare Visual Studio 2010</h1>
<p>(de la organizatori:)<br />
<em>Începem pe 12 iulie la Brasov, apoi megem<br />
in Sibiu, Cluj, Oradea, Timisoara, Bucuresti si Iasi.</em></p>
<p><em>Te îndemn să te înscrii ACUM<br />
<a href="http://bit.ly/VS2010LI">http://bit.ly/VS2010LI</a></em></p>
<p><em>Din cauza numărului de locuri limitate în săli,<br />
numai cei inscrisi au acces GRATUIT.<br />
<a href="http://bit.ly/VS2010LI">http://bit.ly/VS2010LI</a></em></p>
<p><em>M-ar bucura sa folosesti LikedIn Status-ul tău<br />
pentru a populariza acest eveniment în rândul<br />
conexiunilor tale LinkedIn.</em></p>
<p><em>Dacă poți posta acest anunț și pe alte grupuri<br />
relevante contextului VS2010, please do it!</em></p>
<p><em>And thanks.</p>
<p></em></p>
<p><em><a href="http://bit.ly/VS2010LI">http://bit.ly/VS2010LI</a></em></p>
<p>Pentru sibieni intalnirea va avea loc:</p>
<h3>Marti 13 iulie 2010, ora 18:00-22:00<br />
<span style="text-decoration:line-through">Str. Dorului,  Nr. 20 (cladirea in care se afla si Registrul Comertului), etaj 2, camera 7, ora 18:00.</span><br />
<span style="color: #ff0000;">Facultatea de Stiinte &#8211; Str. Ion Ratiu, camera A26, etajul 2 </span></h3>
<h1>Intalnirea lunara a progamatorilor Sibieni</h1>
<p>Subiectul de luna aceasta este Test Driven Development. Daca esti interesat de acest subiect sau vrei sa prezinti ceva legat de el, te asteptam cu placere.</p>
<p>Evenimentul va avea loc:</p>
<h3>Joi, 15 iulie 2010, incepand cu ora 18<br />
Terasa Club Liquid &#8211; Str. Dobrun nr.1</h3>

<p><a href="http://feedads.g.doubleclick.net/~a/LOm_su4OCItzDL5ciT0Suzu5IK0/0/da"><img src="http://feedads.g.doubleclick.net/~a/LOm_su4OCItzDL5ciT0Suzu5IK0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/LOm_su4OCItzDL5ciT0Suzu5IK0/1/da"><img src="http://feedads.g.doubleclick.net/~a/LOm_su4OCItzDL5ciT0Suzu5IK0/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/cNRa59veFNo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2010/07/11/evenimente-pentru-programatorii-sibieni-saptamana-viitoare/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2010/07/11/evenimente-pentru-programatorii-sibieni-saptamana-viitoare/</feedburner:origLink></item>
		<item>
		<title>Easy creation of code generators in Visual Studio 2008 (part 3 of 4)</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/nDzUB0gqw4g/</link>
		<comments>http://byteflux.me/index.php/2010/05/11/easy-creation-of-code-generators-in-visual-studio-2008-part-3-of-4/#comments</comments>
		<pubDate>Tue, 11 May 2010 12:00:00 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Add-ins]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code generators]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2010/05/11/easy-creation-of-code-generators-in-visual-studio-2008-part-3-of-4/</guid>
		<description><![CDATA[This is the third part of a series covering code generators in Visual Studio 2008. Part I and part II of this series cover how to create the project and all the necessary steps for writing a code generator.
The series is divided into four parts:
Part I  – creating a Visual Studio Package
Part II  – creating [...]]]></description>
			<content:encoded><![CDATA[<p>This is the third part of a series covering code generators in Visual Studio 2008. Part I and part II of this series cover how to create the project and all the necessary steps for writing a code generator.</p>
<p>The series is divided into four parts:</p>
<p><a href="http://byteflux.me/index.php/2010/05/07/easy-creation-of-code-generators-in-visual-studio-2008/" target="_blank">Part I  – creating a Visual Studio Package</a></p>
<p><a href="http://byteflux.me/index.php/2010/05/10/easy-creation-of-code-generators-in-visual-studio-2008-part-2-of-4/" target="_blank">Part II  – creating and registering a code generator</a></p>
<p>Part III (this one) – generating code &amp; debugging</p>
<p>Part IV &#8211; creating the setup project and deploying the package</p>
<p><span id="more-69"></span></p>
<h1>Generating the code and debugging</h1>
<p>So far we’ve created a Visual Studio integration package and a skeleton code generator. The next step will be to actually generate some code.</p>
<p>Before we do that, we must decide on the file format and the output.</p>
<p>For simplicity, let’s assume we will  have plain text files which define constants. The first line will have the full class name (including namespace):</p>
<div>
<pre>MyNamespace.MyClass
Red=1
Blue=2
Pink=3</pre>
</div>
<p>The output for the above example could be:</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:679d526d-b942-4fba-bffd-6e8f1df09aca" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: c#;">namespace MyNamespace
{
	public class MyClass
	{
		public const int Red = 1;
		public const int Blue = 2;
		public const int Pink = 3;
	}
}</pre>
</div>
<p>First we need to modify the GetDefaultExtension() method and make it return a file extension. I like the idea of making it clear when the code is auto generated by a designer, so we’ll return “.designer.cs” as the extension.</p>
<p>Second we need to parse the input file. To make this blog post shorter, I will skip over the parsing details, but you can check the source zip for how we parse the file.</p>
<p>To generate the code we will use CodeDom which makes things easier. Right now the class should look like this:</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:92e04475-1741-4d10-bd1d-e0b2b1430344" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: c#;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TextTemplating.VSHost;
using Microsoft.VisualStudio.Shell;
using VSLangProj80;
using System.Diagnostics;
using System.IO;
using System.CodeDom.Compiler;
using System.CodeDom;

namespace MyCompany.MyCodeGenerator
{
    [ComVisible(true)]
    [Guid(GuidList.guidMyCodeGenerator)]
    [CodeGeneratorRegistration(typeof(MyCodeGenerator), ".myfile", vsContextGuids.vsContextGuidVCSProject, GeneratesDesignTimeSource = true, GeneratorRegKeyName = ".myfile")]
    public class MyCodeGenerator : BaseCodeGeneratorWithSite
    {
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            var provider = CodeDomProvider.CreateProvider("C#");

            try
            {
                var parser = new ConstantsDefinitionParser(inputFileContent);
                var definition = parser.Parse();

                var compileUnit = GenerateCode(definition);
                using (StringWriter writer = new StringWriter())
                {
                    CodeGeneratorOptions options = GetCodeOptions();

                    provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
                    //Generate the code
                    writer.Flush();

                    return GetContentBytes(writer);
                }
            }
            catch (Exception e)
            {
                HanldeException(e);

                //Returning null signifies that generation has failed
                return null;
            }
        }

        private CodeCompileUnit GenerateCode(ConstantsDefinition definition)
        {
            throw new NotSupportedException();
        }

        private static CodeGeneratorOptions GetCodeOptions()
        {
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BlankLinesBetweenMembers = true;
            options.BracingStyle = "C";
            return options;
        }

        private void HanldeException(Exception e)
        {
            StackTrace exceptionStackTrace = new StackTrace(e, true);
            StackFrame exceptionStackFrame = exceptionStackTrace.GetFrame(-2);
            int lineNumber = exceptionStackFrame.GetFileLineNumber();
            int column = exceptionStackFrame.GetFileColumnNumber();
            base.GeneratorErrorCallback(false, 4, e.ToString(), lineNumber, column);
        }

        public override string GetDefaultExtension()
        {
            return ".designer.cs";
        }

        private static byte[] GetContentBytes(StringWriter writer)
        {
            //Get the Encoding used by the writer. We're getting the WindowsCodePage encoding,
            //which may not work with all languages
            Encoding enc = Encoding.GetEncoding(writer.Encoding.WindowsCodePage);

            //Get the preamble (byte-order mark) for our encoding
            byte[] preamble = enc.GetPreamble();
            int preambleLength = preamble.Length;

            //Convert the writer contents to a byte array
            byte[] body = enc.GetBytes(writer.ToString());

            //Prepend the preamble to body (store result in resized preamble array)
            Array.Resize&lt;byte&gt;(ref preamble, preambleLength + body.Length);
            Array.Copy(body, 0, preamble, preambleLength, body.Length);

            //Return the combined byte array
            return preamble;
        }

    }
}</pre>
</div>
<p>Most of the code is self-explanatory. The GetContentBytes method is used to convert the string to it’s binary equivalent and to prepend (if necessary) the byte order mark to it.</p>
<p>The whole code generation magic will happen in the GenerateCode(definition) method:</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:ced6909b-232f-40bc-a9fd-d2955d668293" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: c#;"> private CodeCompileUnit GenerateCode(ConstantsDefinition definition)
{
	throw new NotSupportedException();
}</pre>
</div>
<p>Although CodeDom makes it rather easy to generate code, there is a library called LinqToCodedom on CodePlex that provides a whole set of extension methods and classes that help creating dynamic code.</p>
<p>The GenerateCode method needs to create a namespace, add a class definition to it and then add definitions for each of the defined constants. As you can see below, the code is really easy to make and to understand, but a bit verbose if the classes are complex:</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:7284915f-e614-4838-a887-72f9486468fa" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: c#;">        private CodeCompileUnit GenerateCode(ConstantsDefinition definition)
        {
            var generator = new CodeDomGenerator();

            // create the namespace in which the class resides
            var codeNamespace = generator.AddNamespace(definition.Namespace);

            // create the class definition and set it's attributes
            var classDefinition = codeNamespace.AddClass(definition.ClassName);
            classDefinition.IsClass = true;
            classDefinition.TypeAttributes = System.Reflection.TypeAttributes.Public;

            AddConstantDefinitions(definition, classDefinition);

            return generator.GetCompileUnit(CodeDomGenerator.Language.CSharp);
        }

        private static void AddConstantDefinitions(ConstantsDefinition definition, CodeTypeDeclaration classDefinition)
        {
            for (var i = 0; i &lt; definition.Constants.Count; i++)
            {
                var constantName = definition.Constants.GetKey(i);
                var constantValue = definition.Constants.GetValues(i)[0];

                var constant = new CodeMemberField(typeof(int), constantName);
                constant.Attributes = MemberAttributes.Public | MemberAttributes.Const;

                constant.InitExpression = new CodePrimitiveExpression(Convert.ToInt32(constantValue));

                classDefinition.Members.Add(constant);
            }
        }</pre>
</div>
<h2>Debugging the code</h2>
<p>Fortunately debugging code generators is very easy due to a feature of Visual Studio called the “experimental hive”. Basically there is a copy of the related registry keys which can be used by VisualStudio when debugging instead of the main registry (to prevent hard to diagnose and fix problems). The Visual Studio SDK comes with a command line tool called ‘Reset the Microsoft Visual Studio 2008 Experimental hive’ which, as its name implies, resets this copy of the registry. So if something goes wrong, you can easily go back to the initial state of the experimental hive registry.</p>
<p>If you compile and run the code, a new Visual Studio window will appear. Create a new C# project (a ClassLibrary or Console project) and add a new text file to it. Name the file test.myfile and paste the definition into it. The moment you are saving the file, an automated file is created. If you’ve used the definition above, the generated file should look like this:</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:ef7be373-5a20-455d-9b46-3b83c1964b84" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: c#;">//------------------------------------------------------------------------------
// &lt;auto-generated&gt;
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4927
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// &lt;/auto-generated&gt;
//------------------------------------------------------------------------------

namespace MyNamespace
{

    public class MyClass
    {

        public const int Red = 1;

        public const int Blue = 2;

        public const int Pink = 3;
    }
}</pre>
</div>
<p>Stay tuned for part 4 of this series in which I go into creating a setup package and deploying the generator.</p>
<p><strong>Downloads</strong></p>
<div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;">
<div class="shoutIt"><a rev="vote-for" href="http://dotnetshoutout.com/Submit?url=http%3a%2f%2fbyteflux.me%2findex.php%2f2010%2f05%2f11%2feasy-creation-of-code-generators-in-visual-studio-2008-part-3-of-4%2f&amp;title=Easy+creation+of+code+generators+in+Visual+Studio+2008+(part+3+of+4)"><img style="border:0px" src="http://dotnetshoutout.com/image.axd?url=http://byteflux.me/index.php/2010/05/11/easy-creation-of-code-generators-in-visual-studio-2008-part-3-of-4/" alt="Shout it" /></a></div>
</div>

<p><a href="http://feedads.g.doubleclick.net/~a/GP4xwVsl06s1Kw59DeEEt0_wK9c/0/da"><img src="http://feedads.g.doubleclick.net/~a/GP4xwVsl06s1Kw59DeEEt0_wK9c/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/GP4xwVsl06s1Kw59DeEEt0_wK9c/1/da"><img src="http://feedads.g.doubleclick.net/~a/GP4xwVsl06s1Kw59DeEEt0_wK9c/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/nDzUB0gqw4g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2010/05/11/easy-creation-of-code-generators-in-visual-studio-2008-part-3-of-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2010/05/11/easy-creation-of-code-generators-in-visual-studio-2008-part-3-of-4/</feedburner:origLink></item>
		<item>
		<title>Easy creation of code generators in Visual Studio 2008 (part 2 of 4)</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/N-FrrRLbuyw/</link>
		<comments>http://byteflux.me/index.php/2010/05/10/easy-creation-of-code-generators-in-visual-studio-2008-part-2-of-4/#comments</comments>
		<pubDate>Mon, 10 May 2010 12:40:00 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Add-ins]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code generators]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[addins]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2010/05/10/easy-creation-of-code-generators-in-visual-studio-2008-part-2-of-4/</guid>
		<description><![CDATA[In the first part I’ve written about creating a code generator in VS2008 to eliminate repetitive coding.
The series is divided into four parts:
Part I  – creating a Visual Studio Package
Part II (this one) – creating and registering a code generator
Part III – generating code &#38; debugging
Part IV &#8211; creating the setup project and deploying the [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://byteflux.me/index.php/2010/05/07/easy-creation-of-code-generators-in-visual-studio-2008/">first part</a> I’ve written about creating a code generator in VS2008 to eliminate repetitive coding.</p>
<p>The series is divided into four parts:</p>
<p><a href="http://byteflux.me/index.php/2010/05/07/easy-creation-of-code-generators-in-visual-studio-2008/" target="_blank">Part I  – creating a Visual Studio Package</a></p>
<p>Part II (this one) – creating and registering a code generator</p>
<p>Part III – generating code &amp; debugging</p>
<p>Part IV &#8211; creating the setup project and deploying the package</p>
<p>So let’s jump directly to the problem at hand:</p>
<p><span id="more-57"></span></p>
<h1>Part II – Creating and registering a code generator class</h1>
<p>Since VS2008 add-ins are COM objects, our code generator needs to be prepared for COM interop first. To accomplish this, we need to make it com visible and to assign it a unique identifier.</p>
<p>First, open up the Guids.cs file. We will add there a new constant that keeps our generator’s GUID. You can use the GUID tool in Visual Studio (Tools-&gt;Create GUID). Make sure you are using the registry format and remove the brackets. I’m not a big fan of the default coding style found in the pre-generated classes, but for simplicity I’ve kept the same style in our demo project:</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:21a15071-7e0d-4ca2-b1db-d35cbc7c59bc" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: c#;">// Guids.cs
// MUST match guids.h
using System;

namespace MyCompany.MyCodeGenerator
{
    static class GuidList
    {
        public const string guidMyCodeGeneratorPkgString = "51b9761b-d943-454c-9397-29e2f2b68431";
        public const string guidMyCodeGeneratorCmdSetString = "a619ef76-b153-4f88-bbc5-fda566fe7b4a";
        public const string guidMyCodeGenerator = "1432CFC5-D0C4-44cd-A937-1D476F999219";
        public static readonly Guid guidMyCodeGeneratorCmdSet = new Guid(guidMyCodeGeneratorCmdSetString);
    };
}</pre>
</div>
<p>The next step is to mark the actual code generator as COM visible and to set it’s GUID. This can be easily done by decorating the generator class:</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:d768108c-9a34-4430-a946-114170b2b452" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: c#;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace MyCompany.MyCodeGenerator
{
    [ComVisible(true)]
    [Guid(GuidList.guidMyCodeGenerator)]
    public class MyCodeGenerator
    {
    }
}</pre>
</div>
<p>The code generator has to be also registered as a code generator with Visual Studio and must implement the BaseCodeGeneratorWithSite abstract base class. When you register the code generator you have two options:</p>
<ol>
<li>Register it with a human-readable name and then set it explicitly for a certain file. This method is especially useful if you have more generators that get the same input or if you don’t want to use the generator by default for a certain file. Another situation is where you don’t define a special extension but use already existing file types (XML for example).</li>
<li>Register it as a generator for a certain file extension. This is my favorite since I can create different file extensions for different scenarios. I will go with this option in my example, but you only have to change the name from “.extension” to whatever name you want.</li>
</ol>
<p>In the attached zip file you will see that we have a CodeGeneratorRegistrationAttribute class. This can be found in the Visual Studio SDK or you can copy it from the zip. This file is responsible with creating the necessary registry keys for our generator. To register the generator we need to decorate it using this attribute:</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:c770493a-a4b1-4901-86e9-db2e3407b59d" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: c#;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TextTemplating.VSHost;
using Microsoft.VisualStudio.Shell;
using VSLangProj80;

namespace MyCompany.MyCodeGenerator
{
    [ComVisible(true)]
    [Guid(GuidList.guidMyCodeGenerator)]
    [CodeGeneratorRegistration(typeof(MyCodeGenerator), ".myfile", vsContextGuids.vsContextGuidVCSProject, GeneratesDesignTimeSource = true, GeneratorRegKeyName = ".myfile")]
    public class MyCodeGenerator : BaseCodeGeneratorWithSite
    {
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            throw new NotImplementedException();
        }

        public override string GetDefaultExtension()
        {
            throw new NotImplementedException();
        }
    }
}</pre>
</div>
<p>The attribute tells Visual Studio that it should automatically use the generator for all *.myfile files. The vsContextGuids.vsContextGuidVCSProject tells Visual Studio that this generator should be used when editing a C# project. There are other options for different supported languages. The other parameters are self-explaining.</p>
<p><strong>Downloads:</strong></p>
<p><strong><a href="http://byteflux.me/wp-content/uploads/2010/05/MyCodeGenerator-part2.zip">MyCodeGenerator-part2</a><br />
</strong></p>
<div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;">
<div class="shoutIt"><a rev="vote-for" href="http://dotnetshoutout.com/Submit?url=http%3a%2f%2fbyteflux.me%2findex.php%2f2010%2f05%2f10%2feasy-creation-of-code-generators-in-visual-studio-2008-part-2-of-4%2f&amp;title=Easy+creation+of+code+generators+in+Visual+Studio+2008+(part+2+of+4)"><img style="border:0px" src="http://dotnetshoutout.com/image.axd?url=http://byteflux.me/index.php/2010/05/10/easy-creation-of-code-generators-in-visual-studio-2008-part-2-of-4/" alt="Shout it" /></a></div>
</div>

<p><a href="http://feedads.g.doubleclick.net/~a/ufZ72N5Oi1vSCg2lGkVlUewBdm0/0/da"><img src="http://feedads.g.doubleclick.net/~a/ufZ72N5Oi1vSCg2lGkVlUewBdm0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/ufZ72N5Oi1vSCg2lGkVlUewBdm0/1/da"><img src="http://feedads.g.doubleclick.net/~a/ufZ72N5Oi1vSCg2lGkVlUewBdm0/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/N-FrrRLbuyw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2010/05/10/easy-creation-of-code-generators-in-visual-studio-2008-part-2-of-4/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2010/05/10/easy-creation-of-code-generators-in-visual-studio-2008-part-2-of-4/</feedburner:origLink></item>
		<item>
		<title>Easy creation of code generators in Visual Studio 2008</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/HorZYg_M8ys/</link>
		<comments>http://byteflux.me/index.php/2010/05/07/easy-creation-of-code-generators-in-visual-studio-2008/#comments</comments>
		<pubDate>Fri, 07 May 2010 13:17:00 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Add-ins]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Code generators]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[addins]]></category>
		<category><![CDATA[dry]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2010/05/07/easy-creation-of-code-generators-in-visual-studio-2008/</guid>
		<description><![CDATA[I am a big fan of the DRY principle. This means that whenever I can do something that can be reused with minimal or no effort, I try to do it. Recently I’ve started developing a REST framework for our projects, which is not based on WCF (will blog about this too). One of the [...]]]></description>
			<content:encoded><![CDATA[<p>I am a big fan of the DRY principle. This means that whenever I can do something that can be reused with minimal or no effort, I try to do it. Recently I’ve started developing a REST framework for our projects, which is not based on WCF (will blog about this too). One of the most problematic areas is how to create service proxies easily without having to duplicate loads of code, but at the same time maintain maximum flexibility. After going through several options, the most attractive is creating a Visual Studio custom code generator.</p>
<p>Since there are almost no resources (and the existing ones are very blurry) I’ve decided to create a series of blogs that will be a guide to creating custom code generators, followed by some other posts with real examples and solutions to problems.</p>
<p>The series is divided into four parts:</p>
<p>Part I (this one) – creating a Visual Studio Package</p>
<p>Part II – creating and registering a code generator</p>
<p>Part III – generating code &amp; debugging</p>
<p>Part IV &#8211; creating the setup project and deploying the package</p>
<p>*DISCLAIMER* &#8211; Even if everybody is in the new VS2010 hype, I still think there are a lot of developers that are and will be working in VS2008 for a while.</p>
<p> <span id="more-47"></span>Before jumping to the gory details, you need to be aware that creating code generators is only possible using Visual Studio Professional or higher. Also, you need to install the Visual Studio SDK which can be found <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=59EC6EC3-4273-48A3-BA25-DC925A45584D&amp;displaylang=en">here</a>.
</p>
<h1>Part I&#160; &#8211; Creating a Visual Studio package</h1>
<p>In order to create a custom code generator we need to create a Visual Studio package. I am not 100% sure that this is needed, but I wasn’t able to install the code generator in any other kind of project.</p>
<p>So open Visual Studio and create a new Visual Studio Integration Package project. I’ll call it MyCodeGenerator</p>
<p><a href="http://byteflux.me/wp-content/uploads/2010/05/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://byteflux.me/wp-content/uploads/2010/05/image_thumb.png" width="244" height="165" /></a></p>
<p>A wizard will start guiding you through the package creation steps. You will want to select Visual C# as the language, and to create a new key to sign the assembly. Optionally, if you already have a key, use that one:</p>
<p><a href="http://byteflux.me/wp-content/uploads/2010/05/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://byteflux.me/wp-content/uploads/2010/05/image_thumb1.png" width="244" height="186" /></a></p>
<p>The next screen will ask you for package details. You can enter here a company name, a human-readable package name, the package version and the minimum required Visual Studio version required to use the package. Also, you can set an icon for the package. This information will be later used for signing the package.</p>
<p><a href="http://byteflux.me/wp-content/uploads/2010/05/image2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://byteflux.me/wp-content/uploads/2010/05/image_thumb2.png" width="244" height="187" /></a></p>
<p>The third screen will allow you to add extra functionality to the package (menu commands, tool windows or a custom editor). For our generator we don’t need any, so leave the fields unchecked.</p>
<p><a href="http://byteflux.me/wp-content/uploads/2010/05/image3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://byteflux.me/wp-content/uploads/2010/05/image_thumb3.png" width="244" height="187" /></a></p>
<p>To keep things simple, we won’t use integration or unit tests, so leave the next fields unchecked. If you are using TDD with the Visual Studio test framework, you can leave the checkboxes selected.</p>
<p><a href="http://byteflux.me/wp-content/uploads/2010/05/image4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://byteflux.me/wp-content/uploads/2010/05/image_thumb4.png" width="244" height="188" /></a></p>
<p>After clicking finish, you will get a new Visual Studio Integration Project.</p>
<h2>Package and assembly registration</h2>
<p>Add a new class, and name it MyGenerator.cs. Change the class visibility to public. Since Visual Studio code generators and packages are used as COM objects, we need to register the generator assembly and package. The Visual Studio SDK contains an attribute class that makes this an easy task. The class is called CodeGeneratorRegistrationAttribute and you can find it in the SimpleFileGenerator example project (mine is in <strong>c:\Program Files\Microsoft Visual Studio 2008 SDK\VisualStudioIntegration\Samples\Project\CSharp\Example.SingleFileGenerator\CodeGeneratorRegistrationAttribute.cs</strong>). <a href="http://byteflux.me/wp-content/uploads/2010/05/image5.png"><img style="border-right-width: 0px; margin: 10px 30px 0px 5px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" align="left" src="http://byteflux.me/wp-content/uploads/2010/05/image_thumb5.png" width="178" height="244" /></a>Alternatively, you can use the one in the attached project.</p>
<p>Add the file to the project and add references to the following assemblies:</p>
<ul>
<li>Microsoft.VisualStudio.TextTemplating.VSHost </li>
<li>VSLangProj </li>
<li>VSLangProj2 </li>
<li>VSLangProj80 </li>
<li>VSLangProj90 </li>
</ul>
<p>The VSLangProj assemblies are duplicated in the GAC (they can be found in two locations). I have successfully used the ones in the <strong>C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\</strong> folder.</p>
<p>For the package assembly to be registered as a COM, you need to set it’s ComVisible attribute to true. Open Properties –&gt; AssemblyInfo.cs and edit the line that says: <strong><a href="http://byteflux.me/wp-content/uploads/2010/05/image6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" align="right" src="http://byteflux.me/wp-content/uploads/2010/05/image_thumb6.png" width="244" height="110" /></a></strong></p>
<p><strong>[assembly: ComVisible(false)] </strong></p>
<p>to</p>
<p><strong>[assembly: ComVisible(true)]</strong></p>
<p>Also, you need to register the assembly from COM Interop. You can accomplish this by opening the project properties and checking the Register for COM interop checkbox.</p>
<p>That’s it for the first step.</p>
<p>Check out the second part of this tutorial on how the code generator is implemented and how we can debug it.</p>
<p><strong>Downloads:</strong></p>
<p><a href="http://byteflux.me/wp-content/uploads/2010/05/MyCodeGenerator-part1.zip">MyCodeGenerator-part1</a></p>
<div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;">
<div class="shoutIt"><a rev="vote-for" href="http://dotnetshoutout.com/Submit?url=http%3a%2f%2fbyteflux.me%2findex.php%2f2010%2f05%2f07%2feasy-creation-of-code-generators-in-visual-studio-2008%2f&amp;title=Easy+creation+of+code+generators+in+Visual+Studio+2008"><img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http://byteflux.me/index.php/2010/05/07/easy-creation-of-code-generators-in-visual-studio-2008/" style="border:0px" /></a></div>
</div>

<p><a href="http://feedads.g.doubleclick.net/~a/gfvIax8ho9qBKaPILzZZwJy6amQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/gfvIax8ho9qBKaPILzZZwJy6amQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/gfvIax8ho9qBKaPILzZZwJy6amQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/gfvIax8ho9qBKaPILzZZwJy6amQ/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/HorZYg_M8ys" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2010/05/07/easy-creation-of-code-generators-in-visual-studio-2008/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2010/05/07/easy-creation-of-code-generators-in-visual-studio-2008/</feedburner:origLink></item>
		<item>
		<title>Executing actions on the UI thread</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/VXRdOkuc_NM/</link>
		<comments>http://byteflux.me/index.php/2009/11/27/executing-actions-on-the-ui-thread/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 12:22:00 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Synchronization]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[windows presentation foundation]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2009/11/27/executing-actions-on-the-ui-thread/</guid>
		<description><![CDATA[When working with frameworks like Silverlight, WPF or Windows Forms you often encounter the need to ensure a piece of code runs on the UI thread. Below, I present a clean way to do this using the SynchronizationContext class.
The ISynchronized interface
The first thing to do is to make a contract for all classes that need [...]]]></description>
			<content:encoded><![CDATA[<p>When working with frameworks like Silverlight, WPF or Windows Forms you often encounter the need to ensure a piece of code runs on the UI thread. Below, I present a clean way to do this using the SynchronizationContext class.<span id="more-20"></span></p>
<h1>The ISynchronized interface</h1>
<p>The first thing to do is to make a contract for all classes that need to expose this functionality. The ISynchronized interface declares this contract. Basically it only exposes a single property which contains the synchronization context on which the code must be executed.</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System.Threading;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> <span style="color: #008000">/// Contract for classes that must be able to execute methods in a certain synchronization context.</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ISynchronized</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>     <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>     <span style="color: #008000">/// Gets the synchronization context.</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>     <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>     <span style="color: #008000">/// &lt;value&gt;The synchronization context.&lt;/value&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>     SynchronizationContext SynchronizationContext</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>         get;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span> }</pre>
<p><!--CRLF--></div>
</div>
<h1>The extension method</h1>
<p>In order to easily use this I have made an extension method that allows specifying an Action to be executed on the synchronization context.</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span> <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span> <span style="color: #008000">/// This class contains the extension methods for the ISynchronized implementations.</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span> <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> SynchronizedExtensions</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>     <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>     <span style="color: #008000">/// Executes the supplied action in the instance's SynchronizationContext.</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>     <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>     <span style="color: #008000">/// &lt;param name="instance"&gt;The synchronized instance.&lt;/param&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>     <span style="color: #008000">/// &lt;param name="synchronizedAction"&gt;The synchronized action.&lt;/param&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Synchronized(<span style="color: #0000ff">this</span> ISynchronized instance, Action synchronizedAction)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>         <span style="color: #0000ff">if</span> (instance.SynchronizationContext != <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span>             instance.SynchronizationContext.Post(CallAction, synchronizedAction);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum18" style="color: #606060">  18:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum19" style="color: #606060">  19:</span>         <span style="color: #0000ff">else</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum20" style="color: #606060">  20:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum21" style="color: #606060">  21:</span>             synchronizedAction();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum22" style="color: #606060">  22:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum23" style="color: #606060">  23:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum24" style="color: #606060">  24:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum25" style="color: #606060">  25:</span>     <span style="color: #008000">/// &lt;summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum26" style="color: #606060">  26:</span>     <span style="color: #008000">/// Calls the passed action.</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum27" style="color: #606060">  27:</span>     <span style="color: #008000">/// &lt;/summary&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum28" style="color: #606060">  28:</span>     <span style="color: #008000">/// &lt;param name="state"&gt;The state.&lt;/param&gt;</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum29" style="color: #606060">  29:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> CallAction(<span style="color: #0000ff">object</span> action)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum30" style="color: #606060">  30:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum31" style="color: #606060">  31:</span>         var actionToExecute = action <span style="color: #0000ff">as</span> Action;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum32" style="color: #606060">  32:</span>         actionToExecute();</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum33" style="color: #606060">  33:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum34" style="color: #606060">  34:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The Synchronized method checks if there is a synchronization context and if it is, it will post a call on the synchronization context. If it doesn’t exist, it will call the action directly.</p>
<h1>Sample usage</h1>
<p>Suppose we have a method SetText which sets the text in a control on the current Form/User control etc. This method must be called on the UI thread, otherwise an exception is raised by the framework. The following method will do this using the above extension method:</p>
<p>public void MethodThatGetsCalledOnAnArbitraryThread(string text)</p>
<p>{</p>
<p>this.Synchronized(() =&gt;</p>
<p>{</p>
<p>this.SetText(text);</p>
<p>});</p>
<p>}</p>
<p>You can write any code inside the action or just use a delegate for this.</p>
<div id="scid:8eb9d37f-1541-4f29-b6f4-1eea890d4876:dcbf98cb-3466-498b-8bf0-2121419f1f8a" class="wlWriterSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<div>Download the full source code here: <a href="http://byteflux.me/wp-content/uploads/2009/11/Synchronized1.zip" target="_self">Synchronized.zip</a></div>
</div>
<div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;">
<div class="shoutIt"><a rev="vote-for" href="http://dotnetshoutout.com/Submit?url=http%3a%2f%2fbyteflux.me%2findex.php%2f2009%2f11%2f27%2fexecuting-actions-on-the-ui-thread%2f&amp;title=Executing+actions+on+the+UI+thread"><img style="border:0px" src="http://dotnetshoutout.com/image.axd?url=http://byteflux.me/index.php/2009/11/27/executing-actions-on-the-ui-thread/" alt="Shout it" /></a></div>
</div>

<p><a href="http://feedads.g.doubleclick.net/~a/fuQJZXWRnnpD4IxtayqPIXhywTc/0/da"><img src="http://feedads.g.doubleclick.net/~a/fuQJZXWRnnpD4IxtayqPIXhywTc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/fuQJZXWRnnpD4IxtayqPIXhywTc/1/da"><img src="http://feedads.g.doubleclick.net/~a/fuQJZXWRnnpD4IxtayqPIXhywTc/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/VXRdOkuc_NM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2009/11/27/executing-actions-on-the-ui-thread/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2009/11/27/executing-actions-on-the-ui-thread/</feedburner:origLink></item>
		<item>
		<title>Getting rid of the annoying Visual Studio XAML editor bugs</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/Ght4N1hSJIM/</link>
		<comments>http://byteflux.me/index.php/2009/10/16/getting-rid-of-the-annoying-visual-studio-xaml-editor-bugs/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 09:24:00 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[fix]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2009/10/16/getting-rid-of-the-annoying-visual-studio-xaml-editor-bugs/</guid>
		<description><![CDATA[Everybody that is doing XAML editing has hit this issue one moment in time – the editor is buggy. First of all – the designer is really useless. Second, if you keep editors open side-by-side  you will eventually get a Visual Studio crash. Also from time to time you won’t be able to open the [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody that is doing XAML editing has hit this issue one moment in time – the editor is buggy. First of all – the designer is really useless. Second, if you keep editors open side-by-side  you will eventually get a Visual Studio crash. Also from time to time you won’t be able to open the XAML file because apparently it is open but hidden. Finally, I would mention that the XAML editor is a lot slower than the other Visual Studio editors.<span id="more-14"></span></p>
<p>For all the issues above, there is a very simple solution which will make your life easier. You just need to right click an XAML file and choose <strong>Open With…</strong>. In the dialog select <strong>Source Code (Text) Editor</strong> and make it the default editor. You will get exactly the same features as in the XAML editor (Intellisense etc.), but without the crashes and performance issues.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fbyteflux.me%2findex.php%2f2009%2f10%2f16%2fgetting-rid-of-the-annoying-visual-studio-xaml-editor-bugs%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fbyteflux.me%2findex.php%2f2009%2f10%2f16%2fgetting-rid-of-the-annoying-visual-studio-xaml-editor-bugs%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<p><a rev="vote-for" href="http://dotnetshoutout.com/Getting-rid-of-the-annoying-Visual-Studio-XAML-editor-bugs-Tools-Byteflux"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fbyteflux.me%2Findex.php%2F2009%2F10%2F16%2Fgetting-rid-of-the-annoying-visual-studio-xaml-editor-bugs%2F" alt="Shout it" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/KMV_g3x0LiGfAwY77akfpnqhRGE/0/da"><img src="http://feedads.g.doubleclick.net/~a/KMV_g3x0LiGfAwY77akfpnqhRGE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/KMV_g3x0LiGfAwY77akfpnqhRGE/1/da"><img src="http://feedads.g.doubleclick.net/~a/KMV_g3x0LiGfAwY77akfpnqhRGE/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/Ght4N1hSJIM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2009/10/16/getting-rid-of-the-annoying-visual-studio-xaml-editor-bugs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2009/10/16/getting-rid-of-the-annoying-visual-studio-xaml-editor-bugs/</feedburner:origLink></item>
		<item>
		<title>Useful tools for your development experience</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/tkWeg5aoUfg/</link>
		<comments>http://byteflux.me/index.php/2009/09/06/useful-tools-for-your-development-experience/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 15:23:00 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[Add-ins]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[addins]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2009/09/06/useful-tools-for-your-development-experience/</guid>
		<description><![CDATA[Recently, Scott Hanselman has released his notorious tools list for 2009 here at http://hanselman.com/tools. I want to amend his list and add some of the tools that I use for day-to-day work.
Windows &#38; multimedia utilities
Here is stuff that I use for normal Windows operations, multimedia etc.

IZarc – I actually like this more than 7zip because [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, Scott Hanselman has released his notorious tools list for 2009 here at <a href="http://hanselman.com/tools">http://hanselman.com/tools</a>. I want to amend his list and add some of the tools that I use for day-to-day work.<span id="more-13"></span></p>
<h1>Windows &amp; multimedia utilities</h1>
<p>Here is stuff that I use for normal Windows operations, multimedia etc.</p>
<ul>
<li><a href="http://www.izarc.org/" target="_blank">IZarc</a> – I actually like this more than 7zip because of it’s GUI. It “knows” the same archive formats as 7zip and it is very fast.</li>
<li><a href="http://www.ghisler.com" target="_blank">Total Commander</a> – This is one of the most used and useful tools out there. For those that don’t know it (what planet do you live on?), TC is a Windows file manager similar to Norton Commander. It offers two-pane file system navigation, basic copy/move/rename operations (that can be moved in the background or queued), multiple file renaming, file comparison, viewer and editor integration and way more. If you don’t have it already, go try it, it has a very large list of features (and add-ins) that will cover all your file &amp; folder manipulation needs. I have tried several alternatives, but none compare with Total Commander.</li>
<li><a href="http://cdrtfe.sourceforge.net/cdrtfe/index_en.html" target="_blank">cdrtools frontend</a> – A free, open source front-end for all the Linux command line CD/DVD writing tools. It is simple to use and offers all functionality that commercial packages provide (Data CD/DVD &amp; Audio CD writing, creating and writing CD images, Video CD writing etc.). I particularly like it because it’s very light and only does what it’s supposed to do. Most commercial packages (like Nero or Roxio) have become very bloated for me and I don’t use them anymore. If you feel the same, then cdrtools offers you a great alternative.</li>
<li><a href="http://aimp.ru/index.php?do=lang-en" target="_blank">AIMP</a> – A free alternative to Winamp. Same as with Nero, Winamp has become a very big and bloated application. In the end I just need something to listen to music, and AIMP is great for this. Designed as a Winamp clone, it offers more features in some areas (like playlist management), it’s very easy to use and has a very low memory footprint.</li>
<li><a href="http://www.faststone.org/FSViewerDetail.htm" target="_blank">FastStone Image Viewer</a> – One of the best image viewers out there. It supports a lot of image formats (couldn’t find an unsupported one yet), has basic image manipulation features (crop, resize, adjust colors etc.) and also has support for batch file processing. It also features an easy to use screen capture utility.</li>
<li><a href="http://www.gomlab.com/eng/GMP_download.html" target="_blank">GOM player</a> – A very nice video player which automatically downloads necessary codecs. It has a lot of options for tweaking the videos you watch and a feature that I particularly like is that it automatically loads similar files from the same folder. This is very useful when watching a TV series and it loads all other episodes from the same folder.</li>
<li><a href="http://www.gimp.org/" target="_blank">The GIMP</a> – An alternative to Adobe Photoshop. It is not as advanced or easy to use as Photoshop, but you can get simple things done with it. It features multi-layer image editing, a lot of effects and brushes.</li>
<li><a href="http://www.exactaudiocopy.de/" target="_blank">Exact Audio Copy</a> – This is a CD ripping tool that supports output to WAV, FLAC or MP3.</li>
<li><a href="http://www.medieval.it/content/view/28/70/" target="_blank">CUE Splitter</a> – A tool that splits a single audio file into tracks based on information in a CUE file.</li>
<li>Audio Grabber – A free application for ripping and most important Line in sampling. I use it all the time for recording mixes or live performances.</li>
</ul>
<h1>Development tools</h1>
<ul>
<li><span style="color: #efefef;"><a href="http://www.red-gate.com/products/reflector/" target="_blank">Reflector</a> </span>– This is invaluable to any .NET programmer. It allows you to dig inside assemblies and view their source code. Sometimes this is the only way to accomplish things.</li>
<li><span style="color: #efefef;"><a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/" target="_blank">CodeRush Express</a></span> – This is also mentioned on Scott Hanselman’s list but I think it really deserves to be mentioned again. With every version it becomes better and better and it just fills all the missing spots in the Visual Studio IDE. The paid edition offers even more functionality, but even with the free one, you can boost your productivity many times.</li>
<li><span style="color: #efefef;"><a href="http://submain.com/products/ghostdoc.aspx" target="_blank">GhostDoc</a></span> – This small Visual Studio Add-in is a gem. You can provide documentation for your code with two clicks. The documentation is generated by analyzing your method or property names, which means that most of the time you need to hand edit it. Still it is a huge timesaver and if you follow correct coding rules, you can minimize the hand editing effort.</li>
<li><span style="color: #efefef;"><a href="http://www.microsoft.com/downloads/details.aspx?familyid=9AEAA970-F281-4FB0-ABA1-D59D7ED09772&amp;displaylang=en" target="_blank">FXCop</a> </span>– Provided by Microsoft, it is a free add-in that checks your code at compile time for various issues that affect performance, security and other areas.</li>
<li><span style="color: #efefef;"><a href="http://code.msdn.microsoft.com/sourceanalysis" target="_blank">StyleCop</a></span> – The complement for FXCop, it checks your code at compile time for coding standards violations. This is very useful for actively imposing a coding standard to your team.</li>
<li><span style="color: #efefef;"><a href="http://www.narrange.net/narrangemojo/home.aspx" target="_blank">NArrange</a></span> – This is a helper tool which beautifies your source code. There are other alternatives (like<span style="color: #efefef;"> <a href="http://www.rauchy.net/regionerate/" target="_blank">Regionerate</a></span>) out there, but the nice thing about NArrange is that the result is StyleCop compliant. This is very useful especially when introducing StyleCop on a very large project because you can quickly eliminate 80% of the violations. Both tools can be customized to impose your own coding standard.</li>
<li><span style="color: #efefef;"><a href="http://www.nunit.org/index.php" target="_blank">NUnit</a></span> – one of the oldest unit testing tools for the .NET platform. It’s simple and easy to learn and I use it in my testing projects. There are alternatives out there (including the Visual Studio unit testing components), but I like that I can use NUnit without Visual Studio and it is free.</li>
<li><span style="color: #efefef;"><a href="http://code.google.com/p/moq/" target="_blank">Moq</a></span> – A very nice mocking framework. At the beginning I was oscillating between Rhino Mocks and Moq, but I couldn’t use Rhino Mocks for mocking (or is it moqing <img src='http://byteflux.me/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ) methods that received Action or Function arguments. Moq provides a nice fluent interface and it is very very easy to use. I recommend also checking out<span style="color: #efefef;"> <a href="http://ayende.com/projects/rhino-mocks.aspx" target="_blank">Rhino Mocks</a>, </span>since that is also a very good mocking framework.</li>
<li><span style="color: #efefef;"><a href="http://structuremap.sourceforge.net/Default.htm" target="_blank">StructureMap</a></span> – IoC/DI is a very common buzzword. From all IoC/DI frameworks I’ve tried, I like  StructureMap the most. It provides a lot of ways for registering and resolving dependencies and one feature that I use a lot is the possibility of managing the object lifetime.</li>
<li><span style="color: #efefef;"><a href="http://www.mokosh.co.uk/page/VsCommands.aspx">VScommands</a></span> – Free add-on for Visual Studio that allows you to group files together.</li>
<li><span style="color: #efefef;"><a href="http://subversion.tigris.org/" target="_blank">Subversion</a>, <a href="http://tortoisesvn.tigris.org/" target="_blank">TortoiseSVN</a> </span>and <span style="color: #efefef;"><a href="http://www.visualsvn.com/server/" target="_blank">Visual SVN Server</a></span> – This is really indispensable in my day-to-day work. Subversion keeps track of your code files, allows you to share them with your team and to version them. TortoiseSVN is the front-end GUI for Subversion, while Visual SVN Server is a very easy way to setup your Subversion server on the Windows platform.</li>
</ul>
<h1>Browsers, addins and extensions</h1>
<p>I mostly use FireFox for web browsing and sometimes Google Chrome or Internet Explorer. So this will cover mostly my addins for FireFox:</p>
<ul>
<li><span style="color: #efefef;"><a href="http://www.colorzilla.com/firefox/" target="_blank">ColorZilla</a> </span>– Color picker tool useful for web development</li>
<li><span style="color: #efefef;"><a href="http://dafizilla.sourceforge.net/table2clip/" target="_blank">Dafizilla Table2Clipboard</a></span> – Firefox doesn’t copy HTML tables correctly for pasting in Word. This add-on fixes this issue.</li>
<li><span style="color: #efefef;"><a href="http://delicious.com/help/quicktour/firefox" target="_blank">Delicious Bookmarks</a> </span>– This is one of my favorites add-ins. It allows you to save your bookmarks in Delicious. Now you can also tweet your bookmarks.</li>
<li><span style="color: #efefef;"><a href="http://www.downthemall.net/" target="_blank">DownThemAll</a> </span>– A very good download manager for FireFox. It allows you to choose different download destinations and replaces the default basic download manager that comes with Firefox.</li>
<li><span style="color: #efefef;"><a href="http://getfirebug.com/" target="_blank">Firebug</a></span> – Indispensable if you do web development. It allows you to view all details of the current page (network traffic, request/response headers, response content, cookie information, JavaScript information &amp; debugger). Also you can use the YSlow extension to find performance issues in your website.</li>
<li><span style="color: #efefef;"><a href="http://ietab.mozdev.org/" target="_blank">IE Tab</a> </span>– Useful if you need to do cross-browser tests.</li>
<li><span style="color: #efefef;"><a href="http://www.lastpass.com" target="_blank">LastPass </a></span>– This is an add-on which saves your passwords in an online storage. The passwords are encrypted using 256 bits encryption.</li>
<li><a href="http://www.kevinfreitas.net/extensions/measureit/" target="_blank">MeasureIt</a> – Add-on for measuring elements on web pages.</li>
<li><a href="http://chrispederick.com/work/web-developer/" target="_blank">Web Developer toolbar</a> – Adds various web development tools (such as disable JavaScript, disable cache, etc.)</li>
</ul>
<p>While this list is not exhaustive I hope it is useful for somebody out there. Let me know if you know/use more cool tools.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/YHuSD7lb1KNCnR9KYqcwbYf837g/0/da"><img src="http://feedads.g.doubleclick.net/~a/YHuSD7lb1KNCnR9KYqcwbYf837g/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/YHuSD7lb1KNCnR9KYqcwbYf837g/1/da"><img src="http://feedads.g.doubleclick.net/~a/YHuSD7lb1KNCnR9KYqcwbYf837g/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/tkWeg5aoUfg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2009/09/06/useful-tools-for-your-development-experience/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2009/09/06/useful-tools-for-your-development-experience/</feedburner:origLink></item>
		<item>
		<title>Creating a type-safe implementation of the INotifyPropertyChanged interface</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/YCsrRzWIvnw/</link>
		<comments>http://byteflux.me/index.php/2009/08/17/creating-a-type-safe-implementation-of-the-inotifypropertychanged-interface/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 10:38:00 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2009/08/17/creating-a-type-safe-implementation-of-the-inotifypropertychanged-interface/</guid>
		<description><![CDATA[When working with Silverlight two-way binding (and not only there) you need to implement the INotifyPropertyChanged interface in order to notify any observers that the property values of the object have changed.
The INotifyChangedProperty exposes only the PropertyChanged event which is used by observers to subscribe to the change notifications. The typical pattern is to have [...]]]></description>
			<content:encoded><![CDATA[<p>When working with Silverlight two-way binding (and not only there) you need to implement the INotifyPropertyChanged interface in order to notify any observers that the property values of the object have changed.<span id="more-12"></span></p>
<p>The INotifyChangedProperty exposes only the <strong>PropertyChanged</strong> event which is used by observers to subscribe to the change notifications. The typical pattern is to have a base class which will fire the events like this:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> PropertyChangedNotifier : INotifyPropertyChanged</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span>    {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>        <span style="color: #0000ff">protected</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> OnPropertyChanged(<span style="color: #0000ff">string</span> propertyName)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>            var eventHandler = PropertyChanged;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>            <span style="color: #0000ff">if</span> (eventHandler != <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>            {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>                eventHandler(<span style="color: #0000ff">this</span>, <span style="color: #0000ff">new</span> PropertyChangedEventArgs(propertyName));</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>            }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>This can be used like this:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyClass</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> myProperty;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> MyProperty</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>         get { <span style="color: #0000ff">return</span> myProperty; }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>         set</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>             <span style="color: #0000ff">if</span>(myProperty != <span style="color: #0000ff">value</span>)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>             {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>                 myProperty = <span style="color: #0000ff">value</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>                 OnPropertyChanged(<span style="color: #006080">"MyProperty"</span>);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>             }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>For small projects, this implementation is ok, but on large scale projects, using such an implementation will lead to a maintenance nightmare, especially when performing property rename refactorings. After each refactoring, you need to manually change the name of the property inside the setter.</p>
<h1>Type safe implementation using Expression Trees</h1>
<p>.NET 3.5 comes with the ability of navigating through the object hierarchy using Expression trees. This is used for example in LINQ providers for dynamically building the SQL queries. The same Expression trees can be used in this case for building a type safe solution for the <strong>OnPropertyChanged</strong> method:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> PropertyChangedNotifier : INotifyPropertyChanged</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">void</span> OnPropertyChanged&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; action)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>    {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>        var propertyName = GetPropertyName&lt;T&gt;(action);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>        OnPropertyChanged(propertyName);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>    }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GetPropertyName&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; action)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>    {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>        var expression = (MemberExpression)action.Body;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>        var propertyName = expression.Member.Name;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>        <span style="color: #0000ff">return</span> propertyName;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>    }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span>    <span style="color: #0000ff">public</span> <span style="color: #0000ff">event</span> PropertyChangedEventHandler PropertyChanged;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Coming back to the class example above, the new type safe implementation can be used as below:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyClass</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> myProperty;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> MyProperty</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>         get { <span style="color: #0000ff">return</span> myProperty; }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>         set</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>             <span style="color: #0000ff">if</span>(myProperty != <span style="color: #0000ff">value</span>)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>             {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>                 myProperty = <span style="color: #0000ff">value</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>                 OnPropertyChanged(()=&gt;<span style="color: #0000ff">this</span>.MyProperty);</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>             }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span> }</pre>
<p><!--CRLF--></div>
</div>
<h1>Conclusion</h1>
<p>In this blog article I have presented a way of implementing a type-safe notifier for the INotifyPropertyChanged interface. The implementation presented here uses expression trees for achieving this.</p>
<p>I haven’t tested the performance overhead of using expression trees, but I guess if this is an issue, you can easily cache the results for further usage.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/XoCpdLsSANknCZPpxGkPxZL3zcc/0/da"><img src="http://feedads.g.doubleclick.net/~a/XoCpdLsSANknCZPpxGkPxZL3zcc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/XoCpdLsSANknCZPpxGkPxZL3zcc/1/da"><img src="http://feedads.g.doubleclick.net/~a/XoCpdLsSANknCZPpxGkPxZL3zcc/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/YCsrRzWIvnw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2009/08/17/creating-a-type-safe-implementation-of-the-inotifypropertychanged-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2009/08/17/creating-a-type-safe-implementation-of-the-inotifypropertychanged-interface/</feedburner:origLink></item>
		<item>
		<title>Some useful extension methods</title>
		<link>http://feedproxy.google.com/~r/byteflux/kync/~3/Zy_hNszQwII/</link>
		<comments>http://byteflux.me/index.php/2009/08/13/some-useful-extension-methods/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 09:08:00 +0000</pubDate>
		<dc:creator>Sebastian Negomireanu</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://byteflux.me/index.php/2009/08/13/some-useful-extension-methods/</guid>
		<description><![CDATA[The class at the end of this post contains some extension methods that I commonly use in my code:

IDictionary&#60;TKey, TValue&#62;.GetValueOrDefault
IEnumerable&#60;T&#62;.ToHashSet
IEnumerable&#60;T&#62;.ToJoinedString

GetValueOrDefault
GetValueOrDefault is a simple extension method that wraps the IDictionary.TryGetValue method. Since the TryGetValue method cannot be used in lambda expressions, it makes it difficult to use dictionary lookups in LINQ, for example. The GetValueOrDefault method [...]]]></description>
			<content:encoded><![CDATA[<p>The class at the end of this post contains some extension methods that I commonly use in my code:</p>
<ul>
<li>IDictionary&lt;TKey, TValue&gt;.GetValueOrDefault</li>
<li>IEnumerable&lt;T&gt;.ToHashSet</li>
<li>IEnumerable&lt;T&gt;.ToJoinedString<span id="more-5"></span></li>
</ul>
<h1><strong>GetValueOrDefault</strong></h1>
<p>GetValueOrDefault is a simple extension method that wraps the IDictionary.TryGetValue method. Since the TryGetValue method cannot be used in lambda expressions, it makes it difficult to use dictionary lookups in LINQ, for example. The GetValueOrDefault method offers a wrapper which silently tries to get a value based on a key. If the value does not exist in the dictionary, it will return either the default value for the value type or a supplied default value.</p>
<h1><strong>ToHashSet</strong></h1>
<p>There are several extension methods in LINQ which allow conversion from an IEnumerable to another collection type (ToList, ToArray, ToDictionary etc.). Sometimes it is useful to convert an IEnumerable to a HashSet, in order to perform quick lookups for a certain value.</p>
<p>The nice thing about the HashSet is that it has an O(1) lookup and insertion complexity, so it is really fast.</p>
<p>The ToHashSet extension method converts a source IEnumerable to a strongly typed HashSet, and has the possibility of supplying an IEqualityComparer. This is nice if the comparison logic is more complex or if you want for example to build a case-insensitive HashSet&lt;string&gt;.</p>
<h1><strong>ToJoinedString</strong></h1>
<p>One of the methods that is very common when working with string arrays is string.Join. Unfortunately, this method can only use string arrays. Of course you can always convert a string collection to an array and then apply the ToString method, but this induces some overhead, especially when the collection is large.</p>
<p>The ToJoinedString extension method does the same thing as string.Join but works on any type of IEnumerable. Internally it uses a StringBuilder object to concatenate strings, so it offers a high performance, even when working with large string collections.</p>
<h1><strong>The Code</strong></h1>
<p>Here is the code for the extension methods. You can just copy &amp; paste the following code in your project and use it as is:</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Collections.Generic;
<span class="kwrd">using</span> System.Linq;
<span class="kwrd">using</span> System.Text;

<span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> GenericCollectionExtensionMethods
{
    <span class="preproc">#region</span> Methods

    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Tries to get a value from a dictionary based on a supplied key. If the value does not exist in</span>
    <span class="rem">/// the dictionary it will return the default value for the value type.</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="rem">/// &lt;typeparam name="TKey"&gt;The type of the key.&lt;/typeparam&gt;</span>
    <span class="rem">/// &lt;typeparam name="TValue"&gt;The type of the value.&lt;/typeparam&gt;</span>
    <span class="rem">/// &lt;param name="dict"&gt;The dictionary.&lt;/param&gt;</span>
    <span class="rem">/// &lt;param name="key"&gt;The key.&lt;/param&gt;</span>
    <span class="rem">/// &lt;returns&gt;</span>
    <span class="rem">/// The value that corresponds to the supplied key or the default value for the value type.</span>
    <span class="rem">/// &lt;/returns&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> TValue GetValueOrDefault&lt;TKey, TValue&gt;(<span class="kwrd">this</span> IDictionary&lt;TKey, TValue&gt; dict, TKey key)
    {
        TValue <span class="kwrd">value</span>;
        <span class="kwrd">if</span> (dict.TryGetValue(key, <span class="kwrd">out</span> <span class="kwrd">value</span>))
        {
            <span class="kwrd">return</span> <span class="kwrd">value</span>;
        }
        <span class="kwrd">else</span>
        {
            <span class="kwrd">return</span> <span class="kwrd">default</span>(TValue);
        }
    }

    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Tries to get a value from a dictionary based on a supplied key. If the value does not exist in</span>
    <span class="rem">/// the dictionary it will return the supplied value.</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="rem">/// &lt;typeparam name="TKey"&gt;The type of the key.&lt;/typeparam&gt;</span>
    <span class="rem">/// &lt;typeparam name="TValue"&gt;The type of the value.&lt;/typeparam&gt;</span>
    <span class="rem">/// &lt;param name="dict"&gt;The dictionary.&lt;/param&gt;</span>
    <span class="rem">/// &lt;param name="key"&gt;The key.&lt;/param&gt;</span>
    <span class="rem">/// &lt;param name="defaultValue"&gt;The default value.&lt;/param&gt;</span>
    <span class="rem">/// &lt;returns&gt;</span>
    <span class="rem">/// The value that corresponds to the supplied key or the value of the </span>
    <span class="rem">/// &lt;paramref name="defaultValue"/&gt; parameter.</span>
    <span class="rem">/// &lt;/returns&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> TValue GetValueOrDefault&lt;TKey, TValue&gt;(<span class="kwrd">this</span> IDictionary&lt;TKey, TValue&gt; dict, TKey key, TValue defaultValue)
    {
        TValue <span class="kwrd">value</span>;
        <span class="kwrd">if</span> (dict.TryGetValue(key, <span class="kwrd">out</span> <span class="kwrd">value</span>))
        {
            <span class="kwrd">return</span> <span class="kwrd">value</span>;
        }
        <span class="kwrd">else</span>
        {
            <span class="kwrd">return</span> <span class="kwrd">default</span>(TValue);
        }
    }

    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Converts the source to a hash set.</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="rem">/// &lt;typeparam name="TSource"&gt;The type of the source.&lt;/typeparam&gt;</span>
    <span class="rem">/// &lt;param name="source"&gt;The source enumerable.&lt;/param&gt;</span>
    <span class="rem">/// &lt;returns&gt;A typed hash set containing the items from the source enumerable.&lt;/returns&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> HashSet&lt;TSource&gt; ToHashSet&lt;TSource&gt;(<span class="kwrd">this</span> IEnumerable&lt;TSource&gt; source)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> HashSet&lt;TSource&gt;(source);
    }

    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Converts the source to a hash set.</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="rem">/// &lt;typeparam name="TSource"&gt;The type of the source.&lt;/typeparam&gt;</span>
    <span class="rem">/// &lt;param name="source"&gt;The source enumerable.&lt;/param&gt;</span>
    <span class="rem">/// &lt;param name="comparer"&gt;</span>
    <span class="rem">/// The comparer used by the hash set. This is useful for example when building a case insensitive</span>
    <span class="rem">/// hash set.</span>
    <span class="rem">/// &lt;/param&gt;</span>
    <span class="rem">/// &lt;returns&gt;A typed hash set containing the items from the source enumerable.&lt;/returns&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> HashSet&lt;TSource&gt; ToHashSet&lt;TSource&gt;(<span class="kwrd">this</span> IEnumerable&lt;TSource&gt; source, IEqualityComparer&lt;TSource&gt; comparer)
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> HashSet&lt;TSource&gt;(source, comparer);
    }

    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Joins the string representation of all items in an IEnumerable to a string, using a separator.</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="rem">/// &lt;typeparam name="T"&gt;The type of the elements contained in source collection.&lt;/typeparam&gt;</span>
    <span class="rem">/// &lt;param name="source"&gt;The source collection&lt;/param&gt;</span>
    <span class="rem">/// &lt;param name="separator"&gt;The separator used to glue items together&lt;/param&gt;</span>
    <span class="rem">/// &lt;returns&gt;</span>
    <span class="rem">/// A string obtained by glueing the collection items with the separator, similar to string.Join</span>
    <span class="rem">/// &lt;/returns&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> ToJoinedString&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; source, <span class="kwrd">string</span> separator)
    {
        var sb = <span class="kwrd">new</span> StringBuilder();
        <span class="kwrd">bool</span> first = <span class="kwrd">true</span>;
        <span class="kwrd">foreach</span> (var item <span class="kwrd">in</span> source)
        {
            <span class="kwrd">if</span> (first)
            {
                first = <span class="kwrd">false</span>;
            }
            <span class="kwrd">else</span>
            {
                sb.Append(separator);
            }

            sb.Append(item.ToString());
        }

        <span class="kwrd">return</span> sb.ToString();
    }

    <span class="preproc">#endregion</span> Methods
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>

<p><a href="http://feedads.g.doubleclick.net/~a/BnkFJFFt_Zwmtm_CLSBj6Xmxip0/0/da"><img src="http://feedads.g.doubleclick.net/~a/BnkFJFFt_Zwmtm_CLSBj6Xmxip0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/BnkFJFFt_Zwmtm_CLSBj6Xmxip0/1/da"><img src="http://feedads.g.doubleclick.net/~a/BnkFJFFt_Zwmtm_CLSBj6Xmxip0/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/byteflux/kync/~4/Zy_hNszQwII" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://byteflux.me/index.php/2009/08/13/some-useful-extension-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://byteflux.me/index.php/2009/08/13/some-useful-extension-methods/</feedburner:origLink></item>
	</channel>
</rss>
