<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.NET</title>
        <link>http://blogs.interakting.co.uk/steve/category/53.aspx</link>
        <description>Microsoft .NET Framework (any version)</description>
        <language>en-GB</language>
        <copyright>Stephen Horsfield</copyright>
        <managingEditor>stephen.horsfield@interakting.co.uk</managingEditor>
        <generator>Subtext Version 1.9.5.177</generator>
        <item>
            <title>WCF and EPiServer: Using WCF Web Programming Model Services in EPiServer sites on IIS 7</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/11/20/wcf-and-episerver-using-wcf-web-programming-model-services-in.aspx</link>
            <description>&lt;p&gt;I've just managed to get the &lt;a title="Microsoft .NET Framework" target="_blank" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx"&gt;.NET&lt;/a&gt; 3.5 style of WCF Web Programming Model services to work inside an &lt;a title="EPiServer Content Management System" target="_blank" href="http://www.episerver.com"&gt;EPiServer&lt;/a&gt; 5 R2 site on IIS 7 and Windows 2008.  I had a few difficulties finding out how to integrate the new model into IIS/ASP.NET and then further problems getting it working in EPiServer.  Here, I outline the solution.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I'm currently designing a site that will be using the Akamai Edge Suite of technologies (&lt;a href="http://www.akamai.com"&gt;www.akamai.com&lt;/a&gt;) and so I desire that all client access is through the caching layer.  Additionally, I'm motivated towards a REST model for web services to support a greater degree of caching.&lt;/p&gt;
&lt;p&gt;The new WCF Web Programming Model is ideal for this purpose as I can wrap service parameters directly into the URL and reduce the coding effort considerably.  I had been concerned about the integration of services into the same website as EPiServer because of how the URL mapping works in the Web Programming Model.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The solution is really easy once you realise that the typical EPiServer installation changes the default ASP.NET configuration.  Specifically, you may need the following:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Create a new folder to host your services &lt;/li&gt;
    &lt;li&gt;Create a small web.config in this folder (or use location tags in the main web.config file) and add service handlers &lt;/li&gt;
    &lt;li&gt;Create a ".svc" file for the service &lt;/li&gt;
    &lt;li&gt;Create the supporting .NET service code &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;u&gt;Create a new folder to host your services&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;This is necessary to allow a separate application configuration.  You can change the main application configuration at your own risk, however :)&lt;/p&gt;
&lt;p&gt;For the purposes of this post, I'll use "~/services/".&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Create a small web.config for this folder and add service handlers&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;You'll need to add the following:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0"&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;="UTF-8"&lt;/span&gt;?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.webServer&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;handlers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;add&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="svc-ISAPI"&lt;/span&gt; &lt;span class="attr"&gt;path&lt;/span&gt;&lt;span class="kwrd"&gt;="*.svc"&lt;/span&gt; &lt;span class="attr"&gt;verb&lt;/span&gt;&lt;span class="kwrd"&gt;="*"&lt;/span&gt; &lt;span class="attr"&gt;modules&lt;/span&gt;&lt;span class="kwrd"&gt;="IsapiModule"&lt;/span&gt; 
           &lt;span class="attr"&gt;scriptProcessor&lt;/span&gt;&lt;span class="kwrd"&gt;="%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"&lt;/span&gt;
           &lt;span class="attr"&gt;resourceType&lt;/span&gt;&lt;span class="kwrd"&gt;="Unspecified"&lt;/span&gt; 
           &lt;span class="attr"&gt;preCondition&lt;/span&gt;&lt;span class="kwrd"&gt;="classicMode,runtimeVersionv2.0,bitness32"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;add&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="ServiceHost"&lt;/span&gt; &lt;span class="attr"&gt;path&lt;/span&gt;&lt;span class="kwrd"&gt;="*.svc"&lt;/span&gt; &lt;span class="attr"&gt;verb&lt;/span&gt;&lt;span class="kwrd"&gt;="*"&lt;/span&gt;
           &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="System.ServiceModel.Activation.HttpHandler, System.ServiceModel,
                 Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&lt;/span&gt;
           &lt;span class="attr"&gt;resourceType&lt;/span&gt;&lt;span class="kwrd"&gt;="Unspecified"&lt;/span&gt; &lt;span class="attr"&gt;preCondition&lt;/span&gt;&lt;span class="kwrd"&gt;="integratedMode"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;handlers&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;system.webServer&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now I took this from a cleanly created website with no configuration (using the IIS management console) and I recommend that you do the same.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Create a ".svc" file for the service&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;This file is created in the service folder and forms part of the URL so name it accordingly.  I'll just use "service.svc" for illustration:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="asp"&gt;&amp;lt;%@ ServiceHost Language="C#" Service="Service" CodeBehind="~/App_Code/service.cs"
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You should name your services more appropriately, of course.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Create the supporting .NET service code&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;So, you need to create the classes.  Here, I'm using a simple file in the App_Code folder, but you might prefer to use a separate code project and assembly.  Here's a simple example:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.ServiceModel;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.ServiceModel.Web;

[ServiceContract]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IService
{
  [OperationContract]
  [WebGet(UriTemplate = &lt;span class="str"&gt;"test?s={s}"&lt;/span&gt;)]
  &lt;span class="kwrd"&gt;string&lt;/span&gt; test(&lt;span class="kwrd"&gt;string&lt;/span&gt; s);
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Service : IService
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; test(&lt;span class="kwrd"&gt;string&lt;/span&gt; s)
  {
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="str"&gt;"test: "&lt;/span&gt; + s;
  }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;So, this is a very basic service with a single HTTP GET method that returns some data dependent on a single query string parameter.  A perfect REST example as the result is defined by the input, there is no change of state (so two identical GET requests result in the same value) and so the response is suitable for caching.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Using the service&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;So now, you can access the service as &lt;em&gt;http://your_episerver_site/services/service.svc/test?s=YourTestString&lt;/em&gt;.  ASP.NET integration (and IIS) requires the use of a service extension, in this case ".svc", however this article may help you to remove this issue: &lt;a title="http://blogs.msdn.com/bags/archive/2008/08/22/rest-in-wcf-part-ix-controlling-the-uri.aspx" href="http://blogs.msdn.com/bags/archive/2008/08/22/rest-in-wcf-part-ix-controlling-the-uri.aspx"&gt;http://blogs.msdn.com/bags/archive/2008/08/22/rest-in-wcf-part-ix-controlling-the-uri.aspx&lt;/a&gt; but don't forget that EPiServer already does some URL rewriting and so you'll have to make sure they don't conflict!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: ASP.NET, EPiServer, SOA, WCF, .NET, Windows Server &lt;/li&gt;
    &lt;li&gt;Keywords: Web Programming Model, Windows Communication Foundation, Service Oriented Architecture, AJAX, JSON &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/Windows%20Communication%20Foundation"&gt;Windows Communication Foundation&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/JSON"&gt;JSON&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/WCF"&gt;WCF&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/SOA"&gt;SOA&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/service%20design"&gt;service design&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/Service%20Oriented%20Architecture"&gt;Service Oriented Architecture&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/EPiServer"&gt;EPiServer&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/ASP.NET"&gt;ASP.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/AJAX"&gt;AJAX&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/366.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/11/20/wcf-and-episerver-using-wcf-web-programming-model-services-in.aspx</guid>
            <pubDate>Thu, 20 Nov 2008 17:10:58 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/366.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/11/20/wcf-and-episerver-using-wcf-web-programming-model-services-in.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/366.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET: Using Namespaces with XPath in .NET</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/23/.NET-Using-Namespaces-with-XPath-in-.NET.aspx</link>
            <description>&lt;p&gt;A colleague has been asking how to do this and I've found a great post written years ago.  I've linked it here:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a title="Wayne Allen's Weblog" href="http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx"&gt;XPath &amp;amp; Namespaces (Wayne Allen's Blog)&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt; Versions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Microsoft &lt;a title="Microsoft .NET Framework" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx" target="_blank"&gt;.NET&lt;/a&gt; Framework 2.0 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: Software Development, .NET, XPath &lt;/li&gt;
    &lt;li&gt;Additional keywords: namespace manager &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/XML"&gt;XML&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/namespace"&gt;default namespace&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/XML%20namespace"&gt;XML namespace&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/XPath"&gt;XPath&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/296.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/05/23/.NET-Using-Namespaces-with-XPath-in-.NET.aspx</guid>
            <pubDate>Fri, 23 May 2008 07:38:47 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/296.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/05/23/.NET-Using-Namespaces-with-XPath-in-.NET.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/296.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET: JSON support in WCF</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/22/NET-JSON-support-in-WCF.aspx</link>
            <description>&lt;p&gt;This is just a quick addendum to a colleague's post about using JSON in &lt;a title="Microsoft .NET Framework" target="_blank" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx"&gt;.NET&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Original post&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;See Dan's post here: &lt;a title="http://blogs.interakting.co.uk/danmatthews/archive/2008/05/20/Google-Translate-and-.NET.aspx" target="_blank" href="http://blogs.interakting.co.uk/danmatthews/archive/2008/05/20/Google-Translate-and-.NET.aspx"&gt;Google Translate and .NET (Dan Matthews)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Complete example code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This example translates the phrase "a piece of pie" into French using Google Translate.  I do not use any processing on the JSON output except to convert it into an XML document that can be processed in .NET:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Net;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.IO;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Xml;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Runtime.Serialization.Json;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; JSON_Example
{
    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)
        {
            WebRequest req = WebRequest.Create(
                &lt;span class="str"&gt;"http://ajax.googleapis.com/ajax/"&lt;/span&gt; +
                &lt;span class="str"&gt;"services/language/translate?v=1.0&amp;amp;q="&lt;/span&gt; + 
                HttpUtility.UrlEncode(&lt;span class="str"&gt;"a piece of pie"&lt;/span&gt;) + 
                &lt;span class="str"&gt;"&amp;amp;langpair="&lt;/span&gt; + &lt;span class="str"&gt;"en"&lt;/span&gt; + &lt;span class="str"&gt;"%7C"&lt;/span&gt; + &lt;span class="str"&gt;"fr"&lt;/span&gt;);
            WebResponse res = req.GetResponse();


            XmlDocument doc = &lt;span class="kwrd"&gt;new&lt;/span&gt; XmlDocument();
            &lt;span class="kwrd"&gt;using&lt;/span&gt; (Stream s = res.GetResponseStream())
            {
                XmlDictionaryReader xr = 
                    JsonReaderWriterFactory.CreateJsonReader(
                        s,
                        XmlDictionaryReaderQuotas.Max);

                doc.Load(xr);
                xr.Close();
                s.Close();
            }
            Console.WriteLine(doc.OuterXml);

            Console.ReadKey();
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Required assemblies&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;System.Runtime.Serialization (3.0)&lt;/li&gt;
    &lt;li&gt;System.ServiceModel.Web (3.5)&lt;/li&gt;
    &lt;li&gt;System.Web (2.0)&lt;/li&gt;
    &lt;li&gt;System.Xml (2.0)&lt;/li&gt;
    &lt;li&gt;and the other usual suspects&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Versions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Microsoft .NET Framework 3.5 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: Software Development, JSON, XML, Windows Communication Foundation &lt;/li&gt;
    &lt;li&gt;Additional keywords: JSON parsing, JSON conversion &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/JSON"&gt;JSON&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/WCF"&gt;WCF&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/Windows%20Communication%20Foundation"&gt;Windows Communication Foundation&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/integration"&gt;integration&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/Web%202.0"&gt;Web 2.0&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/293.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/05/22/NET-JSON-support-in-WCF.aspx</guid>
            <pubDate>Thu, 22 May 2008 15:05:30 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/293.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/05/22/NET-JSON-support-in-WCF.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/293.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET: Changing the default namespace of an XmlDocument</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/22/NET-Changing-the-default-namespace-of-an-XmlDocument.aspx</link>
            <description>&lt;p&gt;Have you ever wanted to change the default namespace of an XmlDocument in &lt;a title="Microsoft .NET Framework" target="_blank" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx"&gt;.NET&lt;/a&gt;.  It's not as easy as it might be.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The problem&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Sometimes, particularly in middleware systems, you need to pass a source document to a target system.  The document may not need any changes, but one system will apply namespace declarations in the document and the other won't work with them, or one won't add them and the other will require them.&lt;/p&gt;
&lt;p&gt;There are a few main approaches you can take:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;XSL Transforms &lt;/li&gt;
    &lt;li&gt;Regular expression replacements (or other string manipulations) &lt;/li&gt;
    &lt;li&gt;XmlNode traversal methods (DOM or XmlReader) &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For such a simple replacement process, XSL Transforms are overkill.  Regular expressions suffer in being fairly difficult to write well and to work with different encodings, processing instructions and comments.  Not impossible though.  The third option is my preferred solution.&lt;/p&gt;
&lt;p&gt;The third option has two variations.  The first, creating a new XmlDocument by traversing the original document is fairly heavy as a second XmlDocument is built in memory.  Using the XmlReader allows for a lightweight and efficient method, suitable for handling large documents.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A solution framework&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The following solution has a few sections to it:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;The creation of an appropriate XmlReader &lt;/li&gt;
    &lt;li&gt;The creation of an appropriate XmlWriter &lt;/li&gt;
    &lt;li&gt;Processing of nodes prior to the document element node (XML declaration if present and comments) &lt;/li&gt;
    &lt;li&gt;Processing of the document element and descendent nodes &lt;/li&gt;
    &lt;li&gt;Processing of nodes after the document element node (mainly comments) &lt;/li&gt;
    &lt;li&gt;Clean up &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The code that follows uses string I/O.  You are more likely to be using a stream as input and output.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The solution&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Part 1: Creating an XmlReader&lt;/u&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;string&lt;/span&gt; sourceXml =
    &lt;span class="str"&gt;"&amp;lt;?xml version=\"1.0\" ?&amp;gt;"&lt;/span&gt; +
    &lt;span class="str"&gt;"&amp;lt;!-- comment --&amp;gt;"&lt;/span&gt; +
    &lt;span class="str"&gt;"&amp;lt;elementName b='doc value'&amp;gt;"&lt;/span&gt; +
    &lt;span class="str"&gt;"&amp;lt;subelement a='a value'&amp;gt;"&lt;/span&gt; +
    &lt;span class="str"&gt;"test data"&lt;/span&gt; +
    &lt;span class="str"&gt;"&amp;lt;/subelement&amp;gt;"&lt;/span&gt; +
    &lt;span class="str"&gt;"&amp;lt;!-- comment --&amp;gt;"&lt;/span&gt; +
    &lt;span class="str"&gt;"&amp;lt;/elementName&amp;gt;"&lt;/span&gt; +
    &lt;span class="str"&gt;"&amp;lt;!-- comment --&amp;gt;"&lt;/span&gt;;

StringReader sreader = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringReader(sourceXml);
XmlReader xreader = XmlReader.Create(sreader);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note that the source XML does not include a default namespace.  The purpose of this exercise is to add one.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Part 2: Creating an XmlWriter&lt;/u&gt;&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;StringWriter swriter = &lt;span class="kwrd"&gt;new&lt;/span&gt; StringWriter();

XmlWriterSettings wsettings = &lt;span class="kwrd"&gt;new&lt;/span&gt; XmlWriterSettings();
wsettings.OmitXmlDeclaration = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
wsettings.ConformanceLevel = ConformanceLevel.Document;
wsettings.Indent = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
wsettings.NewLineOnAttributes = &lt;span class="kwrd"&gt;true&lt;/span&gt;;

XmlWriter xwriter = XmlWriter.Create(swriter, wsettings);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The XmlWriterSettings is being used to configure the output, in this case preventing the XmlDeclaration from being output.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Part 3: Processing of nodes prior to the document element&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;A well-formed XML document has exactly one root element node, however it may have comments and other nodes prior to the element node.  These need to be copied verbatim to the output, and that is what the following code does:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;xreader.Read();
&lt;span class="kwrd"&gt;while&lt;/span&gt; (! xreader.EOF)
{
    &lt;span class="rem"&gt;// look for an XML element node&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (xreader.NodeType == XmlNodeType.Element)
        &lt;span class="kwrd"&gt;break&lt;/span&gt;;
    &lt;span class="rem"&gt;// otherwise write the content to the&lt;/span&gt;
    &lt;span class="rem"&gt;// output.  This also reads the next node&lt;/span&gt;
    xwriter.WriteNode(xreader, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note that using the WriteNode() method is an efficient way of handling large amounts of content, and it is also used in the following sections.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Part 4: Processing the document element node&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;The document element consists of one of the following:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;An empty element
    &lt;ul&gt;
        &lt;li&gt;doesn't have any contained nodes but may have attributes &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;A matching pair of start and end element tags
    &lt;ol&gt;
        &lt;li&gt;may have attributes &lt;/li&gt;
        &lt;li&gt;may have any internal structure &lt;/li&gt;
    &lt;/ol&gt;
    &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In this example, a default namespace is added to the node.  It is straightforward to do the reverse and remove a default namespace.  The steps are as follows:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Create an appropriate start element &lt;/li&gt;
    &lt;li&gt;Copy in the attributes &lt;/li&gt;
    &lt;li&gt;Process any descendent content &lt;/li&gt;
    &lt;li&gt;Close the element &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here's the code:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (!xreader.EOF)
{
    &lt;span class="rem"&gt;// process the document element&lt;/span&gt;
    xwriter.WriteStartElement(
        String.Empty, &lt;span class="rem"&gt;// no prefix so the default namespace&lt;/span&gt;
        xreader.LocalName, &lt;span class="rem"&gt;// same local name&lt;/span&gt;
        &lt;span class="str"&gt;"urn:tempuri.org/schema"&lt;/span&gt;); &lt;span class="rem"&gt;// the namespace&lt;/span&gt;

    &lt;span class="rem"&gt;// copy in all attributes&lt;/span&gt;
    xwriter.WriteAttributes(xreader, &lt;span class="kwrd"&gt;true&lt;/span&gt;);

    &lt;span class="rem"&gt;// check if it is an empty element&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (xreader.IsEmptyElement)
    {
        &lt;span class="rem"&gt;// move passed the document element&lt;/span&gt;
        xreader.Read();
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt;
    {
        &lt;span class="rem"&gt;// move to descendent content&lt;/span&gt;
        xreader.Read();
        &lt;span class="kwrd"&gt;while&lt;/span&gt; (!xreader.EOF)
        {
            &lt;span class="rem"&gt;// an end element must close the&lt;/span&gt;
            &lt;span class="rem"&gt;// document element&lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (xreader.NodeType == XmlNodeType.EndElement)
            { 
                &lt;span class="rem"&gt;// move past the document element&lt;/span&gt;
                xreader.Read();
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            }
            &lt;span class="rem"&gt;// write the entire node to the output&lt;/span&gt;
            &lt;span class="rem"&gt;// and move to the following node&lt;/span&gt;
            xwriter.WriteNode(xreader, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
        }
    }
    &lt;span class="rem"&gt;// close the document element&lt;/span&gt;
    xwriter.WriteEndElement();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Step 5: Processing subsequent nodes&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;The steps are identical to the initial nodes:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;while&lt;/span&gt; (! xreader.EOF)
{
    xwriter.WriteNode(xreader, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Step 6: Clean up&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;There are two main operations to complete as part of clean up:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Flushing the output to the stream using XmlWriter.Flush() &lt;/li&gt;
    &lt;li&gt;Closing the various streams, readers and writers &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These steps are left to the reader.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Versions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Microsoft .NET Framework 2.0 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: Software Development, .NET, XML &lt;/li&gt;
    &lt;li&gt;Additional keywords: default namespace, transformation, changing namespace &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/XML"&gt;XML&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/namespace"&gt;default namespace&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/XML%20namespace"&gt;XML namespace&lt;/a&gt;, &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/292.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/05/22/NET-Changing-the-default-namespace-of-an-XmlDocument.aspx</guid>
            <pubDate>Thu, 22 May 2008 10:25:45 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/292.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/05/22/NET-Changing-the-default-namespace-of-an-XmlDocument.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/292.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET: Generics and the Badger Example in C#</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/20/NET-Generics-and-the-Badger-Example-in-C.aspx</link>
            <description>&lt;p&gt;Today I've been talking to some of our developers about generics in C#.  As part of it, we worked through an example, &lt;em&gt;the Badger Example&lt;/em&gt;, which I've reproduced here for those intrigued!&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This document demonstrates the creation of a generic class called Badger. Its purpose is entirely educational and introduces the use of type parameter constraints.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Initial Badger Class&lt;/strong&gt;&lt;/p&gt;

&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Badger&amp;lt;TBadgerCharacter&amp;gt;
{
    TBadgerCharacter _character;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Badger(TBadgerCharacter character)
    {
        &lt;span class="rem"&gt;// TODO: Use the new operator&lt;/span&gt;
        _character = character;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; HelloBadger()
    {
        &lt;span class="rem"&gt;// TODO: Call the Bark method on&lt;/span&gt;
        &lt;span class="rem"&gt;//       _character&lt;/span&gt;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The constructor for this class is passed an instance of a TBadgerCharacter type. The type passed should provide a Bark() method and this method should be called as part of the HelloBadger() method. Additionally, rather than receiving an instance of the TBadgerCharacter type, one should be created in the constructor.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Enabling the new operator with a default constructor&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The first problem can be solved by adding a constraint on the template parameter. The following code will not compile because it does not include such a constraint:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Badger&amp;lt;TBadgerCharacter&amp;gt;
{
    TBadgerCharacter _character;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Badger()
    {
        _character = &lt;span class="kwrd"&gt;new&lt;/span&gt; TBadgerCharacter();
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; HelloBadger()
    {
        &lt;span class="rem"&gt;// TODO: Call the Bark method on&lt;/span&gt;
        &lt;span class="rem"&gt;//       _character&lt;/span&gt;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;This code &lt;u&gt;does not&lt;/u&gt; compile: TBadgerCharacter does not provide a default constructor&lt;/font&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Here’s the code with the required constraint so that it will compile:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Badger&amp;lt;TBadgerCharacter&amp;gt;
    &lt;span class="kwrd"&gt;where&lt;/span&gt; TBadgerCharacter : &lt;span class="kwrd"&gt;new&lt;/span&gt;()
{
    TBadgerCharacter _character;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Badger()
    {
        _character = &lt;span class="kwrd"&gt;new&lt;/span&gt; TBadgerCharacter();
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; HelloBadger()
    {
        &lt;span class="rem"&gt;// TODO: Call the Bark method on&lt;/span&gt;
        &lt;span class="rem"&gt;//       _character&lt;/span&gt;
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;font color="#ff0000"&gt;This code &lt;u&gt;does&lt;/u&gt; compile: The new() constraint requires TBadgerCharacter to provide a default constructor&lt;/font&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Enabling the Bark() method with a type constraint&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To enable the Bark() method on TBadgerCharacter there are two requirements. Firstly, a class or interface that declares the Bark() method must be created. Secondly, a constraint must be added to require a compatible type. Here’s the first part of the solution:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;interface&lt;/span&gt; IBadgerCharacter
{
    &lt;span class="kwrd"&gt;void&lt;/span&gt; Bark();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;The first step is to create a class or interface that provides the required method. This pattern of creating an interface for a generic type parameter is very common and good practice.&lt;/font&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The next step is to require the constraint. Here’s the code beforehand:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;interface&lt;/span&gt; IBadgerCharacter
{
    &lt;span class="kwrd"&gt;void&lt;/span&gt; Bark();
}

&lt;span class="kwrd"&gt;class&lt;/span&gt; Badger&amp;lt;TBadgerCharacter&amp;gt;
    &lt;span class="kwrd"&gt;where&lt;/span&gt; TBadgerCharacter : &lt;span class="kwrd"&gt;new&lt;/span&gt;()
{
    TBadgerCharacter _character;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Badger()
    {
        _character = &lt;span class="kwrd"&gt;new&lt;/span&gt; TBadgerCharacter();
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; HelloBadger()
    {
        _character.Bark();
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;font color="#ff0000"&gt;This code &lt;u&gt;does not&lt;/u&gt; compile: The TBadgerCharacter type does not implement the IBadgerCharacter interface or provide a Bark() method.&lt;/font&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Here’s the code with an appropriate constraint:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;interface&lt;/span&gt; IBadgerCharacter
{
    &lt;span class="kwrd"&gt;void&lt;/span&gt; Bark();
}

&lt;span class="kwrd"&gt;class&lt;/span&gt; Badger&amp;lt;TBadgerCharacter&amp;gt;
    &lt;span class="kwrd"&gt;where&lt;/span&gt; TBadgerCharacter : IBadgerCharacter, &lt;span class="kwrd"&gt;new&lt;/span&gt;()
{
    TBadgerCharacter _character;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Badger()
    {
        _character = &lt;span class="kwrd"&gt;new&lt;/span&gt; TBadgerCharacter();
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; HelloBadger()
    {
        _character.Bark();
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;font color="#ff0000"&gt;This code &lt;u&gt;does&lt;/u&gt; compile: The TBadgerCharacter type is required to implement a default constructor and also the IBadgerCharacter interface.&lt;/font&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A final comment about constraints&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Multiple constraints can be placed on a generic template parameter. These constraints fall into the following categories:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;either or neither of a &lt;em&gt;Value Type&lt;/em&gt; or &lt;em&gt;Reference Type&lt;/em&gt; constraint
    &lt;ul&gt;
        &lt;li&gt;use the &lt;em&gt;class&lt;/em&gt; keyword for a reference type &lt;/li&gt;
        &lt;li&gt;use the &lt;em&gt;struct&lt;/em&gt; keyword for a value type &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;zero or more base class and/or interface implementation requirements
    &lt;ul&gt;
        &lt;li&gt;for instance, the &lt;em&gt;IBadgerCharacter&lt;/em&gt; interface in the above example &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;optionally a default constructor constraint
    &lt;ul&gt;
        &lt;li&gt;use the &lt;em&gt;new()&lt;/em&gt; keyword—the parentheses are required &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These categories of constraint must be specified in the order shown. The order of individual classes and interfaces in the second category is not important.&lt;/p&gt;
&lt;p&gt;Constraints on multiple generic template parameter types must be specified with completely separate &lt;em&gt;where&lt;/em&gt; clauses.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Versions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Microsoft &lt;a title="Microsoft .NET Framework" target="_blank" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx"&gt;.NET&lt;/a&gt; Framework 2.0 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: .NET, Software Development, Generics, C# &lt;/li&gt;
    &lt;li&gt;Additional keywords: generic template parameters, type constraints, example &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/CSharp"&gt;CSharp&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/C#"&gt;C#&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/generics"&gt;generics&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/type%20parameters"&gt;type parameters&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/type%20constraints"&gt;type constraints&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/example"&gt;example&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/288.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/05/20/NET-Generics-and-the-Badger-Example-in-C.aspx</guid>
            <pubDate>Tue, 20 May 2008 14:08:30 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/288.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/05/20/NET-Generics-and-the-Badger-Example-in-C.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/288.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET: Encapsulation, hiding and member binding in C#</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/20/NET-Encapsulation-hiding-and-member-binding-in-C.aspx</link>
            <description>&lt;p&gt;I've been looking at the C# specification for the &lt;em&gt;new&lt;/em&gt; keyword when used on a class member declaration and thought I'd share my findings.  If you are a die-hard C# coder then you won't find anything new here, but of course that doesn't mean that you already know it!  Maybe it would be better to check...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The basics&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Encapsulation&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;Encapsulation is a term used in object-oriented design.  It describes the idea of wrapping some functionality in an outer shell.  The outer shell may contain significant functionality or it may just adapt the behaviour of the contained code.&lt;/p&gt;
&lt;p&gt;Further, encapsulation protects the consumer of a class from needing to know about the internal operation of that class.  It allows the programmer to code against a contract of function, rather than needing to know the detail of its implementation.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Binding modes and invocation&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;In C#, and in &lt;a title="Microsoft .NET Framework" target="_blank" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx"&gt;.NET&lt;/a&gt; in general, there are two modes of member binding and invocation.  In this post, compile-time-binding means that the compiler determines what to use and runtime-binding means that the CLR determines what to use.  This will be precisely defined in what follows, after introducing some other terms and keywords.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;The "virtual" and "override" keywords&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;virtual&lt;/em&gt; keyword is used to allow a derived class to change or specify the behaviour of the inherited class.  This is usually used for methods but can also be used for properties.  There are some constraints: the method signature must match.  Additionally, in the inherited class the &lt;em&gt;override&lt;/em&gt; keyword must be used to indicate that the method is intended to be an implementation of the &lt;em&gt;virtual&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;The "new" keyword&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;new&lt;/em&gt; keyword is used to indicate that a member of a derived class with the same name as a member of the inherited class has that name because the author intended to "hide" the member in the inherited class.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Note:  the "new" and "override" keywords are mutually exclusive, however "new" and "virtual" can be used together.&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Context&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Virtual&lt;/em&gt; methods are used throughout the .NET framework.  They allow you to extend the functionality of a class for specific cases.  Sometimes though, a method or property that you would like to be marked as &lt;em&gt;virtual&lt;/em&gt; is not.  In these cases, the &lt;em&gt;override&lt;/em&gt; keyword cannot be used.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;new &lt;/em&gt;keyword is not used frequently as it has the effect of hiding the member of the parent class, though with some important caveats that I'll come on to.  Its main use is to simplify the use of the inherited class by "hiding" certain functionality.&lt;/p&gt;
&lt;p&gt;A particular problem is when you wish to encapsulate some code over which you have no control.  The question is then how to encapsulate the code efficiently, whether the issue is the performance of the solution or the time required to implement it.  I recently posted about a specific example of this, &lt;a title=".NET Enumeration Selector Controls (15 May 2008)" href="http://blogs.interakting.co.uk/steve/archive/2008/05/15/NET--Enumeration-Selector-Controls.aspx"&gt;using a ComboBox control to allow the selection of an enumeration value&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In detail&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;virtual&lt;/em&gt; keyword indicates to the compiler that the invocation of a method should be bound at runtime.  This is more expensive than compile-time-binding, and also prevents certain optimisations and so it is not the default behaviour.  In every case that the &lt;em&gt;virtual &lt;/em&gt;keyword is not specified and not implied (by a derived class using the &lt;em&gt;override&lt;/em&gt; keyword, for example), compile-time-binding is used.  Only the &lt;em&gt;virtual&lt;/em&gt; keyword instructs the compiler to use runtime-binding.&lt;/p&gt;
&lt;p&gt;How does runtime-binding work?  In both compile-time-binding and runtime-binding, the compile determines the member to use on the basis of compile-time type information.  Only the invocation is different.  This is essential to a proper understanding of the &lt;em&gt;virtual&lt;/em&gt; and &lt;em&gt;new&lt;/em&gt; keywords, particularly when used together.  The &lt;em&gt;compile-time &lt;/em&gt;type of the object determines member selection.&lt;/p&gt;
&lt;p&gt;What does this mean for us?  This means that the member selected may differ depending on the declared type of the object.  It means that the &lt;em&gt;virtual&lt;/em&gt; keyword does not play any role in the selection of a type member to use.&lt;/p&gt;
&lt;p&gt;So what does the &lt;em&gt;virtual&lt;/em&gt; keyword do?  The &lt;em&gt;virtual&lt;/em&gt; keyword changes the invocation behaviour.  When applied, the bound member is invoked on the derived type.  For a non-virtual member, the member is invoked on the declared type.&lt;/p&gt;
&lt;p&gt;What about the &lt;em&gt;new &lt;/em&gt;keyword?  The &lt;em&gt;new&lt;/em&gt; keyword specifies that a new member with the same name exists in the derived type in addition to the pre-existing member of the inherited type.  Using the &lt;em&gt;new&lt;/em&gt; keyword adds an additional member to a derived type.&lt;/p&gt;
&lt;p&gt;How does the &lt;em&gt;new &lt;/em&gt;keyword interact with binding and invocation?  It has no effect at all.  In other words, if you declare a type as 'C' then the member will be bound against 'C'.  The invocation will follow the normal rules, depending on whether it is marked as &lt;em&gt;virtual&lt;/em&gt; or not.  The declaration of the inherited type's member is irrelevant to this.  The &lt;em&gt;new&lt;/em&gt; keyword plays no part in the binding or invocation logic, it only extends the definition of the derived type with an additional member.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Some conclusions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The following conclusions can be drawn from this discussion:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;The &lt;em&gt;new&lt;/em&gt; keyword cannot impact the behaviour of an inherited type
    &lt;ul&gt;
        &lt;li&gt;To change the behaviour, references must use the derived type explicitly &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;The &lt;em&gt;new&lt;/em&gt; keyword cannot prevent access to a member of an inherited type
    &lt;ul&gt;
        &lt;li&gt;The inherited type can be fully utilised when a reference to a derived type is first cast to the inherited type &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;The &lt;em&gt;virtual&lt;/em&gt; keyword does not impact binding, only invocation
    &lt;ul&gt;
        &lt;li&gt;All member binding in C# occurs at compile-time &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;The &lt;em&gt;virtual&lt;/em&gt; keyword alone allows manipulation of behaviour by a derived class
    &lt;ul&gt;
        &lt;li&gt;But it depends on the use of the &lt;em&gt;virtual &lt;/em&gt;keyword in the inherited class which may not be within the same domain as the code being written (for example it may be a class from the .NET framework class library) &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;True encapsulation that can fully adapt the behaviour of a class requires one of the following models
    &lt;ul&gt;
        &lt;li&gt;The class may be wrapped in an outer class that only exposes a subset of functionality, or else adapts the behaviour of exposed functionality&lt;/li&gt;
        &lt;li&gt;A wholly new class may be developed&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;Software design is important
    &lt;ul&gt;
        &lt;li&gt;Some decisions are difficult to change later, for example not using the &lt;em&gt;virtual &lt;/em&gt;keyword&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a title="The C# Language Specification 3.0 (Microsoft Word Document from microsoft.com)" target="_blank" href="http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/CSharp%20Language%20Specification.doc"&gt;The C# Language Specification 3.0&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Versions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Microsoft .NET Framework 3.5 &lt;/li&gt;
    &lt;li&gt;Microsoft .NET Framework 2.0 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: Software Development, .NET, C# &lt;/li&gt;
    &lt;li&gt;Additional keywords: .NET internals, virtual keyword, new keyword, binding, invocation &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/CSharp"&gt;CSharp&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/C#"&gt;C#&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/compilation"&gt;compilation&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/.NET%20internals"&gt;.NET internals&lt;/a&gt; &lt;a rel="tag" href="http://technorati.com/tags/architecture"&gt;architecture&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/encapsulation"&gt;encapsulation&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/invocation"&gt;invocation&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/binding"&gt;binding&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/virtual"&gt;virtual&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/new"&gt;new&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/hiding"&gt;hiding&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/286.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/05/20/NET-Encapsulation-hiding-and-member-binding-in-C.aspx</guid>
            <pubDate>Tue, 20 May 2008 09:30:33 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/286.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/05/20/NET-Encapsulation-hiding-and-member-binding-in-C.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/286.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET: Hacking events and manipulating delegates</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/19/NET--Hacking-events-and-manipulating-delegates.aspx</link>
            <description>&lt;p&gt;Have you ever wanted to remove a handler from an event, but haven't had a reference to the object receiving the event, or perhaps you don't even know what type the object has!  &lt;a title="Microsoft .NET Framework" target="_blank" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx"&gt;.NET&lt;/a&gt; hides the details of events but with a bit of help from the Reflection classes, you can edit the handlers for your own purposes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Cautionary note&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In general, this is a really, really, really bad thing to do! I'm writing about it partly because it is educational, and partly because it was an interesting problem to solve.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Behind the scenes of .NET events&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I'm going to be using C# throughout this post, but the comments apply equally to those of you using Visual Basic .NET.&lt;/p&gt;
&lt;p&gt;You declare an event in C# using the &lt;em&gt;event&lt;/em&gt; keyword. In its simplest form it looks like the following:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EventClass
{
  &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; EventHandler MyEvent;
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The compiler converts this into four class members:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;A private, instance field named &lt;em&gt;MyEvent&lt;/em&gt; of type &lt;em&gt;EventHandler&lt;/em&gt; (which is derived from &lt;em&gt;System.MulticastDelegate&lt;/em&gt;) &lt;/li&gt;
    &lt;li&gt;A public, instance event named &lt;em&gt;MyEvent&lt;/em&gt; of type &lt;em&gt;EventHandler&lt;/em&gt; (which wraps the private field) &lt;/li&gt;
    &lt;li&gt;A public, instance method named &lt;em&gt;add_MyEvent&lt;/em&gt; (to attach an event handler) &lt;/li&gt;
    &lt;li&gt;A public, instance method named&lt;em&gt; remove_MyEvent&lt;/em&gt; (to remove an attached event handler) &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To manipulate the handler list you need access to the private field.  This cannot be accomplished using C# code directly.  Instead, you need to use reflection to access the field.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Getting access to the private field&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To get access to the private field, you need to use the reflection classes.  In particular you need to specify that you want to bind to a non-public, instance field.  This is achieved as follows:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// Add a using statement for System.Reflection&lt;/span&gt;
&lt;span class="rem"&gt;// Assumes an instance of EventClass called e&lt;/span&gt;
Type t = e.GetType();
FieldInfo f = t.GetField(&lt;span class="str"&gt;"MyEvent"&lt;/span&gt;,
  BindingFlags.Instance |
  BindingFlags.NonPublic);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The name of the field will match the name of the event, in this case it is &lt;em&gt;MyEvent&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Manipulating the handler list&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The value of the field is either &lt;em&gt;null&lt;/em&gt; or an instance of a &lt;em&gt;MulticastDelegate&lt;/em&gt;.  A &lt;em&gt;MulticastDelegate&lt;/em&gt; wraps a list of &lt;em&gt;System.Delegate&lt;/em&gt; instances.  The list is ordered and so the order should also be preserved within any manipulation.  &lt;em&gt;MulticastDelegates &lt;/em&gt;are also immutable so we need to create a new &lt;em&gt;MulticastDelegate&lt;/em&gt; with only those handlers we want to keep.  The process is as follows:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Retrieve the current value of the private field &lt;/li&gt;
    &lt;li&gt;Check whether any handlers are currently attached &lt;/li&gt;
    &lt;li&gt;Get the list of &lt;em&gt;Delegates&lt;/em&gt; currently attached &lt;/li&gt;
    &lt;li&gt;Create a new, filtered list of &lt;em&gt;Delegates&lt;/em&gt; &lt;/li&gt;
    &lt;li&gt;Create a new &lt;em&gt;MulticastDelegate&lt;/em&gt; using the filtered list of &lt;em&gt;Delegates&lt;/em&gt; &lt;/li&gt;
    &lt;li&gt;Set the value of the field to the newly created &lt;em&gt;MulticastDelegate&lt;/em&gt; &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;u&gt;Step 1: Retrieve the current value of the private field&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;Retrieving the existing value is straightforward:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;object&lt;/span&gt; originalValue = f.GetValue(e);&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Step 2: Check whether any handlers are currently attached&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;The value of the field will be &lt;em&gt;null&lt;/em&gt; if no handlers are attached, so wrap the remaining steps in a conditional block:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (originalValue != &lt;span class="kwrd"&gt;null&lt;/span&gt;) {
    ...
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Step 3: Get the list of Delegates currently attached&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;The value must be cast to the type &lt;em&gt;System.MulticastDelegate&lt;/em&gt;.  Then, the instance method &lt;em&gt;GetInvocationList&lt;/em&gt; of &lt;em&gt;MulticastDelegate&lt;/em&gt; can be used to get the list of attached handlers:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;System.MulticastDelegate originalDelegate =
    (System.MulticastDelegate) originalValue;
System.Delegate[] originalHandlers =
    originalDelegate.GetInvocationList();&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Step 4: Create a new, filtered list of Delegates&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;The following code illustrates how to remove all handlers registered by a class &lt;em&gt;DelegateHacking.A&lt;/em&gt;.  It has not been written particularly efficiently, but it demonstrates the process:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// requires: using System.Collections.Generic;&lt;/span&gt;
List&amp;lt;System.Delegate&amp;gt; newHandlers =
    &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;System.Delegate&amp;gt;();

&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (System.Delegate handler &lt;span class="kwrd"&gt;in&lt;/span&gt; originalHandlers)
{
    &lt;span class="rem"&gt;// compare the type of the class containing&lt;/span&gt;
    &lt;span class="rem"&gt;// the event handler with a known type&lt;/span&gt;
    &lt;span class="rem"&gt;// to be filtered&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (handler.Method.ReflectedType.Equals(
            &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(DelegateHacking.A)))
    {
        &lt;span class="rem"&gt;// don't add any matches to the list&lt;/span&gt;
        &lt;span class="kwrd"&gt;continue&lt;/span&gt;;
    }
    &lt;span class="rem"&gt;// add the handler to the filtered list&lt;/span&gt;
    newHandlers.Add(handler);
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Step 5: Create a new MulticastDelegate using the filtered list of Delegates&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;You cannot create a Delegate instance directly.  Instead, you need to use the static methods of &lt;em&gt;Delegate&lt;/em&gt; or &lt;em&gt;MulticastDelegate&lt;/em&gt;.  In this case, the &lt;em&gt;Combine&lt;/em&gt; method of the &lt;em&gt;MulticastDelegate&lt;/em&gt; class is used:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;System.MulticastDelegate newValue =
    System.MulticastDelegate.Combine(
        newHandlers.ToArray());&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;u&gt;Step 6: Set the value of the field to the newly created MulticastDelegate&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;This final step sets the value of the field to the new delegate.  It is interesting to note that if the list of event handlers had been filtered completely, so that none were left, the value returned by &lt;em&gt;MulticastDelegate.Combine&lt;/em&gt; would be &lt;em&gt;null&lt;/em&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;f.SetValue(e, newValue);&lt;/pre&gt;
&lt;/div&gt;
&lt;strong&gt;&lt;/strong&gt;
&lt;p&gt;&lt;strong&gt;The manipulation in full&lt;/strong&gt;&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Reflection;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EventClass
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; EventHandler MyEvent;
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Test
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ManipulateHandlers(EventClass e)
    {
        Type t = e.GetType();
        FieldInfo f = t.GetField(&lt;span class="str"&gt;"MyEvent"&lt;/span&gt;,
            BindingFlags.Instance |
            BindingFlags.NonPublic);

        &lt;span class="kwrd"&gt;object&lt;/span&gt; originalValue = f.GetValue(e);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (originalValue != &lt;span class="kwrd"&gt;null&lt;/span&gt;) {

            System.MulticastDelegate originalDelegate =
                (System.MulticastDelegate) originalValue;
            System.Delegate[] originalHandlers =
                originalDelegate.GetInvocationList();

            List&amp;lt;System.Delegate&amp;gt; newHandlers =
                &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;System.Delegate&amp;gt;();

            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (System.Delegate handler 
                        &lt;span class="kwrd"&gt;in&lt;/span&gt; originalHandlers)
            {
                &lt;span class="rem"&gt;// compare the type of the class containing&lt;/span&gt;
                &lt;span class="rem"&gt;// the event handler with a known type&lt;/span&gt;
                &lt;span class="rem"&gt;// to be filtered&lt;/span&gt;
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (handler.Method.ReflectedType.Equals(
                        &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(DelegateHacking.A)))
                {
                    &lt;span class="rem"&gt;// don't add any matches to the list&lt;/span&gt;
                    &lt;span class="kwrd"&gt;continue&lt;/span&gt;;
                }
                &lt;span class="rem"&gt;// add the handler to the filtered list&lt;/span&gt;
                newHandlers.Add(handler);
            }

            System.MulticastDelegate newValue =
                System.MulticastDelegate.Combine(
                    newHandlers.ToArray());

            f.SetValue(e, newValue);
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Full example source code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You can download the code for a full console application example here (you'll need to rename it):&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a title="HackingDelegate.cs (CSharp code)" target="_blank" rel="enclosure" href="http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/steve/examples/HackingDelegates.cs.txt"&gt;HackingDelegates.cs&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Acknowledgements&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a title="http://netcode.ru/dotnet/?artID=6699" href="http://netcode.ru/dotnet/?artID=6699"&gt;http://netcode.ru/dotnet/?artID=6699&lt;/a&gt; discusses delegates in detail and was helpful in constructing this post &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Versions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Microsoft .NET Framework 2.0 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: Software Development, .NET, Reflection, Events, Delegates &lt;/li&gt;
    &lt;li&gt;Additional keywords: event handlers, System.Delegate, System.MulticastDelegate &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/CSharp"&gt;CSharp&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/C#"&gt;C#&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/reflection"&gt;reflection&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/delegates"&gt;delegates&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/events"&gt;events&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/.NET%20internals"&gt;.NET internals&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/284.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/05/19/NET--Hacking-events-and-manipulating-delegates.aspx</guid>
            <pubDate>Mon, 19 May 2008 15:28:42 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/284.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/05/19/NET--Hacking-events-and-manipulating-delegates.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/284.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET: Literals and expressions in the C# Language</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/17/NET-Literals-and-expressions-in-the-CSharp-Language.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I was recently asked why some expressions didn't seem to be doing what the developer intended.  Typically, this was due to the use of the &lt;em&gt;short&lt;/em&gt; or &lt;em&gt;byte &lt;/em&gt;data types.  This post is a brief discussion on the related parts of the C# Language Specification (version 3.0).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You can find the C# Language Specification here: &lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Download it here: &lt;a title="C# Language Specification 3.0" href="http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/CSharp%20Language%20Specification.doc"&gt;C# Language Specification 3.0 (MSDN)&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Note that page references in this post may vary slightly from your view of the document due to printable page size differences on your computer.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Introducing literals&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;(pages 40-45 of the specification for those interested)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When you write a constant string or number in a C# program, you are defining a “literal”.  The value of the expression is “literally equivalent” to the code.  Here are some literals:&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="0" width="486" border="0"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="119"&gt;
            &lt;ul&gt;
                &lt;li&gt;“a string” &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="365"&gt;textual string&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="119"&gt;
            &lt;ul&gt;
                &lt;li&gt;5 &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="368"&gt;integer number&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="119"&gt;
            &lt;ul&gt;
                &lt;li&gt;2.750 &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="368"&gt;real number&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="119"&gt;
            &lt;ul&gt;
                &lt;li&gt;true &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="368"&gt;boolean value&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="119"&gt;
            &lt;ul&gt;
                &lt;li&gt;‘C’ &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="368"&gt;character literal&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;I’ve been careful to avoid using the C# types for this, because there is a bit more to know in the numeric cases.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Firstly, you can only define the following numeric literal values directly:&lt;/u&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;int &lt;/li&gt;
    &lt;li&gt;uint &lt;/li&gt;
    &lt;li&gt;long &lt;/li&gt;
    &lt;li&gt;ulong &lt;/li&gt;
    &lt;li&gt;float &lt;/li&gt;
    &lt;li&gt;double (default for real numbers) &lt;/li&gt;
    &lt;li&gt;decimal &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Further, the type of literal can be specified with suffixes (case-insensitive) as follows:&lt;/p&gt;
&lt;table cellspacing="0" cellpadding="0" width="495" border="0"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="113"&gt;
            &lt;ul&gt;
                &lt;li&gt;u &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="380"&gt;unsigned (cannot be negative — only for integer literals)&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="113"&gt;
            &lt;ul&gt;
                &lt;li&gt;l &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="380"&gt;long (as opposed to int type — only for integer literals)&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="113"&gt;
            &lt;ul&gt;
                &lt;li&gt;d &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="380"&gt;double (double type — for real literals)&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="113"&gt;
            &lt;ul&gt;
                &lt;li&gt;f &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="380"&gt;float (float type — for real literals)&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td valign="top" width="113"&gt;
            &lt;ul&gt;
                &lt;li&gt;m &lt;/li&gt;
            &lt;/ul&gt;
            &lt;/td&gt;
            &lt;td valign="top" width="380"&gt;decimal (or money type — for real literals)&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;If the number does not include an exponent or a decimal point (with following digits) then the suffix is required to indicate a real number. &lt;/p&gt;
&lt;p&gt;For an integer literal without a suffix, the type of the constant is the first integer type that can correctly represent the literal from the above list.&lt;/p&gt;
&lt;p&gt;Note there are some exceptions to these rules that won’t generally affect you.  Read the documentation if you want to find out more.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Secondly, some types cannot be directly defined as literal values:&lt;/u&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;byte &lt;/li&gt;
    &lt;li&gt;sbyte &lt;/li&gt;
    &lt;li&gt;short &lt;/li&gt;
    &lt;li&gt;ushort &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Literal assignments to these types work only when the compiler can deduce that the result is acceptable, otherwise normal implicit conversion rules apply.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Understanding assignments&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;(pages 218ff.)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The next part of our problem is the assignment operator.  In its simplest form, the assignment operator looks as follows:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;X   =   Y&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The compiler first determines the variable represented by the expression X.  Then it works out the value of Y.  Finally it attempts to store the value of Y in X, performing any needed implicit conversions.&lt;/p&gt;
&lt;p&gt;Why is this important?  The expression Y may not have the same type as X.  In particular, there may not be an implicit narrowing conversion to X.&lt;/p&gt;
&lt;p&gt;Here’s an example:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;short x = 5;&lt;/p&gt;
&lt;p&gt;short y = x + 5;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This will not compile.  Why? Because the 5 has forced a widening conversion on “x”  resulting in an “int” type expression.  This can’t be converted to a “short” implicitly.&lt;/p&gt;
&lt;p&gt;However, the compiler does perform constant coalescing.  This means that a constant expression can be combined into a single constant.  Additional rules are also applied to allow narrowing assignments that do not result in a loss of data.  For example,&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;short y = 5 + 5;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This statement does compile and executes correctly.  The following statement does not:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;short y = 1000 * 1000;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The compile error given is: &lt;em&gt;Error  Constant value '1000000' cannot be converted to a 'short'.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Understanding expressions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;(pages 129ff.)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Expressions are (mostly) evaluated from left to right.  Different operators have different precedences too, so a multiplication takes priority over an addition.  Each sub expression is evaluated in turn until the entire expression has been computed.  Notice that it will perform widening conversions as needed along the way which may give you unexpected behaviour.&lt;/p&gt;
&lt;p&gt;To see an example of this working as you might expect, have a look here:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/3b1ff23f.aspx"&gt;http://msdn.microsoft.com/en-us/library/3b1ff23f.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Versions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Microsoft &lt;a title="Microsoft .NET Framework" target="_blank" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx"&gt;.NET&lt;/a&gt; Framework 3.5 &lt;/li&gt;
    &lt;li&gt;Microsoft .NET Framework 2.0 &lt;/li&gt;
    &lt;li&gt;C# Language Specification 3.0 &lt;/li&gt;
    &lt;li&gt;C# Language Specification 2.0 &lt;/li&gt;
    &lt;li&gt;&lt;a title="Microsoft Visual Studio" target="_blank" href="http://msdn2.microsoft.com/en-gb/vstudio/default.aspx"&gt;Visual Studio&lt;/a&gt; 2005/8 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: Software Development, .NET, C# &lt;/li&gt;
    &lt;li&gt;Additional keywords: language intrinsics, expressions, assignments, conversions &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/CSharp"&gt;CSharp&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/C%23"&gt;C#&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/expressions"&gt;expressions&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/assignments"&gt;assignments&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/compilation"&gt;compilation&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/conversion"&gt;conversion&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/data types"&gt;data types&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/literals"&gt;literals&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/282.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/05/17/NET-Literals-and-expressions-in-the-CSharp-Language.aspx</guid>
            <pubDate>Sat, 17 May 2008 10:28:27 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/282.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/05/17/NET-Literals-and-expressions-in-the-CSharp-Language.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/282.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET: Enumeration Selector Controls</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/15/NET--Enumeration-Selector-Controls.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I'm in the process of writing a &lt;a title="Microsoft Visual Studio" target="_blank" href="http://msdn2.microsoft.com/en-gb/vstudio/default.aspx"&gt;Visual Studio&lt;/a&gt; template extension to automate the creation of Windows Communication Foundation components.  As part of the Wizard, I want to allow users to configure options based on various enumerations.  To help with this, I've developed a generic control that extends a ComboBox to manage a set of allowed values automatically.  This way, the same code can be used with any &lt;a title="Microsoft .NET Framework" target="_blank" href="http://msdn2.microsoft.com/en-gb/netframework/default.aspx"&gt;.NET&lt;/a&gt; enumeration type.&lt;/p&gt;
&lt;p&gt;I've coded it as a generic type but it would be just as easy to use a runtime-provided type for the functionality.  Indeed, it already uses Reflection to check that the type is an enumeration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Solution overview&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The code derives a class from ComboBox with the following properties:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;The control is a generic class that requires a value-type (struct) as a template parameter &lt;/li&gt;
    &lt;li&gt;The control can (optionally) allow non-selection using a Nullable&amp;lt;T&amp;gt; value &lt;/li&gt;
    &lt;li&gt;The control hides the internal Items collection to prevent unintentional manipulation of the collection &lt;/li&gt;
    &lt;li&gt;The control &lt;em&gt;does not&lt;/em&gt; provide localisation or pretty names, but this is simple to add &lt;/li&gt;
    &lt;li&gt;The control can be used directly in place of an existing ComboBox control &lt;/li&gt;
    &lt;li&gt;Specific implementations can be created by derivation and used immediately &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;u&gt;Class definition&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;The class definition is as follows:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; EnumerationSelectorControl&amp;lt;T&amp;gt; : ComboBox
    &lt;span class="kwrd"&gt;where&lt;/span&gt; T : &lt;span class="kwrd"&gt;struct&lt;/span&gt;
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; AllowNull { ... }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Nullable&amp;lt;T&amp;gt; Value { ... }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ObjectCollection Items { get { &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotSupportedException(); } }
    
    &lt;span class="kwrd"&gt;public&lt;/span&gt; EnumerationSelectorControl() : &lt;span class="kwrd"&gt;base&lt;/span&gt;()
    {
        ...
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;struct&lt;/span&gt; NullMember { ... }
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;struct&lt;/span&gt; EnumMember { ... }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As always, the implementation is the interesting part.&lt;/p&gt;
&lt;p&gt;Firstly, note how the Items collection is hidden to the casual observer.&lt;/p&gt;
&lt;p&gt;Secondly, the ComboBox Items collection is populated in the constructor using a combination of the NullMember and EnumMember types.  These types allow the separation of item value and item display.  The AllowNull property manages the existence of a NullMember item in the collection, and the constructor populates the collection with instances of EnumMember based on the output of Enum.GetNames(typeof(T)).&lt;/p&gt;
&lt;p&gt;The implementation of the Value property is responsible for conversion between types and must take into account the value of AllowNull.  It is also responsible for selecting the appropriate item in the collection.&lt;/p&gt;
&lt;p&gt;I'll leave the complete implementation as an exercise for the reader.  Once complete, you can define an enumeration-linked ComboBox as follows:&lt;/p&gt;
&lt;div&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; ProtectionLevelControl :
  EnumerationSelectorControl&amp;lt;global::System.Net.ProtectionLevel&amp;gt;
{}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Versions&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Microsoft .NET Framework 2.0 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Metadata&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Categories: Generics, Enumerations, Software Development, Reflection &lt;/li&gt;
    &lt;li&gt;Additional keywords: ComboBox, enumeration selector, control development &lt;/li&gt;
    &lt;li&gt;Technorati Tags: &lt;a rel="tag" href="http://technorati.com/tags/.NET"&gt;.NET&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/software%20development"&gt;software development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/control%20development"&gt;control development&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/reflection"&gt;reflection&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/generics"&gt;generics&lt;/a&gt;, &lt;a rel="tag" href="http://technorati.com/tags/enumerations"&gt;enumerations&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://blogs.interakting.co.uk/steve/aggbug/278.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stephen Horsfield</dc:creator>
            <guid>http://blogs.interakting.co.uk/steve/archive/2008/05/15/NET--Enumeration-Selector-Controls.aspx</guid>
            <pubDate>Thu, 15 May 2008 08:14:02 GMT</pubDate>
            <wfw:comment>http://blogs.interakting.co.uk/steve/comments/278.aspx</wfw:comment>
            <comments>http://blogs.interakting.co.uk/steve/archive/2008/05/15/NET--Enumeration-Selector-Controls.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.interakting.co.uk/steve/comments/commentRss/278.aspx</wfw:commentRss>
        </item>
        <item>
            <title>WCF: Visual Studio C# Code Snippets for Windows Communication Foundation</title>
            <link>http://blogs.interakting.co.uk/steve/archive/2008/05/06/WCF-Visual-Studio-C-Code-Snippets-for-Windows-Communication-Foundation.aspx</link>
            <description>&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Following my earlier post about WCF service design guidelines, I've created some WCF code snippets to simplify the implementation of services, operations, messages and data contracts.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Resources&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Download the snippet here:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a title="Cpde snippets for Windows Communication Foundation" type="application/xml" rel="enclosure" href="http://blogs.interakting.co.uk/images/blogs_interakting_co_uk/steve/visual-studio-snippets/wcf-services.snippet.xml"&gt;wcf-services.snippet&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;(note that you will need to save the file with the .snippet extension; I've had to rename the extension for this server)&lt;/em&gt; &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Content&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I've included the full conte&lt;a&gt;&lt;/a&gt;nt here, partly to support searches:&lt;/p&gt;
&lt;div class="csharpcode-wrapper"&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0"&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;="utf-8"&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CodeSnippets&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;&amp;lt;!-- wcf-service-contract --&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CodeSnippet&lt;/span&gt; &lt;span class="attr"&gt;Format&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0.0"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Author&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Stephen Horsfield&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Author&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Description&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;This snippet creates a basic WCF service contract outline&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Description&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keywords&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;WCF&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Contract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keywords&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Shortcut&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;wcf-service-contract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Shortcut&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SnippetTypes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SnippetType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Expansion&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;SnippetType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;SnippetTypes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;wcf-service-contract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Snippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;References&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.ServiceModel.dll&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.Runtime.Serialization.dll&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;References&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Declarations&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;NS&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;The CLR namespace of the interface definition&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;ServiceContracts&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;ServiceClassName&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name the service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;ServiceContract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Namespace&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;The namespace of the service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;urn:tempuri.org/services/&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;ProtectionLevel&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;The minimum ProtectionLevel of the service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;None&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt; &lt;span class="attr"&gt;Editable&lt;/span&gt;&lt;span class="kwrd"&gt;="false"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;PL&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SimpleTypeName(global::System.Net.Security.ProtectionLevel)&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;global::System.Net.Security.ProtectionLevel&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt; &lt;span class="attr"&gt;Editable&lt;/span&gt;&lt;span class="kwrd"&gt;="false"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SM&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SimpleTypeName(global::System.ServiceModel.SessionMode)&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;global::System.ServiceModel.SessionMode&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SessionMode&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;The session mode of the service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.ServiceModel.SessionMode&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Allowed&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt; &lt;span class="attr"&gt;Editable&lt;/span&gt;&lt;span class="kwrd"&gt;="false"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;sysSvcContract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SimpleTypeName(global::System.ServiceModel.ServiceContractAttribute)&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;access&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;public&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Declarations&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Code&lt;/span&gt; &lt;span class="attr"&gt;Kind&lt;/span&gt;&lt;span class="kwrd"&gt;="file"&lt;/span&gt; &lt;span class="attr"&gt;Language&lt;/span&gt;&lt;span class="kwrd"&gt;="CSharp"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;!&lt;/span&gt;[CDATA[/*
  $NS$.$ServiceClassName$
  
  This is a Windows Communication Foundation Service Contract definition for:
  
  Service:   $ServiceClassName$
  Namespace: $Namespace$$ServiceClassName$
*/
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Net.Security;
using System.Runtime.Serialization;
using System.Xml;

namespace $NS$ {

    /// &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;summary&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    /// $ServiceClassName$ is a Windows Communication Foundation service contract.
    /// The contract definition is defined as follows:
    /// 
    /// Name:             $ServiceClassName$
    /// Namespace:        $Namespace$$ServiceClassName$
    /// Session Mode:     $SessionMode$
    /// Protection Level: $ProtectionLevel$
    /// &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;summary&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    [$sysSvcContract$(
        Name="$ServiceClassName$",
        Namespace="$Namespace$$ServiceClassName$",
        ProtectionLevel=$PL$.$ProtectionLevel$,
        SessionMode=$SM$.$SessionMode$
        )]
    $access$ interface $ServiceClassName$
    {

    }
}                
                ]]&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Snippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;CodeSnippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;&amp;lt;!-- wcf-service-contract-duplex --&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CodeSnippet&lt;/span&gt; &lt;span class="attr"&gt;Format&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0.0"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Author&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Stephen Horsfield&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Author&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Description&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;This snippet creates a duplexed pair of WCF service contract outlines&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Description&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keywords&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;WCF&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Contract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keywords&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Shortcut&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;wcf-service-contract-duplex&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Shortcut&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SnippetTypes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SnippetType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Expansion&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;SnippetType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;SnippetTypes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;wcf-service-contract-duplex&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Snippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;References&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.ServiceModel.dll&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.Runtime.Serialization.dll&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;References&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Declarations&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;NS&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;The CLR namespace of the interface definition&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;ServiceContracts&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;ServiceClassName&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Name the service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;ServiceContract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Namespace&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;The namespace of the service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;urn:tempuri.org/services/&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;ProtectionLevel&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;The minimum ProtectionLevel of the service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ToolTip&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;global::System.Net.Security.ProtectionLevel&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Type&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;None&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt; &lt;span class="attr"&gt;Editable&lt;/span&gt;&lt;span class="kwrd"&gt;="false"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;PL&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SimpleTypeName(global::System.Net.Security.ProtectionLevel)&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;global::System.Net.Security.ProtectionLevel&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt; &lt;span class="attr"&gt;Editable&lt;/span&gt;&lt;span class="kwrd"&gt;="false"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SM&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SimpleTypeName(global::System.ServiceModel.SessionMode)&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;global::System.ServiceModel.SessionMode&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt; &lt;span class="attr"&gt;Editable&lt;/span&gt;&lt;span class="kwrd"&gt;="false"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;sysSvcContract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;SimpleTypeName(global::System.ServiceModel.ServiceContractAttribute)&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Function&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;access&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;public&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Default&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Declarations&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Code&lt;/span&gt; &lt;span class="attr"&gt;Kind&lt;/span&gt;&lt;span class="kwrd"&gt;="file"&lt;/span&gt; &lt;span class="attr"&gt;Language&lt;/span&gt;&lt;span class="kwrd"&gt;="CSharp"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;!&lt;/span&gt;[CDATA[/*
  $NS$.$ServiceClassName$
  
  This is a Windows Communication Foundation Service Contract definition for a duplex pair
  of services:
  
  Services:  $ServiceClassName$, $ServiceClassName$Callback
  Namespace: $Namespace$$ServiceClassName$
*/
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Net.Security;
using System.Runtime.Serialization;
using System.Xml;

namespace $NS$ {

    /// &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;summary&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    /// $ServiceClassName$ is a Windows Communication Foundation service contract.
    /// Together with $ServiceClassName$Callback these form a duplex pair.
    /// The contract definition is defined as follows:
    /// 
    /// Name:              $ServiceClassName$
    /// Namespace:         $Namespace$$ServiceClassName$
    /// Session Mode:      Required
    /// Protection Level:  $ProtectionLevel$
    /// Callback Contract: $ServiceClassName$Callback
    /// &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;summary&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    [$sysSvcContract$(
        Name="$ServiceClassName$",
        Namespace="$Namespace$$ServiceClassName$",
        ProtectionLevel=$PL$.$ProtectionLevel$,
        SessionMode=$SM$.Required,
        CallbackContract=typeof($ServiceClassName$Callback)
        )]
    $access$ interface $ServiceClassName$
    {

    }

    /// &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;summary&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    /// $ServiceClassName$Callback is a Windows Communication Foundation service
    /// contract. Together with $ServiceClassName$ these form a duplex pair.
    /// The contract definition is defined as follows:
    /// 
    /// Name:              $ServiceClassName$Callback
    /// Namespace:         $Namespace$$ServiceClassName$
    /// Session Mode:      Required
    /// Protection Level:  $ProtectionLevel$
    /// Primary Contract:  $ServiceClassName$
    /// &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;summary&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    [$sysSvcContract$(
        Name="$ServiceClassName$",
        Namespace="$Namespace$$ServiceClassName$",
        ProtectionLevel=$PL$.$ProtectionLevel$,
        SessionMode=$SM$.Required
        )]
    $access$ interface $ServiceClassName$Callback
    {

    }
}                
                ]]&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Code&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Snippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;CodeSnippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;&amp;lt;!-- wcf-service-contract-fullduplex --&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;CodeSnippet&lt;/span&gt; &lt;span class="attr"&gt;Format&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0.0"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Author&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Stephen Horsfield&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Author&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Description&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;This snippet creates a fully duplexed pair of WCF service contract outlines&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Description&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keywords&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;WCF&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Service&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Contract&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keyword&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Keywords&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Shortcut&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;wcf-service-contract-fullduplex&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Shortcut&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SnippetTypes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SnippetType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Expansion&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;SnippetType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;SnippetTypes&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;wcf-service-contract-fullduplex&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Title&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Header&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Snippet&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;References&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.ServiceModel.dll&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;System.Runtime.Serialization.dll&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Assembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;References&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Declarations&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Literal&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                    &