<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Rick Strahl's Web Log</title>
    <link>http://www.west-wind.com/weblog/</link>
    <description>Life, Surf, Code and everything in between</description>
    <generator>West Wind Web Log</generator>
    <language>en-us</language>
    <image>
      <url>http://www.west-wind.com/weblog/images/WebLogBannerLogo.jpg</url>
      <title>Rick Strahl's Web Log</title>
      <link>http://www.west-wind.com/weblog/</link>
    </image>
    <pubDate>Thu, 09 Feb 2012 03:13:42 GMT</pubDate>
    <lastBuildDate>Wed, 08 Feb 2012 10:28:28 GMT</lastBuildDate>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RickStrahl" /><feedburner:info uri="rickstrahl" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>20.906999</geo:lat><geo:long>-156.382029</geo:long><creativeCommons:license>http://creativecommons.org/licenses/by/2.0/</creativeCommons:license><xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /><meta xmlns="http://pipes.yahoo.com" name="pipes" content="noprocess" /><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2FRickStrahl" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FRickStrahl" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2FRickStrahl" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FRickStrahl" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.live.com/?add=http%3A%2F%2Ffeeds.feedburner.com%2FRickStrahl" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><item>
      <title>Creating a dynamic, extensible C# Expando Object</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/MS9XK_eWO1s/Creating-a-dynamic-extensible-C-Expando-Object</link>
      <guid isPermaLink="false">1262439_201202081028</guid>
      <pubDate>Wed, 08 Feb 2012 10:28:28 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2012/Feb/08/Creating-a-dynamic-extensible-C-Expando-Object#Comments</comments>
      <slash:comments>5</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1262439</wfw:commentRss>
      <category>CSharp</category>
      <category>.NET</category>
      <category>Dynamic Types</category>
      <description>&lt;p&gt;I love dynamic functionality in a strongly typed language because it offers us the best of both worlds. In C# (or any of the main .NET languages) we now have the &lt;em&gt;dynamic&lt;/em&gt; type that provides a host of dynamic features for the static C# language.&lt;/p&gt; &lt;p&gt;One place where I've found dynamic to be incredibly useful is in building extensible types or types that expose traditionally non-object data (like dictionaries) in easier to use and more readable syntax. I wrote about a couple of these for accessing old school ADO.NET &lt;a href="http://www.west-wind.com/weblog/posts/2011/Nov/24/Creating-a-Dynamic-DataRow-for-easier-DataRow-Syntax" target="_blank"&gt;DataRows&lt;/a&gt; and &lt;a href="http://www.west-wind.com/weblog/posts/2011/Dec/06/Creating-a-Dynamic-DataReader-for-easier-Property-Access" target="_blank"&gt;DataReaders&lt;/a&gt; more easily for example. These classes are dynamic wrappers that provide easier syntax and auto-type conversions which greatly simplifies code clutter and increases clarity in existing code.&lt;/p&gt; &lt;h3&gt;ExpandoObject in .NET 4.0&lt;/h3&gt; &lt;p&gt;Another great use case for dynamic objects is the ability to create extensible objects - objects that start out with a set of static members and then can add additional properties and even methods dynamically. The .NET 4.0 framework actually includes an &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx" target="_blank"&gt;ExpandoObject class&lt;/a&gt; which provides a very dynamic object that allows you to add properties and methods on the fly and then access them again.&lt;/p&gt; &lt;p&gt;For example with ExpandoObject you can do stuff like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;expand = &lt;span style="color: blue"&gt;new &lt;/span&gt;ExpandoObject();

expand.Name = &lt;span style="color: #a31515"&gt;"Rick"&lt;/span&gt;;
expand.HelloWorld = (&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;) ((&lt;span style="color: blue"&gt;string &lt;/span&gt;name) =&amp;gt; 
{ 
    &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #a31515"&gt;"Hello " &lt;/span&gt;+ name; 
});

&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(expand.Name);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(expand.HelloWorld(&lt;span style="color: #a31515"&gt;"Dufus"&lt;/span&gt;));&lt;/pre&gt;
&lt;p&gt;Internally ExpandoObject uses a Dictionary like structure and interface to store properties and methods and then allows you to add and access properties and methods easily. As cool as ExpandoObject is it has a few shortcomings too:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It's a sealed type so you can't use it as a base class 
&lt;li&gt;It only works off 'properties' in the internal Dictionary - you can't expose existing type data 
&lt;li&gt;It doesn't serialize to XML or with DataContractSerializer/DataContractJsonSerializer&lt;/li&gt;&lt;/ul&gt;
&lt;h3&gt;Expando - A truly extensible Object&lt;/h3&gt;
&lt;p&gt;ExpandoObject is nice if you just need a dynamic container for a dictionary like structure. However, if you want to build an extensible object that starts out with a set of strongly typed properties and then allows you to extend it, ExpandoObject does not work because it's a sealed class that can't be inherited.&lt;/p&gt;
&lt;p&gt;I started thinking about this very scenario for one of my applications I'm building for a customer. In this system we are connecting to various different user stores. Each user store has the same basic requirements for username, password, name etc. But then each store also has a number of extended properties that is available to each application. In the real world scenario the data is loaded from the database in a data reader and the known properties are assigned from the known fields in the database. All unknown fields are then 'added' to the expando object dynamically.&lt;/p&gt;
&lt;p&gt;In the past I've done this very thing with a separate property - Properties - just like I do for this class. But the property and dictionary syntax is not ideal and tedious to work with.&lt;/p&gt;
&lt;p&gt;I started thinking about how to represent these extra property structures. One way certainly would be to add a Dictionary, or an ExpandoObject to hold all those extra properties. But wouldn't it be nice if the application could actually extend an existing object that looks something like this as you can with the Expando object:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;User &lt;/span&gt;: Westwind.Utilities.Dynamic.&lt;span style="color: #2b91af"&gt;Expando
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Email { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Password { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;Active { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;? ExpiresOn { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}

&lt;/pre&gt;
&lt;p&gt;and then simply start extending the properties of this object dynamically? Using the Expando object I describe later you can now do the following:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;UserExampleTest()
{            
    &lt;span style="color: blue"&gt;var &lt;/span&gt;user = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;User&lt;/span&gt;();

    &lt;span style="color: green"&gt;// Set strongly typed properties
    &lt;/span&gt;user.Email = &lt;span style="color: #a31515"&gt;"rick@west-wind.com"&lt;/span&gt;;
    user.Password = &lt;span style="color: #a31515"&gt;"nonya123"&lt;/span&gt;;
    user.Name = &lt;span style="color: #a31515"&gt;"Rickochet"&lt;/span&gt;;
    user.Active = &lt;span style="color: blue"&gt;true&lt;/span&gt;;

    &lt;span style="color: green"&gt;// Now add dynamic properties
    &lt;/span&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;duser = user;
    duser.Entered = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;
    duser.Accesses = 1;

    &lt;span style="color: green"&gt;// you can also add dynamic props via indexer
    &lt;/span&gt;user[&lt;span style="color: #a31515"&gt;"NickName"&lt;/span&gt;] = &lt;span style="color: #a31515"&gt;"AntiSocialX"&lt;/span&gt;;
    duser[&lt;span style="color: #a31515"&gt;"WebSite"&lt;/span&gt;] = &lt;span style="color: #a31515"&gt;"http://www.west-wind.com/weblog"&lt;/span&gt;;

&lt;span style="color: green"&gt;
    // Access strong type through dynamic ref
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(user.Name,duser.Name);

    &lt;span style="color: green"&gt;// Access strong type through indexer 
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(user.Password,user[&lt;span style="color: #a31515"&gt;"Password"&lt;/span&gt;]);
    

    &lt;span style="color: green"&gt;// access dyanmically added value through indexer
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(duser.Entered,user[&lt;span style="color: #a31515"&gt;"Entered"&lt;/span&gt;]);
    
    &lt;span style="color: green"&gt;// access index added value through dynamic
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(user[&lt;span style="color: #a31515"&gt;"NickName"&lt;/span&gt;],duser.NickName);
    

    &lt;span style="color: green"&gt;// loop through all properties dynamic AND strong type properties (true)
    &lt;/span&gt;&lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;prop &lt;span style="color: blue"&gt;in &lt;/span&gt;user.GetProperties(&lt;span style="color: blue"&gt;true&lt;/span&gt;))
    { 
        &lt;span style="color: blue"&gt;object &lt;/span&gt;val = prop.Value;
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(val == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            val = &lt;span style="color: #a31515"&gt;"null"&lt;/span&gt;;

        &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(prop.Key + &lt;span style="color: #a31515"&gt;": " &lt;/span&gt;+ val.ToString());
    }
}
&lt;/pre&gt;
&lt;p&gt;As you can see this code somewhat blurs the line between a static and dynamic type. You start with a strongly typed object that has a fixed set of properties. You can then cast the object to dynamic (&lt;a href="http://www.west-wind.com/weblog/posts/2012/Feb/01/Dynamic-Types-and-DynamicObject-References-in-C"&gt;as I discussed in my last post&lt;/a&gt;) and add additional properties to the object. You can also use an indexer to add dynamic properties to the object. &lt;/p&gt;
&lt;p&gt;To access the strongly typed properties you can use either the strongly typed instance, the indexer or the dynamic cast of the object. Personally I think it's kinda cool to have an easy way to access strongly typed properties by string which can make some data scenarios much easier.&lt;/p&gt;
&lt;p&gt;To access the 'dynamically added' properties you can use either the indexer on the strongly typed object, or property syntax on the dynamic cast.&lt;/p&gt;
&lt;p&gt;Using the dynamic type allows all three modes to work on both strongly typed and dynamic properties.&lt;/p&gt;
&lt;p&gt;Finally you can iterate over all properties, both dynamic and strongly typed if you chose. Lots of flexibility.&lt;/p&gt;
&lt;p&gt;Note also that by default the Expando object works against the (this) instance meaning it extends the current object. You can also pass in a separate instance to the constructor in which case that object will be used to iterate over to find properties rather than this.&lt;/p&gt;
&lt;p&gt;Using this approach provides some really interesting functionality when use the dynamic type. To use this we have to add an explicit constructor to the Expando subclass:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;User &lt;/span&gt;: Westwind.Utilities.Dynamic.&lt;span style="color: #2b91af"&gt;Expando
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Email { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Password { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Name { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;Active { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;? ExpiresOn { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;User() : &lt;span style="color: blue"&gt;base&lt;/span&gt;()
    { }

    &lt;span style="color: green"&gt;// only required if you want to mix in seperate instance
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;User(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance)
        : &lt;span style="color: blue"&gt;base&lt;/span&gt;(instance)
    {
    }
}
&lt;/pre&gt;
&lt;p&gt;to allow the instance to be passed. When you do you can now do:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;ExpandoMixinTest()
{
    &lt;span style="color: green"&gt;// have Expando work on Addresses
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;user = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;User&lt;/span&gt;( &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Address&lt;/span&gt;() );

    &lt;span style="color: green"&gt;// cast to dynamicAccessToPropertyTest
    &lt;/span&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;duser = user;

    &lt;span style="color: green"&gt;// Set strongly typed properties
    &lt;/span&gt;duser.Email = &lt;span style="color: #a31515"&gt;"rick@west-wind.com"&lt;/span&gt;;
    user.Password = &lt;span style="color: #a31515"&gt;"nonya123"&lt;/span&gt;;
    
    &lt;span style="color: green"&gt;// Set properties on address object
    &lt;/span&gt;duser.Address = &lt;span style="color: #a31515"&gt;"32 Kaiea"&lt;/span&gt;;
    &lt;span style="color: green"&gt;//duser.Phone = "808-123-2131";

    // set dynamic properties
    &lt;/span&gt;duser.NonExistantProperty = &lt;span style="color: #a31515"&gt;"This works too"&lt;/span&gt;;

    &lt;span style="color: green"&gt;// shows default value Address.Phone value
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(duser.Phone);

}&lt;/pre&gt;
&lt;p&gt;&lt;br&gt;Using the dynamic cast in this case allows you to access *three* different 'objects': The strong type properties, the dynamically added properties in the dictionary and the properties of the instance passed in! Effectively this gives you a way to simulate multiple inheritance (which is scary - so be very careful with this, but you can do it).&lt;/p&gt;
&lt;h3&gt;How Expando works&lt;/h3&gt;
&lt;p&gt;Behind the scenes Expando is a DynamicObject subclass as &lt;a href="http://www.west-wind.com/weblog/posts/2012/Feb/01/Dynamic-Types-and-DynamicObject-References-in-C"&gt;I discussed in my last post&lt;/a&gt;. By implementing a few of DynamicObject's methods you can basically create a type that can trap 'property missing' and 'method missing' operations. When you access a non-existant property a known method is fired that our code can intercept and provide a value for. Internally Expando uses a custom dictionary implementation to hold the dynamic properties you might add to your expandable object.&lt;/p&gt;
&lt;p&gt;Let's look at code first. The code for the Expando type is straight forward and given what it provides relatively short. Here it is.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Linq;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Dynamic;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Reflection;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Westwind.Utilities.Dynamic
{
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Class that provides extensible properties and methods. This
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;dynamic object stores 'extra' properties in a dictionary or
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;checks the actual properties of the instance.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// 
    /// &lt;/span&gt;&lt;span style="color: green"&gt;This means you can subclass this expando and retrieve either
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;native properties or properties from values in the dictionary.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// 
    /// &lt;/span&gt;&lt;span style="color: green"&gt;This type allows you three ways to access its properties:
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// 
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Directly: any explicitly declared properties are accessible
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Dynamic: dynamic cast allows access to dictionary and native properties/methods
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Dictionary: Any of the extended properties are accessible via IDictionary interface
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;[&lt;span style="color: #2b91af"&gt;Serializable&lt;/span&gt;]
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Expando &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DynamicObject&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IDynamicMetaObjectProvider
    &lt;/span&gt;{
        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Instance of object passed in
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;object &lt;/span&gt;Instance;

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Cached type of the instance
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;InstanceType;

        &lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt;[] InstancePropertyInfo
        {
            &lt;span style="color: blue"&gt;get
            &lt;/span&gt;{
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(_InstancePropertyInfo == &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; Instance != &lt;span style="color: blue"&gt;null&lt;/span&gt;)                
                    _InstancePropertyInfo = Instance.GetType().GetProperties(&lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance | &lt;/pre&gt;&lt;pre class="code"&gt;                                                          &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.DeclaredOnly);
                &lt;span style="color: blue"&gt;return &lt;/span&gt;_InstancePropertyInfo;                
            }
        }
        &lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt;[] _InstancePropertyInfo;


        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;String Dictionary that contains the extra dynamic values
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;stored on this object/instance
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;        
        /// &amp;lt;remarks&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Using PropertyBag to support XML Serialization of the dictionary&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/remarks&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyBag &lt;/span&gt;Properties = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyBag&lt;/span&gt;();

        &lt;span style="color: green"&gt;//public Dictionary&amp;lt;string,object&amp;gt; Properties = new Dictionary&amp;lt;string, object&amp;gt;();

        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;This constructor just works off the internal dictionary and any 
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;public properties of this object.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// 
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Note you can subclass Expando.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;Expando() 
        {
            Initialize(&lt;span style="color: blue"&gt;this&lt;/span&gt;);            
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Allows passing in an existing instance variable to 'extend'.        
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;remarks&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;You can pass in null here if you don't want to 
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;check native properties and only check the Dictionary!
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/remarks&amp;gt;
        /// &amp;lt;param name="instance"&amp;gt;&amp;lt;/param&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;Expando(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance)
        {
            Initialize(instance);
        }


        &lt;span style="color: blue"&gt;protected virtual void &lt;/span&gt;Initialize(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance)
        {
            Instance = instance;
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(instance != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
                InstanceType = instance.GetType();           
        }


       &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
       /// &lt;/span&gt;&lt;span style="color: green"&gt;Try to retrieve a member by name first from instance properties
       &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;followed by the collection entries.
       &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
       /// &amp;lt;param name="binder"&amp;gt;&amp;lt;/param&amp;gt;
       /// &amp;lt;param name="result"&amp;gt;&amp;lt;/param&amp;gt;
       /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TryGetMember(&lt;span style="color: #2b91af"&gt;GetMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result)
        {
            result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;

            &lt;span style="color: green"&gt;// first check the Properties collection for member
            &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(Properties.Keys.Contains(binder.Name))
            {
                result = Properties[binder.Name];
                &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
            }


            &lt;span style="color: green"&gt;// Next check for Public properties via Reflection
            &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(Instance != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color: blue"&gt;try
                &lt;/span&gt;{
                    &lt;span style="color: blue"&gt;return &lt;/span&gt;GetProperty(Instance, binder.Name, &lt;span style="color: blue"&gt;out &lt;/span&gt;result);                    
                }
                &lt;span style="color: blue"&gt;catch &lt;/span&gt;{ }
            }

            &lt;span style="color: green"&gt;// failed to retrieve a property
            &lt;/span&gt;result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
            &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
        }


        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Property setter implementation tries to retrieve value from instance 
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;first then into this object
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="binder"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="value"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TrySetMember(&lt;span style="color: #2b91af"&gt;SetMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;object &lt;/span&gt;value)
        {

            &lt;span style="color: green"&gt;// first check to see if there's a native property to set
            &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(Instance != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color: blue"&gt;try
                &lt;/span&gt;{
                    &lt;span style="color: blue"&gt;bool &lt;/span&gt;result = SetProperty(Instance, binder.Name, value);
                    &lt;span style="color: blue"&gt;if &lt;/span&gt;(result)
                        &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
                }
                &lt;span style="color: blue"&gt;catch &lt;/span&gt;{ }
            }
            
            &lt;span style="color: green"&gt;// no match - set or add to dictionary
            &lt;/span&gt;Properties[binder.Name] = value;
            &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Dynamic invocation method. Currently allows only for Reflection based
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;operation (no ability to add methods dynamically).
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="binder"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="args"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="result"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TryInvokeMember(&lt;span style="color: #2b91af"&gt;InvokeMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] args, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(Instance != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color: blue"&gt;try
                &lt;/span&gt;{
                    &lt;span style="color: green"&gt;// check instance passed in for methods to invoke
                    &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(InvokeMethod(Instance, binder.Name, args, &lt;span style="color: blue"&gt;out &lt;/span&gt;result))
                        &lt;span style="color: blue"&gt;return true&lt;/span&gt;;                    
                }
                &lt;span style="color: blue"&gt;catch &lt;/span&gt;{ }
            }

            result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
            &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
        }
        

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Reflection Helper method to retrieve a property
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="instance"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="name"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="result"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;protected bool &lt;/span&gt;GetProperty(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance, &lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(instance == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
                instance = &lt;span style="color: blue"&gt;this&lt;/span&gt;;

            &lt;span style="color: blue"&gt;var &lt;/span&gt;miArray = InstanceType.GetMember(name, &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;/pre&gt;&lt;pre class="code"&gt;                                      &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.GetProperty | &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance);
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(miArray != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; miArray.Length &amp;gt; 0)
            {
                &lt;span style="color: blue"&gt;var &lt;/span&gt;mi = miArray[0];
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(mi.MemberType == &lt;span style="color: #2b91af"&gt;MemberTypes&lt;/span&gt;.Property)
                {
                    result = ((&lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt;)mi).GetValue(instance,&lt;span style="color: blue"&gt;null&lt;/span&gt;);
                    &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
                }
            }

            result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
            &lt;span style="color: blue"&gt;return false&lt;/span&gt;;                
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Reflection helper method to set a property value
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="instance"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="name"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="value"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;protected bool &lt;/span&gt;SetProperty(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance, &lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: blue"&gt;object &lt;/span&gt;value)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(instance == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
                instance = &lt;span style="color: blue"&gt;this&lt;/span&gt;;

            &lt;span style="color: blue"&gt;var &lt;/span&gt;miArray = InstanceType.GetMember(name, &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;/pre&gt;&lt;pre class="code"&gt;                                                       &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.SetProperty | &lt;/pre&gt;&lt;pre class="code"&gt;                                                       &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance);
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(miArray != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; miArray.Length &amp;gt; 0)
            {
                &lt;span style="color: blue"&gt;var &lt;/span&gt;mi = miArray[0];
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(mi.MemberType == &lt;span style="color: #2b91af"&gt;MemberTypes&lt;/span&gt;.Property)
                {
                    ((&lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt;)mi).SetValue(Instance, value, &lt;span style="color: blue"&gt;null&lt;/span&gt;);
                    &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
                }
            }
            &lt;span style="color: blue"&gt;return false&lt;/span&gt;;                
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Reflection helper method to invoke a method
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="instance"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="name"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="args"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;param name="result"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;protected bool &lt;/span&gt;InvokeMethod(&lt;span style="color: blue"&gt;object &lt;/span&gt;instance, &lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] args, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(instance == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
                instance = &lt;span style="color: blue"&gt;this&lt;/span&gt;;

            &lt;span style="color: green"&gt;// Look at the instanceType
            &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;miArray = InstanceType.GetMember(name,
                                    &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.InvokeMethod |
                                    &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance);

            &lt;span style="color: blue"&gt;if &lt;/span&gt;(miArray != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; miArray.Length &amp;gt; 0)
            {
                &lt;span style="color: blue"&gt;var &lt;/span&gt;mi = miArray[0] &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt;;
                result = mi.Invoke(Instance, args);
                &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
            }

            result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
            &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
        }



        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Convenience method that provides a string Indexer 
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;to the Properties collection AND the strongly typed
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;properties of the object by name.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// 
        /// &lt;/span&gt;&lt;span style="color: green"&gt;// dynamic
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;exp["Address"] = "112 nowhere lane"; 
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;// strong
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;var name = exp["StronglyTypedProperty"] as string; 
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;remarks&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;The getter checks the Properties dictionary first
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;then looks in PropertyInfo for properties.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;The setter checks the instance properties before
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;checking the Properties dictionary.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/remarks&amp;gt;
        /// &amp;lt;param name="key"&amp;gt;&amp;lt;/param&amp;gt;
        /// 
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public object this&lt;/span&gt;[&lt;span style="color: blue"&gt;string &lt;/span&gt;key]
        {
            &lt;span style="color: blue"&gt;get
            &lt;/span&gt;{
                &lt;span style="color: blue"&gt;try
                &lt;/span&gt;{
                    &lt;span style="color: green"&gt;// try to get from properties collection first
                    &lt;/span&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;Properties[key];
                }
                &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;KeyNotFoundException &lt;/span&gt;ex)
                {
                    &lt;span style="color: green"&gt;// try reflection on instanceType
                    &lt;/span&gt;&lt;span style="color: blue"&gt;object &lt;/span&gt;result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
                    &lt;span style="color: blue"&gt;if &lt;/span&gt;(GetProperty(Instance, key, &lt;span style="color: blue"&gt;out &lt;/span&gt;result))
                        &lt;span style="color: blue"&gt;return &lt;/span&gt;result;

                    &lt;span style="color: green"&gt;// nope doesn't exist
                    &lt;/span&gt;&lt;span style="color: blue"&gt;throw&lt;/span&gt;;
                }
            }
            &lt;span style="color: blue"&gt;set
            &lt;/span&gt;{
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(Properties.ContainsKey(key))
                {
                    Properties[key] = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
                    &lt;span style="color: blue"&gt;return&lt;/span&gt;;
                }

                &lt;span style="color: green"&gt;// check instance for existance of type first
                &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;miArray = InstanceType.GetMember(key, &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public | &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.GetProperty);
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(miArray != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; miArray.Length &amp;gt; 0)
                    SetProperty(Instance, key, &lt;span style="color: blue"&gt;value&lt;/span&gt;);
                &lt;span style="color: blue"&gt;else
                    &lt;/span&gt;Properties[key] = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
            }
        }&lt;span style="color: blue"&gt;


        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Returns and the properties of 
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="includeProperties"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;,&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; GetProperties(&lt;span style="color: blue"&gt;bool &lt;/span&gt;includeInstanceProperties = &lt;span style="color: blue"&gt;false&lt;/span&gt;)
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(includeInstanceProperties &amp;amp;&amp;amp; Instance != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;prop &lt;span style="color: blue"&gt;in this&lt;/span&gt;.InstancePropertyInfo)
                    &lt;span style="color: blue"&gt;yield return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;(prop.Name, prop.GetValue(Instance, &lt;span style="color: blue"&gt;null&lt;/span&gt;));
            }

            &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;key &lt;span style="color: blue"&gt;in this&lt;/span&gt;.Properties.Keys)
               &lt;span style="color: blue"&gt;yield return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;(key, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Properties[key]);

        }
  

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Checks whether a property exists in the Property collection
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;or as a property on the instance
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        /// &amp;lt;param name="item"&amp;gt;&amp;lt;/param&amp;gt;
        /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public bool &lt;/span&gt;Contains(&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; item, &lt;span style="color: blue"&gt;bool &lt;/span&gt;includeInstanceProperties = &lt;span style="color: blue"&gt;false&lt;/span&gt;)
        {
            &lt;span style="color: blue"&gt;bool &lt;/span&gt;res = Properties.ContainsKey(item.Key);
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(res)
                &lt;span style="color: blue"&gt;return true&lt;/span&gt;;

            &lt;span style="color: blue"&gt;if &lt;/span&gt;(includeInstanceProperties &amp;amp;&amp;amp; Instance != &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            {
                &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;prop &lt;span style="color: blue"&gt;in this&lt;/span&gt;.InstancePropertyInfo)
                {
                    &lt;span style="color: blue"&gt;if &lt;/span&gt;(prop.Name == item.Key)
                        &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
                }
            }

            &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
        }        

    }
}&lt;/pre&gt;
&lt;p&gt;Although the Expando class supports an indexer, it doesn't actually implement IDictionary or even IEnumerable. It only provides the indexer and Contains() and GetProperties() methods, that work against the Properties dictionary AND the internal instance.&lt;/p&gt;
&lt;p&gt;The reason for not implementing IDictionary is that a) it doesn't add much value since you can access the Properties dictionary directly and that b) I wanted to keep the interface to class very lean so that it can serve as an entity type if desired. Implementing these IDictionary (or even IEnumerable) causes LINQ extension methods to pop up on the type which obscures the property interface and would only confuse the purpose of the type. IDictionary and IEnumerable are also problematic for XML and JSON Serialization - the XML Serializer doesn't serialize IDictionary&amp;lt;string,object&amp;gt;, nor does the DataContractSerializer. The JavaScriptSerializer does serialize, but it treats the entire object like a dictionary and doesn't serialize the strongly typed properties of the type, only the dictionary values which is also not desirable. Hence the decision to stick with only implementing the indexer to support the user["CustomProperty"] functionality and leaving iteration functions to the publicly exposed Properties dictionary.&lt;/p&gt;
&lt;p&gt;Note that the Dictionary used here is &lt;a href="http://www.west-wind.com/weblog/posts/2011/Sep/27/An-Xml-Serializable-PropertyBag-Dictionary-Class-for-NET"&gt;a custom &lt;em&gt;PropertyBag &lt;/em&gt;class I created to allow for serialization to work&lt;/a&gt;. One important aspect for my apps is that whatever custom properties get added they have to be accessible to AJAX clients since the particular app I'm working on is a SIngle Page Web app where most of the Web access is through JSON AJAX calls. PropertyBag can serialize to XML and one way serialize to JSON using the JavaScript serializer (not the DCS serializers though).&lt;/p&gt;
&lt;p&gt;The key components that make Expando work in this code are the Properties Dictionary and the TryGetMember() and TrySetMember() methods. The Properties collection is public so if you choose you can explicitly access the collection to get better performance or to manipulate the members in internal code (like loading up dynamic values form a database). &lt;/p&gt;
&lt;p&gt;Notice that TryGetMember() and TrySetMember() both work against the dictionary AND the internal instance to retrieve and set properties. This means that user["Name"] works against native properties of the object as does user["Name"] = "RogaDugDog".&lt;/p&gt;
&lt;h3&gt;What's your Use Case?&lt;/h3&gt;
&lt;p&gt;This is still an early prototype but I've plugged it into one of my customer's applications and so far it's working very well. The key features for me were the ability to easily extend the type with values coming from a database and exposing those values in a nice and easy to use manner. I'm also finding that using this type of object for ViewModels works very well to add custom properties to view models. I suspect there will be lots of uses for this - I've been using the extra dictionary approach to extensibility for years - using a dynamic type to make the syntax cleaner is just a bonus here.&lt;/p&gt;
&lt;p&gt;What can you think of to use this for?&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/RickStrahl/Expando"&gt;Source Code (GitHub)&lt;/a&gt;&lt;/strong&gt; 
&lt;li&gt;&lt;strong&gt;&lt;a href="http://www.west-wind.com/WestwindWebToolkit/"&gt;Also integrated in Westwind.Utilities of the West Wind Web Toolkit&lt;/a&gt;&lt;/strong&gt; 
&lt;li&gt;&lt;strong&gt;&lt;a href="http://nuget.org/packages/Westwind.Utilities"&gt;West Wind Utilities NuGet&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category=CSharp'&gt;CSharp&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=.NET'&gt;.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=Dynamic Types'&gt;Dynamic Types&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="Creating a dynamic, extensible C# Expando Object" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2012/Feb/08/Creating-a-dynamic-extensible-C-Expando-Object"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2012/Feb/08/Creating-a-dynamic-extensible-C-Expando-Object" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=MS9XK_eWO1s:-JHbMK4NKRw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=MS9XK_eWO1s:-JHbMK4NKRw:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=MS9XK_eWO1s:-JHbMK4NKRw:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=MS9XK_eWO1s:-JHbMK4NKRw:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=MS9XK_eWO1s:-JHbMK4NKRw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=MS9XK_eWO1s:-JHbMK4NKRw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=MS9XK_eWO1s:-JHbMK4NKRw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=MS9XK_eWO1s:-JHbMK4NKRw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=MS9XK_eWO1s:-JHbMK4NKRw:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/MS9XK_eWO1s" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2012/Feb/08/Creating-a-dynamic-extensible-C-Expando-Object</feedburner:origLink></item>
    <item>
      <title>Dynamic Types and DynamicObject References in C#</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/wsidMJbslo8/Dynamic-Types-and-DynamicObject-References-in-C</link>
      <guid isPermaLink="false">1256168_201202011047</guid>
      <pubDate>Wed, 01 Feb 2012 10:47:50 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2012/Feb/01/Dynamic-Types-and-DynamicObject-References-in-C#Comments</comments>
      <slash:comments>6</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1256168</wfw:commentRss>
      <category>CSharp</category>
      <category>.NET</category>
      <description>&lt;p&gt;I've been working a bit with C# custom dynamic types for several customers recently and I've seen some confusion in understanding how dynamic types are referenced. This discussion specifically centers around types that implement IDynamicMetaObjectProvider or subclass from DynamicObject as opposed to arbitrary type casts of standard .NET types. IDynamicMetaObjectProvider types&amp;nbsp; are treated special when they are cast to the &lt;em&gt;dynamic &lt;/em&gt;type.&lt;/p&gt; &lt;p&gt;Assume for a second that I've created my own implementation of a custom dynamic type called DynamicFoo which is about as simple of a dynamic class that I can think of:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DynamicObject
&lt;/span&gt;{
    &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; properties = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;();

    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Bar { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;Entered { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TryGetMember(&lt;span style="color: #2b91af"&gt;GetMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result)
    {
        result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(!properties.ContainsKey(binder.Name))
            &lt;span style="color: blue"&gt;return false&lt;/span&gt;;

        result = properties[binder.Name];
        &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
    }

    &lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TrySetMember(&lt;span style="color: #2b91af"&gt;SetMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;object &lt;/span&gt;value)
    {
        properties[binder.Name] = value;
        &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;p&gt;This class has an internal dictionary member and I'm exposing this dictionary member through a dynamic by implementing DynamicObject. This implementation exposes the &lt;em&gt;properties&lt;/em&gt; dictionary so the dictionary keys can be referenced like properties (foo.NewProperty = "Cool!"). I override TryGetMember() and TrySetMember() which are fired at runtime every time you access a 'property' on a dynamic instance of this DynamicFoo type.&lt;/p&gt;
&lt;h3&gt;Strong Typing and Dynamic Casting&lt;/h3&gt;
&lt;p&gt;I now can instantiate and use DynamicFoo in a couple of different ways:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Strong Typing&lt;/strong&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo &lt;/span&gt;fooExplicit = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo&lt;/span&gt;();
&lt;span style="color: blue"&gt;var &lt;/span&gt;fooVar = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo&lt;/span&gt;();&lt;br&gt;&lt;/pre&gt;
&lt;p&gt;These two commands are essentially identical and use strong typing. The compiler generates identical code for both of them. The &lt;em&gt;var&lt;/em&gt; statement is merely a compiler directive to infer the type of fooVar at &lt;em&gt;compile time&lt;/em&gt; and so the type of fooExplicit is DynamicFoo, just like fooExplicit. This is very static - nothing dynamic about it - and it completely ignores the IDynamicMetaObjectProvider implementation of my class above as it's never used.&lt;/p&gt;
&lt;p&gt;Using either of these I can access the native properties:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo &lt;/span&gt;fooExplicit = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo&lt;/span&gt;();&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;font color="#008000"&gt;// static typing assignments&lt;/font&gt;&lt;/pre&gt;&lt;/span&gt;fooVar.Bar = &lt;span style="color: #a31515"&gt;"Barred!"&lt;/span&gt;;
fooExplicit.Entered = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;
&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;// echo back static values
&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(fooVar.Bar);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(fooExplicit.Entered);
&lt;/pre&gt;
&lt;p&gt;but I have no access whatsoever to the &lt;em&gt;properties&lt;/em&gt; dictionary. Basically this creates a strongly typed instance of the type with access only to the strongly typed interface. You get no dynamic behavior at all. The IDynamicMetaObjectProvider features don't kick in until you cast the type to &lt;em&gt;dynamic&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If I try to access a non-existing property on fooExplicit I get a compilation error that tells me that the property doesn't exist. Again, it's clearly and utterly non-dynamic.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dynamic&lt;/strong&gt;&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;fooDynamic = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo&lt;/span&gt;();&lt;/pre&gt;
&lt;p&gt;fooDynamic on the other hand is created as a dynamic type and it's a completely different beast. I can also create a dynamic by simply casting any type to dynamic like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo &lt;/span&gt;fooExplicit = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo&lt;/span&gt;();
&lt;strong&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;fooDynamic = fooExplicit;&lt;/strong&gt;&lt;/pre&gt;
&lt;p&gt;Note that dynamic typically doesn't require an explicit cast as the compiler automatically performs the cast so there's no need to use &lt;em&gt;as dynamic&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Dynamic functionality works at runtime and allows for the dynamic wrapper to look up and call members dynamically. A dynamic type will look for members to access or call in two places:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Using the strongly typed members of the object 
&lt;li&gt;Using theIDynamicMetaObjectProvider Interface methods to access members&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;So rather than statically linking and calling a method or retrieving a property, the dynamic type looks up - at runtime&amp;nbsp; - where the value actually comes from. It's essentially late-binding which allows runtime determination what action to take when a member is accessed at runtime *if* the member you are accessing does not exist on the object. Class members are checked first before IDynamicMetaObjectProvider interface methods are kick in.&lt;/p&gt;
&lt;p&gt;All of the following works with the &lt;em&gt;dynamic&lt;/em&gt; type:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;fooDynamic = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo&lt;/span&gt;();
&lt;span style="color: green"&gt;// dynamic typing assignments
&lt;/span&gt;fooDynamic.NewProperty = &lt;span style="color: #a31515"&gt;"Something new!"&lt;/span&gt;;
fooDynamic.LastAccess = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;

&lt;span style="color: green"&gt;// dynamic assigning static properties
&lt;/span&gt;fooDynamic.Bar = &lt;span style="color: #a31515"&gt;"dynamic barred"&lt;/span&gt;;
fooDynamic.Entered = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;
&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;&lt;br&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;// echo back dynamic values
&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(fooDynamic.NewProperty);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(fooDynamic.LastAccess);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(fooDynamic.Bar);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(fooDynamic.Entered);
&lt;/pre&gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;The dynamic type can access the native class properties (Bar and Entered) and create and read new ones (NewProperty,LastAccess) all using a single type instance which is pretty cool. As you can see it's pretty easy to create an extensible type this way that can dynamically add members at runtime dynamically.&lt;/p&gt;
&lt;h3&gt;The Alter Ego of IDynamicObject &lt;/h3&gt;
&lt;p&gt;The key point here is that all three statements - explicit, var and dynamic - declare a new DynamicFoo(), but the dynamic declaration results in completely different behavior than the first two simply because the type has been cast to dynamic.&lt;/p&gt;
&lt;p&gt;Dynamic binding means that the type loses its typical strong typing, compile time features. You can see this easily in the Visual Studio code editor. As soon as you assign a value to a dynamic you lose Intellisense and you see &lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Dynamic-object-Confusion_F09D/DynamicInDebugger_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="DynamicInDebugger" border="0" alt="DynamicInDebugger" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Dynamic-object-Confusion_F09D/DynamicInDebugger_thumb.png" width="365" height="116"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;which means there's no Intellisense and no compiler type checking on any members you apply to this instance. &lt;/p&gt;
&lt;p&gt;If you're new to the dynamic type it might seem really confusing that a single type can behave differently depending on how it is cast, but that's exactly what happens when you use a type that implements IDynamicMetaObjectProvider. Declare the type as its strong type name and you only get to access the native instance members of the type. Declare or cast it to dynamic and you get dynamic behavior which accesses native members plus it uses IDynamicMetaObjectProvider implementation to handle any missing member definitions by running custom code.&lt;/p&gt;
&lt;p&gt;You can easily cast objects back and forth between dynamic and the original type:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;fooDynamic = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicFoo&lt;/span&gt;();
fooDynamic.NewProperty = &lt;span style="color: #a31515"&gt;"New Property Value"&lt;/span&gt;;             
&lt;span style="color: #2b91af"&gt;DynamicFoo &lt;/span&gt;foo = fooDynamic;
foo.Bar = &lt;span style="color: #a31515"&gt;"Barred"&lt;/span&gt;;&lt;/pre&gt;
&lt;p&gt;Here the code starts out with a dynamic cast and a dynamic assignment. The code then casts back the value to the DynamicFoo. Notice that when casting from dynamic to DynamicFoo and back we typically do not have to specify the cast explicitly - the compiler can induce the type so I don't need to specify as dynamic or as DynamicFoo.&lt;/p&gt;
&lt;h3&gt;Moral of the Story&lt;/h3&gt;
&lt;p&gt;This easy interchange between dynamic and the underlying type is actually super useful, because it allows you to create extensible objects that can expose non-member data stores and expose them as an object interface. You can create an object that hosts a number of strongly typed properties and then cast the object to dynamic and add additional dynamic properties to the same type at runtime. You can easily switch back and forth between the strongly typed instance to access the well-known strongly typed properties and to dynamic for the dynamic properties added at runtime. &lt;/p&gt;
&lt;p&gt;Keep in mind that dynamic object access has quite a bit of overhead and is definitely slower than strongly typed binding, so if you're accessing the strongly typed parts of your objects you definitely want to use a strongly typed reference. Reserve dynamic for the dynamic members to optimize your code.&lt;/p&gt;
&lt;p&gt;The real beauty of dynamic is that with very little effort you can build expandable objects or objects that expose different data stores to an object interface. I'll have more on this in my next post when I create a customized and extensible Expando object based on DynamicObject.&lt;/p&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category=CSharp'&gt;CSharp&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=.NET'&gt;.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="Dynamic Types and DynamicObject References in C#" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2012/Feb/01/Dynamic-Types-and-DynamicObject-References-in-C"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2012/Feb/01/Dynamic-Types-and-DynamicObject-References-in-C" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=wsidMJbslo8:_hoUcAhYfmw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=wsidMJbslo8:_hoUcAhYfmw:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=wsidMJbslo8:_hoUcAhYfmw:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=wsidMJbslo8:_hoUcAhYfmw:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=wsidMJbslo8:_hoUcAhYfmw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=wsidMJbslo8:_hoUcAhYfmw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=wsidMJbslo8:_hoUcAhYfmw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=wsidMJbslo8:_hoUcAhYfmw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=wsidMJbslo8:_hoUcAhYfmw:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/wsidMJbslo8" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2012/Feb/01/Dynamic-Types-and-DynamicObject-References-in-C</feedburner:origLink></item>
    <item>
      <title>Unable to cast transparent proxy to type &amp;lt;type&amp;gt;</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/skG-VegNXKM/Unable-to-cast-transparent-proxy-to-type-type</link>
      <guid isPermaLink="false">1239017_201201132125</guid>
      <pubDate>Fri, 13 Jan 2012 21:25:00 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2012/Jan/13/Unable-to-cast-transparent-proxy-to-type-type#Comments</comments>
      <slash:comments>2</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1239017</wfw:commentRss>
      <category>.NET</category>
      <category>COM</category>
      <description>&lt;p&gt;This is not the first time I've run into this wonderful error while creating new AppDomains in .NET and then trying to load types and access them across App Domains.&lt;/p&gt; &lt;p&gt;In almost all cases the problem I've run into with this error the problem comes from the two AppDomains involved loading different copies of the same type. Unless the types match exactly and come exactly from the same assembly the typecast will fail. The most common scenario is that the types are loaded from different assemblies - as unlikely as that sounds.&lt;/p&gt; &lt;h3&gt;An Example of Failure&lt;/h3&gt; &lt;p&gt;To give some context, I'm working on some old code in &lt;a href="http://www.west-wind.com/wwhelp/" target="_blank"&gt;Html Help Builder&lt;/a&gt; that creates a new AppDomain in order to parse assembly information for documentation purposes. I create a new AppDomain in order to load up an assembly process it and then immediately unload it along with the AppDomain. The AppDomain allows for unloading that otherwise wouldn't be possible as well as isolating my code from the assembly that's being loaded.&lt;/p&gt; &lt;p&gt;The process to accomplish this is fairly established and I use it for lots of applications that use add-in like functionality - basically anywhere where code needs to be isolated and have the ability to be unloaded. My pattern for this is:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Create a new AppDomain  &lt;li&gt;Load a Factory Class into the AppDomain  &lt;li&gt;Use the Factory Class to load additional types from the remote domain&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Here's the relevant code from my TypeParserFactory that creates a domain and then loads a specific type - TypeParser - that is accessed cross-AppDomain in the parent domain:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TypeParserFactory &lt;/span&gt;: System.&lt;span style="color: #2b91af"&gt;MarshalByRefObject&lt;/span&gt;,&lt;span style="color: #2b91af"&gt;IDisposable    
&lt;/span&gt;{&lt;br&gt;    …&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;TypeParser Factory method that loads the TypeParser
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;object into a new AppDomain so it can be unloaded.
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Creates AppDomain and creates type.
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TypeParser &lt;/span&gt;CreateTypeParser() 
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(!CreateAppDomain(&lt;span style="color: blue"&gt;null&lt;/span&gt;))
        &lt;span style="color: blue"&gt;return null&lt;/span&gt;;

&lt;span style="color: gray"&gt;    /// &lt;/span&gt;&lt;span style="color: green"&gt;Create the instance inside of the new AppDomain
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Note: remote domain uses local EXE's AppBasePath!!!
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TypeParser &lt;/span&gt;parser = &lt;span style="color: blue"&gt;null&lt;/span&gt;;

    &lt;span style="color: blue"&gt;try 
    &lt;/span&gt;{
       &lt;span style="color: #2b91af"&gt;Assembly &lt;/span&gt;assembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly();               
&lt;strong&gt;       &lt;span style="color: blue"&gt;string &lt;/span&gt;assemblyPath = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly().Location;
       parser = (&lt;span style="color: #2b91af"&gt;TypeParser&lt;/span&gt;) &lt;span style="color: blue"&gt;this&lt;/span&gt;.LocalAppDomain.CreateInstanceFrom(assemblyPath,
                                              &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;TypeParser&lt;/span&gt;).FullName).Unwrap();                              
&lt;/strong&gt;    }
    &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex)
    {
        &lt;span style="color: blue"&gt;this&lt;/span&gt;.ErrorMessage = ex.GetBaseException().Message;
        &lt;span style="color: blue"&gt;return null&lt;/span&gt;;
    }

    &lt;span style="color: blue"&gt;return &lt;/span&gt;parser;
}

&lt;span style="color: blue"&gt;private bool &lt;/span&gt;CreateAppDomain(&lt;span style="color: blue"&gt;string &lt;/span&gt;lcAppDomain) 
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(lcAppDomain == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
        lcAppDomain = &lt;span style="color: #a31515"&gt;"wwReflection" &lt;/span&gt;+ &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid().ToString().GetHashCode().ToString(&lt;span style="color: #a31515"&gt;"x"&lt;/span&gt;);

    &lt;span style="color: #2b91af"&gt;AppDomainSetup &lt;/span&gt;setup = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AppDomainSetup&lt;/span&gt;();

    &lt;span style="color: green"&gt;// *** Point at current directory
    &lt;/span&gt;setup.ApplicationBase = &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain.BaseDirectory;
    &lt;span style="color: green"&gt;//setup.PrivateBinPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin");

    &lt;/span&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;.LocalAppDomain = &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CreateDomain(lcAppDomain,&lt;span style="color: blue"&gt;null&lt;/span&gt;,setup);

    &lt;span style="color: green"&gt;// Need a custom resolver so we can load assembly from non current path
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain.AssemblyResolve += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ResolveEventHandler&lt;/span&gt;(CurrentDomain_AssemblyResolve);
    
    &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
}
   …&lt;br&gt;}
&lt;/pre&gt;
&lt;p&gt;Note that the classes must be either &lt;em&gt;[Serializable]&lt;/em&gt; (by value) or inherit from &lt;em&gt;MarshalByRefObject &lt;/em&gt;in order to be accessible remotely. Here I need to call methods on the remote object so all classes are MarshalByRefObject.&lt;/p&gt;
&lt;p&gt;The specific problem code is the loading up a new type which points at an assembly that visible both in the current domain and the remote domain and then instantiates a type from it. This is the code in question:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Assembly &lt;/span&gt;assembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly();               
&lt;span style="color: blue"&gt;string &lt;/span&gt;assemblyPath = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly().Location;
parser = (&lt;span style="color: #2b91af"&gt;TypeParser&lt;/span&gt;) &lt;span style="color: blue"&gt;this&lt;/span&gt;.LocalAppDomain.CreateInstanceFrom(assemblyPath,
                                       &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;TypeParser&lt;/span&gt;).FullName).Unwrap();  
&lt;/pre&gt;
&lt;p&gt;The last line of code is what blows up with the &lt;em&gt;Unable to cast transparent proxy to type &amp;lt;type&amp;gt; &lt;/em&gt;error. Without the cast the code actually returns a TransparentProxy instance, but the cast is what blows up. In other words I AM in fact getting a TypeParser instance back but it can't be cast to the TypeParser type that is loaded in the current AppDomain.&lt;/p&gt;
&lt;h3&gt;Finding the Problem&lt;/h3&gt;
&lt;p&gt;To see what's going on I tried using the .NET 4.0 dynamic type on the result and lo and behold it worked with dynamic - the value returned is actually a TypeParser instance:&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #2b91af"&gt;&lt;font color="#000000"&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Assembly &lt;/span&gt;assembly = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly();               
&lt;span style="color: blue"&gt;string &lt;/span&gt;assemblyPath = &lt;span style="color: #2b91af"&gt;Assembly&lt;/span&gt;.GetExecutingAssembly().Location;
&lt;span style="color: blue"&gt;object &lt;/span&gt;objparser = &lt;span style="color: blue"&gt;this&lt;/span&gt;.LocalAppDomain.CreateInstanceFrom(assemblyPath,
                                      &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;TypeParser&lt;/span&gt;).FullName).Unwrap();


&lt;span style="color: green"&gt;// dynamic works
&lt;/span&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;dynParser = objparser;&lt;/strong&gt;
&lt;span style="color: blue"&gt;string &lt;/span&gt;info = dynParser.GetVersionInfo(); &lt;span style="color: green"&gt;// method call works

// casting fails
&lt;/span&gt;parser = (&lt;span style="color: #2b91af"&gt;TypeParser&lt;/span&gt;)objparser; &lt;/pre&gt;&lt;/font&gt;&lt;/span&gt;

&lt;p&gt;So clearly &lt;em&gt;a&lt;/em&gt; TypeParser type is coming back, but nevertheless it's &lt;em&gt;not the right one&lt;/em&gt;. Hmmm… mysterious.&lt;br&gt;Another couple of tries reveal the problem however:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;// works
&lt;/span&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;dynParser = objparser;
&lt;span style="color: blue"&gt;string &lt;/span&gt;info = dynParser.GetVersionInfo(); &lt;span style="color: green"&gt;// method call works

&lt;strong&gt;// c:\wwapps\wwhelp\wwReflection20.dll   (Current Execution Folder)
&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;info3 = &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;TypeParser&lt;/span&gt;).Assembly.CodeBase;

&lt;/strong&gt;&lt;strong&gt;&lt;span style="color: green"&gt;// c:\program files\vfp9\wwReflection20.dll   (my COM client EXE's folder)
&lt;/span&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;info4 = dynParser.GetType().Assembly.CodeBase;&lt;/strong&gt;

&lt;span style="color: green"&gt;// fails
&lt;/span&gt;parser = (&lt;span style="color: #2b91af"&gt;TypeParser&lt;/span&gt;)objparser; 
&lt;/pre&gt;
&lt;p&gt;As you can see the second value is coming from a totally different assembly. Note that this is even though I EXPLICITLY SPECIFIED an assembly path to load the assembly from! Instead .NET decided to load the assembly from the original ApplicationBase folder. Ouch!&lt;/p&gt;
&lt;p&gt;How I actually tracked this down was a little more tedious: I added a method like this to both the factory and the instance types and then compared notes:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;GetVersionInfo()
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #a31515"&gt;".NET Version: " &lt;/span&gt;+ &lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.Version.ToString() + &lt;span style="color: #a31515"&gt;"\r\n" &lt;/span&gt;+
    &lt;span style="color: #a31515"&gt;"wwReflection Assembly: " &lt;/span&gt;+ &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;TypeParserFactory&lt;/span&gt;).Assembly.CodeBase.Replace(&lt;span style="color: #a31515"&gt;"file:///"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;""&lt;/span&gt;).Replace(&lt;span style="color: #a31515"&gt;"/"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"\\"&lt;/span&gt;) + &lt;span style="color: #a31515"&gt;"\r\n" &lt;/span&gt;+
    &lt;span style="color: #a31515"&gt;"Assembly Cur Dir: " &lt;/span&gt;+ &lt;span style="color: #2b91af"&gt;Directory&lt;/span&gt;.GetCurrentDirectory() + &lt;span style="color: #a31515"&gt;"\r\n" &lt;/span&gt;+
    &lt;span style="color: #a31515"&gt;"ApplicationBase: " &lt;/span&gt;+ &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain.SetupInformation.ApplicationBase + &lt;span style="color: #a31515"&gt;"\r\n" &lt;/span&gt;+
    &lt;span style="color: #a31515"&gt;"App Domain: " &lt;/span&gt;+ &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain.FriendlyName + &lt;span style="color: #a31515"&gt;"\r\n"&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;p&gt;For the factory I got:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;.NET Version: 4.0.30319.239&lt;br&gt;&lt;em&gt;wwReflection Assembly: c:\wwapps\wwhelp\bin\wwreflection20.dll&lt;br&gt;&lt;/em&gt;Assembly Cur Dir: c:\wwapps\wwhelp&lt;br&gt;ApplicationBase: C:\Programs\vfp9\&lt;br&gt;App Domain: wwReflection534cfa1f&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For the instance type I got:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;.NET Version: 4.0.30319.239&lt;br&gt;&lt;em&gt;wwReflection Assembly: C:\\Programs\\vfp9\wwreflection20.dll&lt;/em&gt;&lt;br&gt;Assembly Cur Dir: c:\\wwapps\\wwhelp&lt;br&gt;ApplicationBase: C:\\Programs\\vfp9\&lt;br&gt;App Domain: wwDotNetBridge_56006605&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;which clearly shows the problem. You can see that both are loading from different appDomains but the each is loading the assembly from a different location.&lt;/p&gt;
&lt;p&gt;Probably a better solution yet (for ANY kind of assembly loading problem) is to use the &lt;a href="http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.71).aspx" target="_blank"&gt;.NET Fusion Log Viewer&lt;/a&gt; to trace assembly loads.The Fusion viewer will show a load trace for each assembly loaded and where it's looking to find it. Here's what the viewer looks like:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/29a2e6d53a17_18B0/FusionLogViewer_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="FusionLogViewer" border="0" alt="FusionLogViewer" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/29a2e6d53a17_18B0/FusionLogViewer_thumb.png" width="721" height="358"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The last trace above that I found for the second wwReflection20 load (the one that is wonky) looks like this:&lt;/p&gt;&lt;pre&gt;*** Assembly Binder Log Entry  (1/13/2012 @ 3:06:49 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\V4.0.30319\clr.dll
Running under executable  c:\programs\vfp9\vfp9.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = Ras\ricks
LOG: DisplayName = wwReflection20, Version=4.61.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Programs/vfp9/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = vfp9.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Programs\vfp9\vfp9.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\V4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
&lt;strong&gt;LOG: Attempting download of new URL file:///C:/Programs/vfp9/wwReflection20.DLL.
&lt;/strong&gt;LOG: Assembly download was successful. Attempting setup of file: C:\Programs\vfp9\wwReflection20.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: wwReflection20, Version=4.61.0.0, Culture=neutral, PublicKeyToken=null
LOG: Binding succeeds. Returns assembly from C:\Programs\vfp9\wwReflection20.dll.
LOG: Assembly is loaded in default load context.
WRN: The same assembly was loaded into multiple contexts of an application domain:
WRN: Context: Default | Domain ID: 2 | Assembly Name: wwReflection20, Version=4.61.0.0, Culture=neutral, PublicKeyToken=null
WRN: Context: LoadFrom | Domain ID: 2 | Assembly Name: wwReflection20, Version=4.61.0.0, Culture=neutral, PublicKeyToken=null
WRN: This might lead to runtime failures.
WRN: It is recommended to inspect your application on whether this is intentional or not.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.&lt;/pre&gt;
&lt;p&gt;Notice that the fusion log clearly shows that the .NET loader makes no attempt to even load the assembly from the path I explicitly specified.&lt;/p&gt;
&lt;h3&gt;Remember your Assembly Locations&lt;/h3&gt;
&lt;p&gt;As mentioned earlier all failures I've seen like this ultimately resulted from different versions of the same type being available in the two AppDomains. At first sight that seems ridiculous - how could the types be different and why would you have multiple assemblies - but there are actually a number of scenarios where it's quite possible to have multiple copies of the same assembly floating around in multiple places.&lt;/p&gt;
&lt;p&gt;If you're hosting different environments (like hosting the Razor Engine, or ASP.NET Runtime for example) it's common to create a private BIN folder and it's important to make sure that there's no overlap of assemblies.&lt;/p&gt;
&lt;p&gt;In my case of &lt;a href="http://www.west-wind.com/wwhelp/" target="top"&gt;Html Help Builder&lt;/a&gt; the problem started because I'm using COM interop to access the .NET assembly and the above code. COM Interop has very specific requirements on where assemblies can be found and because I was mucking around with the loader code today, I ended up moving assemblies around to a new location for explicit loading. The explicit load works in the main AppDomain, but failed in the remote domain as I showed. The solution here was simple enough: Delete the extraneous assembly which was left around by accident.&lt;/p&gt;
&lt;p&gt;Not a common problem, but one that when it bites is pretty nasty to figure out because it seems so unlikely that types wouldn't match. I know I've run into this a few times and writing this down hopefully will make me remember in the future rather than poking around again for an hour trying to debug the issue as I did today. Hopefully it'll save some of you some time as well in the future.&lt;/p&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category=.NET'&gt;.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=COM'&gt;COM&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="Unable to cast transparent proxy to type &amp;lt;type&amp;gt;" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2012/Jan/13/Unable-to-cast-transparent-proxy-to-type-type"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2012/Jan/13/Unable-to-cast-transparent-proxy-to-type-type" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=skG-VegNXKM:TAKlOkIAvdE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=skG-VegNXKM:TAKlOkIAvdE:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=skG-VegNXKM:TAKlOkIAvdE:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=skG-VegNXKM:TAKlOkIAvdE:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=skG-VegNXKM:TAKlOkIAvdE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=skG-VegNXKM:TAKlOkIAvdE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=skG-VegNXKM:TAKlOkIAvdE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=skG-VegNXKM:TAKlOkIAvdE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=skG-VegNXKM:TAKlOkIAvdE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/skG-VegNXKM" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2012/Jan/13/Unable-to-cast-transparent-proxy-to-type-type</feedburner:origLink></item>
    <item>
      <title>Problems with opening CHM Help files from Network or Internet</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/O6-nQTjl1_0/Problems-with-opening-CHM-Help-files-from-Network-or-Internet</link>
      <guid isPermaLink="false">581253_201201110813</guid>
      <pubDate>Wed, 11 Jan 2012 08:13:00 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2012/Jan/11/Problems-with-opening-CHM-Help-files-from-Network-or-Internet#Comments</comments>
      <slash:comments>48</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=581253</wfw:commentRss>
      <description>&lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;span&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;As a publisher of a Help Creation tool called &lt;a title="" href="http://www.west-wind.com/wwHelp/"&gt;Html Help Help Builder&lt;/a&gt;, I’ve seen a lot of problems with help files that won't properly display actual topic content and displays an error message for topics instead. Here’s the scenario: You go ahead and happily build your fancy, schmanzy Help File for your application and deploy it to your customer. Or alternately you've created a help file and you let your customers download them off the Internet directly or in a zip file.&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;The customer downloads the file, opens the zip file and copies the help file contained in the zip file to disk. She then opens the help file and finds the following unfortunate result: &lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;o:p&gt;&lt;font size="2" face="Verdana"&gt;&lt;br&gt;&lt;img src="http://www.west-wind.com/wwhelp/docs/images/Miscellaneous/ChmTopicError.png"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"&gt;&lt;v:stroke joinstyle="miter"&gt;&lt;/v:stroke&gt;&lt;v:formulas&gt;&lt;v:f eqn="if lineDrawn pixelLineWidth 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 1 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum 0 0 @1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @2 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @3 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @0 0 1"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @6 1 2"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelWidth"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @8 21600 0"&gt;&lt;/v:f&gt;&lt;v:f eqn="prod @7 21600 pixelHeight"&gt;&lt;/v:f&gt;&lt;v:f eqn="sum @10 21600 0"&gt;&lt;/v:f&gt;&lt;/v:formulas&gt;&lt;v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"&gt;&lt;/v:path&gt;&lt;o:lock aspectratio="t" v:ext="edit"&gt;&lt;/o:lock&gt;&lt;/v:shapetype&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;o:p&gt;&lt;font size="2" face="Verdana"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;The help file&amp;nbsp; comes up with all topics in the tree on the left, but a &lt;em&gt;Navigation to the WebPage was cancelled&lt;/em&gt; or &lt;em&gt;Operation Aborted&lt;/em&gt; error in the Help Viewer's content window whenever you try to open a topic. The CHM file obviously opened since the topic list is there, but the Help Viewer refuses to display the content. Looks like a broken help file, right? But it's not - it's merely a Windows security 'feature' that tries to be overly helpful in protecting you. &lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;br&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;The reason this happens is because files downloaded off the Internet - including ZIP files and CHM files contained in those zip files - are marked as as coming from the Internet and so can potentially be malicious, so do not get browsing rights on the local machine – they can’t access local Web content, which is exactly what help topics are. If you look at the URL of a help topic you see something like this:&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;&amp;nbsp; &lt;br&gt;&lt;strong&gt;mk:@MSITStore:C:\wwapps\wwIPStuff\wwipstuff.chm::/indexpage.htm&lt;/strong&gt;&lt;/font&gt; &lt;/font&gt; &lt;p&gt;which points at a special Microsoft Url Moniker that in turn points the CHM file and a relative path within that HTML help file. Try pasting a URL like this into Internet Explorer and you'll see the help topic pop up in your browser (along with a warning most likely). Although the URL looks weird this still equates to a call to the local computer zone, the same as if you had navigated to a local file in IE which by default is not allowed.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Unfortunately, unlike Internet Explorer where you have the option of clicking a security toolbar, the CHM viewer simply refuses to load the page and you get an error page as shown above.&lt;/p&gt;&lt;/span&gt;&lt;span&gt;&lt;font size="2" face="Verdana"&gt;&lt;/font&gt;&lt;/span&gt; &lt;h3&gt;How to Fix This - Unblock the Help File&lt;/h3&gt; &lt;h3 style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;o:p&gt;&lt;font size="2" face="Verdana"&gt;&lt;/font&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/h3&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;font size="2"&gt;&lt;font face="Verdana"&gt;There's a workaround that lets you explicitly 'unblock' a CHM help file. To do this:&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;span&gt;Open Windows Explorer&lt;/span&gt;  &lt;li&gt;&lt;span&gt;Find your CHM file&lt;/span&gt;  &lt;li&gt;&lt;span&gt;Right click and select &lt;em&gt;Properties&lt;/em&gt;&lt;/span&gt;  &lt;li&gt;&lt;span&gt;Click the &lt;em&gt;Unblock&lt;/em&gt; button on the General tab&lt;/span&gt;&lt;span&gt;&lt;o:p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;Here's what the dialog looks like:&lt;/span&gt;&lt;span&gt;&lt;font size="2" face="Verdana"&gt;&lt;br&gt;&lt;/font&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;img src="http://www.west-wind.com/wwhelp/docs/images/Miscellaneous/ChmTopicErrorFix.png"&gt;&amp;nbsp; &lt;br&gt;&lt;span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;o:p&gt;&lt;font size="2" face="Verdana"&gt;Clicking the Unblock button basically, tells Windows that you approve this Help File and allows topics to be viewed.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;Is this insecure? Not unless you're running a really old Version of Windows (XP pre-SP1). In recent versions of Windows Internet Explorer pops up various security dialogs or fires script errors when potentially malicious operations are accessed (like loading Active Controls), so it's relatively safe to run local content in the CHM viewer. Since most help files don't contain script or only load script that runs pure JavaScript access web resources this works fine without issues.&lt;/span&gt;&lt;/p&gt; &lt;h3&gt;How to avoid this Problem&lt;/h3&gt; &lt;p&gt;As an application developer there's a simple solution around this problem: Always install your Help Files with an Installer. The above security warning pop up because Windows can't validate the source of the CHM file. However, if the help file is installed as part of an installation the installation and all files associated with that installation including the help file are trusted. A fully installed Help File of an application works just fine because it is trusted by Windows.&lt;/p&gt; &lt;h3&gt;Summary&lt;/h3&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;br&gt;&lt;span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p style="margin: 0in 0in 0pt" class="MsoNormal"&gt;&lt;span&gt;&lt;o:p&gt;&lt;font size="2" face="Verdana"&gt;It's annoying as all hell that this sort of obtrusive marking is necessary, but it's admittedly a necessary evil because of Microsoft's use of the insecure Internet Explorer engine that drives the CHM Html Engine's topic viewer. Because help files are viewing local content and script is allowed to execute in CHM files there's potential for malicious code hiding in CHM files and the above precautions are supposed to avoid any issues.&lt;/font&gt;&lt;/o:p&gt;&lt;/span&gt; &lt;br&gt;&lt;span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="Problems with opening CHM Help files from Network or Internet" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2012/Jan/11/Problems-with-opening-CHM-Help-files-from-Network-or-Internet"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2012/Jan/11/Problems-with-opening-CHM-Help-files-from-Network-or-Internet" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=O6-nQTjl1_0:671iS6sd4Rk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=O6-nQTjl1_0:671iS6sd4Rk:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=O6-nQTjl1_0:671iS6sd4Rk:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=O6-nQTjl1_0:671iS6sd4Rk:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=O6-nQTjl1_0:671iS6sd4Rk:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=O6-nQTjl1_0:671iS6sd4Rk:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=O6-nQTjl1_0:671iS6sd4Rk:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=O6-nQTjl1_0:671iS6sd4Rk:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=O6-nQTjl1_0:671iS6sd4Rk:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/O6-nQTjl1_0" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2012/Jan/11/Problems-with-opening-CHM-Help-files-from-Network-or-Internet</feedburner:origLink></item>
    <item>
      <title>IE9 not rendering box-shadow Elements inside of Table Cells</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/mThGauN22FY/IE9-not-rendering-boxshadow-Elements-inside-of-Table-Cells</link>
      <guid isPermaLink="false">1230297_201201040244</guid>
      <pubDate>Wed, 04 Jan 2012 02:44:15 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2012/Jan/03/IE9-not-rendering-boxshadow-Elements-inside-of-Table-Cells#Comments</comments>
      <slash:comments>6</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1230297</wfw:commentRss>
      <category>HTML</category>
      <category>Internet Explorer</category>
      <description>&lt;p&gt;Ran into an annoying problem today with IE 9. Slowly updating some older sites with CSS 3 tags and for the most part IE9 does a reasonably decent job of working with the new CSS 3 features. Not all by a long shot but at least some of the more useful ones &lt;a href="http://www.west-wind.com/weblog/posts/2011/Apr/11/Rounded-Corners-and-Shadows-Dialogs-with-CSS" target="_blank"&gt;like border-radius and box-shadow are supported&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Until today I was happy to see that IE supported box-shadow just fine, but I ran into a problem with some old markup that uses tables for its main layout sections. I found that inside of a table cell IE fails to render a box-shadow. &lt;/p&gt; &lt;p&gt;Below are images from Chrome (left) and IE 9 (right) of the same content:&lt;/p&gt; &lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/ffd604c03c04_D291/ChromeAndIe_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ChromeAndIe" border="0" alt="ChromeAndIe" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/ffd604c03c04_D291/ChromeAndIe_thumb.png" width="653" height="612"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;The download and purchase images are rendered with:&lt;/p&gt; &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;/div&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;="download.asp" &lt;/span&gt;&lt;span style="color: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;="&lt;/span&gt;&lt;span style="color: red"&gt;display&lt;/span&gt;&lt;span style="color: blue"&gt;:block;&lt;/span&gt;&lt;span style="color: red"&gt;margin&lt;/span&gt;&lt;span style="color: blue"&gt;: 10px;"&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;img &lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;="../images/download.gif" &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="boxshadow roundbox" /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;where the .boxshadow and .roundbox styles look like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: maroon"&gt;.boxshadow 
&lt;/span&gt;{
  &lt;span style="color: red"&gt;-moz-box-shadow&lt;/span&gt;: &lt;span style="color: blue"&gt;3px 3px 5px #535353&lt;/span&gt;;
  &lt;span style="color: red"&gt;-webkit-box-shadow&lt;/span&gt;: &lt;span style="color: blue"&gt;3px 3px 5px #535353&lt;/span&gt;;       
  &lt;span style="color: red"&gt;box-shadow&lt;/span&gt;: &lt;span style="color: blue"&gt;3px 3px 5px #535353&lt;/span&gt;;
}
&lt;span style="color: maroon"&gt;.roundbox
&lt;/span&gt;{  
  &lt;span style="color: red"&gt;-moz-border-radius&lt;/span&gt;: &lt;span style="color: blue"&gt;6px 6px 6px 6px&lt;/span&gt;;
  &lt;span style="color: red"&gt;-webkit-border-radius&lt;/span&gt;: &lt;span style="color: blue"&gt;6px&lt;/span&gt;;  
  &lt;span style="color: red"&gt;border-radius&lt;/span&gt;: &lt;span style="color: blue"&gt;6px 6px 6px 6px&lt;/span&gt;;
}&lt;/pre&gt;
&lt;h3&gt;And the Problem is… collapsed Table Borders&lt;/h3&gt;
&lt;p&gt;Now normally these two styles work just fine in IE 9 when applied to elements. But the box-shadow doesn't work inside of this markup - because the parent container is a table cell.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;td &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="sidebar" &lt;/span&gt;&lt;span style="color: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;="&lt;/span&gt;&lt;span style="color: red"&gt;&lt;strong&gt;border-collapse&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;strong&gt;: collapse&lt;/strong&gt;"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;   …&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;="download.asp" &lt;/span&gt;&lt;span style="color: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;="&lt;/span&gt;&lt;span style="color: red"&gt;display&lt;/span&gt;&lt;span style="color: blue"&gt;:block;&lt;/span&gt;&lt;span style="color: red"&gt;margin&lt;/span&gt;&lt;span style="color: blue"&gt;: 10px;"&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;img &lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;="../images/download.gif" &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="boxshadow roundbox" /&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;   …&lt;br&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;td&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;This HTML causes the image to not show a shadow. In actuality I'm not styling inline, but as part of my browser Reset I have the following in my master .css file:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: maroon"&gt;table &lt;/span&gt;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: maroon"&gt;&lt;/span&gt;{
    &lt;span style="color: red"&gt;border-collapse&lt;/span&gt;: &lt;span style="color: blue"&gt;collapse&lt;/span&gt;;
    &lt;span style="color: red"&gt;border-spacing&lt;/span&gt;: &lt;span style="color: blue"&gt;0&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;p&gt;which has the same effect as the inline style. border-collapse by default inherits from the parent and so the TD inherits from table and tr - so TD tags are effectively collapsed. &lt;/p&gt;
&lt;p&gt;You can check out a test document that demonstrates this behavior here in this &lt;a href="http://codepaste.net/bxsx2w" target="_blank"&gt;CodePaste.net snippet&lt;/a&gt; or &lt;a href="http://codepaste.net/showhtml/bxsx2w" target="_blank"&gt;run it here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;How to work around this Issue&lt;/h3&gt;
&lt;p&gt;To get IE9 to render the shadows inside of the TD tag correctly, I can just change the style explicitly NOT to use border-collapse:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;td &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="sidebar" &lt;/span&gt;&lt;span style="color: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;="&lt;/span&gt;&lt;span style="color: red"&gt;&lt;strong&gt;border-collapse&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;strong&gt;: separate;&lt;/strong&gt; border-width: 0;"&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Or better yet (thanks to &lt;a href="http://www.west-wind.com/weblog/posts/2012/Jan/03/IE9-not-rendering-boxshadow-Elements-inside-of-Table-Cells#1231143" target="_blank"&gt;David's comment below&lt;/a&gt;), you can add the border-collapse: separate to the .boxshadow style like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: maroon"&gt;.boxshadow 
&lt;/span&gt;{
  &lt;span style="color: red"&gt;-moz-box-shadow&lt;/span&gt;: &lt;span style="color: blue"&gt;3px 3px 5px #535353&lt;/span&gt;;
  &lt;span style="color: red"&gt;-webkit-box-shadow&lt;/span&gt;: &lt;span style="color: blue"&gt;3px 3px 5px #535353&lt;/span&gt;;       
  &lt;span style="color: red"&gt;box-shadow&lt;/span&gt;: &lt;span style="color: blue"&gt;3px 3px 5px #535353&lt;/span&gt;;
  &lt;strong&gt;&lt;span style="color: red"&gt;border-collapse&lt;/span&gt;: &lt;span style="color: blue"&gt;separate&lt;/span&gt;;&lt;/strong&gt;
}

&lt;/pre&gt;
&lt;p&gt;With either of these approaches IE renders the shadows correctly. &lt;/p&gt;
&lt;h3&gt;Do you really need border-collapse?&lt;/h3&gt;
&lt;p&gt;Should you bother with border-collapse? I think so! Collapsed borders render flat as a single fat line if a border-width and border-color are assigned, while separated borders render a thin line with a bunch of weird white space around it or worse render a old skool 3D raised border which is terribly ugly as well. So as a matter of course in any app my browser Reset includes the above code to make sure all tables with borders render the same flat borders.&lt;/p&gt;
&lt;p&gt;As you probably know, IE has all sorts of rendering issues in tables and on backgrounds (opacity backgrounds or image backgrounds) most of which is caused by the way that IE internally uses ActiveX filters to apply these effects. Apparently collapsed borders are yet one more item that causes problems with rendering.&lt;/p&gt;
&lt;p&gt;There you have it. Another crappy failure in IE we have to check for now, just one more reason to hate Internet Explorer. Luckily this one has a reasonably easy workaround. I hope this helps out somebody and saves them the hour I spent trying to figure out what caused this problem in the first place.&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://codepaste.net/bxsx2w" target="_blank"&gt;&lt;strong&gt;Sample HTML document that demonstrates the behavior&lt;/strong&gt;&lt;/a&gt; 
&lt;li&gt;&lt;strong&gt;&lt;a href="http://codepaste.net/showhtml/bxsx2w" target="_blank"&gt;Run the Sample&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category=HTML'&gt;HTML&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=Internet Explorer'&gt;Internet Explorer&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="IE9 not rendering box-shadow Elements inside of Table Cells" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2012/Jan/03/IE9-not-rendering-boxshadow-Elements-inside-of-Table-Cells"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2012/Jan/03/IE9-not-rendering-boxshadow-Elements-inside-of-Table-Cells" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=mThGauN22FY:4mC3zWL67UY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=mThGauN22FY:4mC3zWL67UY:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=mThGauN22FY:4mC3zWL67UY:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=mThGauN22FY:4mC3zWL67UY:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=mThGauN22FY:4mC3zWL67UY:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=mThGauN22FY:4mC3zWL67UY:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=mThGauN22FY:4mC3zWL67UY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=mThGauN22FY:4mC3zWL67UY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=mThGauN22FY:4mC3zWL67UY:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/mThGauN22FY" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2012/Jan/03/IE9-not-rendering-boxshadow-Elements-inside-of-Table-Cells</feedburner:origLink></item>
    <item>
      <title>XmlWriter and lower ASCII characters</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/gxkcshCeqJc/XmlWriter-and-lower-ASCII-characters</link>
      <guid isPermaLink="false">1228729_201201021036</guid>
      <pubDate>Mon, 02 Jan 2012 10:36:44 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2012/Jan/02/XmlWriter-and-lower-ASCII-characters#Comments</comments>
      <slash:comments>3</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1228729</wfw:commentRss>
      <category>.NET</category>
      <category>XML</category>
      <description>&lt;p&gt;Ran into an interesting problem today on my &lt;a href="http://codepaste.net" target="_blank"&gt;CodePaste.net site&lt;/a&gt;: The main RSS and ATOM feeds on the site were broken because one code snippet on the site contained a lower ASCII character (CHR(3)). I don't think this was done on purpose but it was enough to make the feeds fail. &lt;/p&gt; &lt;p&gt;After quite a bit of debugging and throwing in a custom error handler into my actual feed generation code that just spit out the raw error instead of running it through the ASP.NET MVC and my own error pipeline I found the actual error.&lt;/p&gt; &lt;p&gt;The lovely base exception and error trace I got looked like this:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Error: '', hexadecimal value 0x03, is an invalid character.&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br&gt;at System.Xml.XmlUtf8RawTextWriter.InvalidXmlChar(Int32 ch, Byte* pDst, Boolean entitize)&lt;br&gt;at System.Xml.XmlUtf8RawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)&lt;br&gt;at System.Xml.XmlUtf8RawTextWriter.WriteString(String text)&lt;br&gt;at System.Xml.XmlWellFormedWriter.WriteString(String text)&lt;br&gt;at System.Xml.XmlWriter.WriteElementString(String localName, String ns, String value)&lt;br&gt;at System.ServiceModel.Syndication.Rss20FeedFormatter.WriteItemContents(XmlWriter writer, SyndicationItem item, Uri feedBaseUri)&lt;br&gt;at System.ServiceModel.Syndication.Rss20FeedFormatter.WriteItem(XmlWriter writer, SyndicationItem item, Uri feedBaseUri)&lt;br&gt;at System.ServiceModel.Syndication.Rss20FeedFormatter.WriteItems(XmlWriter writer, IEnumerable`1 items, Uri feedBaseUri)&lt;br&gt;at System.ServiceModel.Syndication.Rss20FeedFormatter.WriteFeed(XmlWriter writer)&lt;br&gt;at System.ServiceModel.Syndication.Rss20FeedFormatter.WriteTo(XmlWriter writer)&lt;br&gt;at CodePasteMvc.Controllers.ApiControllerBase.GetFeed(Object instance) in C:\Projects2010\CodePaste\CodePasteMvc\Controllers\ApiControllerBase.cs:line 131&lt;/p&gt; &lt;h3&gt;XML doesn't like extended ASCII Characters&lt;/h3&gt; &lt;p&gt;It turns out the issue is that XML in general does not deal well with lower ASCII characters. According to the &lt;a href="http://www.w3.org/TR/xml/#charsets" target="_blank"&gt;XML spec&lt;/a&gt; it looks like any characters below 0x09 are invalid. If you generate an XML document in .NET with an embedded &amp;amp;#x3; entity (as mine did to create the error above), you tend to get an XML document error when displaying it in a viewer. For example, here's what the result of my&amp;nbsp; feed output looks like with the invalid character embedded inside of Chrome which displays RSS feeds as raw XML by default:&lt;/p&gt; &lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/XmlWriter-and-lower-ASCII-character_14B05/ChromeError_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ChromeError" border="0" alt="ChromeError" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/XmlWriter-and-lower-ASCII-character_14B05/ChromeError_thumb.png" width="669" height="419"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Other browsers show similar error messages. The nice thing about Chrome is that you can actually view source and jump down to see the line that causes the error which allowed me to track down the actual message that failed.&lt;/p&gt; &lt;p&gt;If you create an XML document that contains a 0x03 character the XML writer fails outright with the error:&lt;/p&gt; &lt;p&gt;&lt;strong&gt;'', hexadecimal value 0x03, is an invalid character.&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The good news is that this behavior is overridable so XML output can at least be created by using the XmlSettings object when configuring the XmlWriter instance. In my RSS configuration code this looks something like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;MemoryStream &lt;/span&gt;ms = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;();
&lt;strong&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;settings = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;XmlWriterSettings&lt;/span&gt;()
{
    CheckCharacters = &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;false
&lt;/span&gt;};&lt;/strong&gt;
&lt;span style="color: #2b91af"&gt;XmlWriter &lt;/span&gt;writer = &lt;span style="color: #2b91af"&gt;XmlWriter&lt;/span&gt;.Create(ms,&lt;strong&gt;settings&lt;/strong&gt;);
&lt;/pre&gt;
&lt;p&gt;and voila the feed now generates.&lt;/p&gt;
&lt;p&gt;Now generally this is probably NOT a good idea, because as mentioned above these characters are illegal and if you view a raw XML document you'll get validation errors. Luckily though most RSS feed readers however don't care and happily accept and display the feed correctly, which is good because it got me over an embarrassing hump until I figured out a better solution.&lt;/p&gt;
&lt;h3&gt;How to handle extended Characters?&lt;/h3&gt;
&lt;p&gt;I was glad to get the feed fixed for the time being, but now I was still stuck with an interesting dilemma. CodePaste.net accepts user input for code snippets and those code snippets can contain just about anything. This means that ASP.NET's standard request filtering cannot be applied to this content. The code content displayed is encoded before display so for the HTML end the CHR(3) input is not really an issue.&lt;/p&gt;
&lt;p&gt;While invisible characters are hardly useful in user input it's not uncommon that odd characters show up in code snippets. You know the old fat fingering that happens when you're in the middle of a coding session and those invisible characters do end up sometimes in code editors and then end up pasted into the HTML textbox for pasting as a Codepaste.net snippet.&lt;/p&gt;
&lt;p&gt;The question is how to filter this text? Looking back at the &lt;a href="http://www.w3.org/TR/xml/#charsets" target="_blank"&gt;XML Charset Spec&lt;/a&gt; it looks like all characters below 0x20 (space) except for 0x09 (tab), 0x0A (LF), 0x0D (CR) are illegal. So applying the following filter with a RegEx should work to remove invalid characters:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;code = &lt;span style="color: #2b91af"&gt;Regex&lt;/span&gt;.Replace(item.Code, &lt;span style="color: #a31515"&gt;@"[\u0000-\u0008,\u000B,\u000C,\u000E-\u001F]"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;""&lt;/span&gt;);
&lt;/pre&gt;
&lt;p&gt;Applying this RegEx to the code snippet (and title) eliminates the problems and the feed renders cleanly.&lt;/p&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category=.NET'&gt;.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=XML'&gt;XML&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="XmlWriter and lower ASCII characters" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2012/Jan/02/XmlWriter-and-lower-ASCII-characters"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2012/Jan/02/XmlWriter-and-lower-ASCII-characters" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gxkcshCeqJc:GlTXVZdRnt0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gxkcshCeqJc:GlTXVZdRnt0:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gxkcshCeqJc:GlTXVZdRnt0:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=gxkcshCeqJc:GlTXVZdRnt0:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gxkcshCeqJc:GlTXVZdRnt0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=gxkcshCeqJc:GlTXVZdRnt0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gxkcshCeqJc:GlTXVZdRnt0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=gxkcshCeqJc:GlTXVZdRnt0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gxkcshCeqJc:GlTXVZdRnt0:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/gxkcshCeqJc" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2012/Jan/02/XmlWriter-and-lower-ASCII-characters</feedburner:origLink></item>
    <item>
      <title>Changing the default HTML Templates to HTML5 in Visual Studio</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/ikySZ8t3VG4/Changing-the-default-HTML-Templates-to-HTML5-in-Visual-Studio</link>
      <guid isPermaLink="false">1219669_201112231219</guid>
      <pubDate>Fri, 23 Dec 2011 12:19:34 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2011/Dec/23/Changing-the-default-HTML-Templates-to-HTML5-in-Visual-Studio#Comments</comments>
      <slash:comments>0</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1219669</wfw:commentRss>
      <category> ASP.NET</category>
      <category>.NET</category>
      <category>HTML5</category>
      <description>&lt;p&gt;If you're using Visual Studio 2010 to create Web applications, you probably have found out that the default Web templates for ASP.NET Web Forms and Master pages and plain HTML pages all create HTML 4 XHTML headers like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: maroon"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;="C#" &lt;/span&gt;&lt;span style="color: red"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color: blue"&gt;="true" &lt;/span&gt;&lt;span style="color: red"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue"&gt;="$fileinputname$.aspx.cs" &lt;/span&gt;&lt;span style="color: red"&gt;Inherits&lt;/span&gt;&lt;span style="color: blue"&gt;="$rootnamespace$.$classname$" &lt;/span&gt;&lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: maroon"&gt;DOCTYPE &lt;/span&gt;&lt;span style="color: red"&gt;html PUBLIC &lt;/span&gt;&lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;html &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: blue"&gt;&lt;strong&gt;="http://www.w3.org/1999/xhtml"&amp;gt;&lt;/strong&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now I don't know about you, but I'm happy to use HTML5's simple DOCTYPE definition. The first thing I tend to do is manually change my document header so that it looks like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: maroon"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;="C#" &lt;/span&gt;&lt;span style="color: red"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color: blue"&gt;="true" &lt;/span&gt;&lt;span style="color: red"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue"&gt;="$fileinputname$.aspx.cs" &lt;/span&gt;&lt;span style="color: red"&gt;Inherits&lt;/span&gt;&lt;span style="color: blue"&gt;="$rootnamespace$.$classname$" &lt;/span&gt;&lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: maroon"&gt;DOCTYPE &lt;/span&gt;&lt;span style="color: red"&gt;html&lt;/span&gt;&lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;html&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: blue"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Wouldn't it be nice if this was the default?&lt;/p&gt;
&lt;p&gt;If you have a few minutes it's easy to change the stock templates in Visual Studio, or if you prefer you can create your own custom templates to exactly suit your needs.&lt;/p&gt;
&lt;h3&gt;&lt;/h3&gt;
&lt;h3&gt;Stock templates in Visual Studio 2010&lt;/h3&gt;
&lt;p&gt;All the default document types in Visual Studio are based on templates which are stored conveniently in .zip files. The folder where Visual Studio stores its HTML templates for Web Application Projects lives in this location:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;C:\Program Files (x86)\Visual Studio 2010\Common7\IDE\ItemTemplates\CSharp\Web\1033&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you're using Web Site project the location is:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;C:\Program Files (x86)\Visual Studio 2010\Common7\IDE\ItemTemplates\Web\CSharp\1033&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;There are a ton of templates in both folders. For WebForms the one we want is - not surprisingly - WebForms.zip. If you open up WebForm.zip with your favorite Zip tool (or Explorer) you'll see something like this for Web Application Projects:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/WebForm_Template%5B6%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="WebForm_Template[6]" border="0" alt="WebForm_Template[6]" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/WebForm_Template%5B6%5D_thumb.png" width="667" height="432"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;You can see the three templates - the ASPX, .cs and designer files in the zip file. You can edit all of these, but for our purpose here I'll just change the HTML header.&lt;/p&gt;
&lt;p&gt;If I open the default.aspx page you see the following default markup:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: maroon"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;="C#" &lt;/span&gt;&lt;span style="color: red"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color: blue"&gt;="true" &lt;/span&gt;&lt;span style="color: red"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue"&gt;="$fileinputname$.aspx.cs" &lt;/span&gt;&lt;span style="color: red"&gt;Inherits&lt;/span&gt;&lt;span style="color: blue"&gt;="$rootnamespace$.$classname$" &lt;/span&gt;&lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: maroon"&gt;DOCTYPE &lt;/span&gt;&lt;span style="color: red"&gt;html PUBLIC &lt;/span&gt;&lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;html &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: blue"&gt;&lt;strong&gt;="http://www.w3.org/1999/xhtml"&amp;gt;&lt;/strong&gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;head &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;form &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="form1" &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;form&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It's a template so you see a few template expressions like $fileinputname$ and $rootnamespace$ in the document. Visual Studio fills those values in when the template is loaded and a new item added to a project. However, the rest of the document can be changed to your heart's delight. For the basic WebForms template I simply added the HTML 5 doctype header like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: maroon"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;="C#" &lt;/span&gt;&lt;span style="color: red"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color: blue"&gt;="true" &lt;/span&gt;&lt;span style="color: red"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue"&gt;="$fileinputname$.aspx.cs" &lt;/span&gt;&lt;span style="color: red"&gt;Inherits&lt;/span&gt;&lt;span style="color: blue"&gt;="$rootnamespace$.$classname$" &lt;/span&gt;&lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: maroon"&gt;DOCTYPE &lt;/span&gt;&lt;span style="color: red"&gt;html&lt;/span&gt;&lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;html&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: blue"&gt;&lt;strong&gt;&amp;gt;
&lt;/strong&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;head &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;form &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="form1" &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;form&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Save the file and make sure that the zip file gets updated with the saved data (check: Open the Zip file again and edit the file again to make sure). If you use any recent zipping tool (or even Explorer) you will be able to simply edit the file and save it to write the changes to the Zip file.&lt;/p&gt;
&lt;h3&gt;Rebuild the TemplateCache&lt;/h3&gt;
&lt;p&gt;You're not quite done yet, unfortunately. Once you've updated your zip template you need to override the Cache that Visual Studio creates from templates. Visual Studio internally unzips these template zip files and stores them in a TemplateCache folder which lives in:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;C:\Program Files (x86)\Visual Studio 2010\Common7\IDE\ItemTemplatesCache\CSharp\Web\1033\WebForms.zip&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;C:\Program Files (x86)\Visual Studio 2010\Common7\IDE\ItemTemplatesCache\Web\CSharp\1033\WebForms.zip&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Basically there are folders that mimic the .zip files and hold the unzipped content on disk. Updating the Zip file in the ItemTemplates folder on its own will not yet give you the new template until the TemplateCache has been updated.&lt;/p&gt;
&lt;p&gt;To do this you need to run:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;DevEnv /InstallVsTemplates&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;from the Visual Studio Command Prompt. This recreates the template cache with the updated templates you modified.&lt;/p&gt;
&lt;p&gt;Alternately I also found that you can delete all the folders in the ItemTempaltesCache folder which effectively clears the cache. Visual Studio will then use the templates directly from the zip file. This can be useful if you're mucking around with the templates a bit or you're trying out multiple templates all at once as it bypasses the Template registration steps. When you're all done though it's a good idea to /InstallVsTemplates just to be 'correct' about it.&lt;/p&gt;
&lt;p&gt;While you're at it you probably also want to change:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;HtmlPage.zip 
&lt;li&gt;MasterPage.zip&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;along the same lines. Note that the various Razor templates already use HTML5 doc headers, so no need to update them for HTML 5.&lt;/p&gt;
&lt;h3&gt;Creating new Templates&lt;/h3&gt;
&lt;p&gt;Changing the stock templates is useful because you probably use them all the time every day. But if you want to make sweeping changes to templates, or you want to have multiple templates that do various specific tasks, it's probably better to create brand spanking new templates instead. It's also very easy to create brand new templates. One of the easiest ways to do that is actually built right into Visual Studio via the Template Export mechanism. &lt;/p&gt;
&lt;p&gt;Let's look at an example. I'll use a Web Application project here, but the same works for any kind of project: Web Site, MVC w/ Razor, Web Pages.&lt;/p&gt;
&lt;p&gt;Let's start with a page that acts as my default template I'm going to create. This template includes a few basic setups for some base layout that is common on sample pages I create:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: maroon"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;="C#" &lt;/span&gt;&lt;span style="color: red"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color: blue"&gt;="true" &lt;/span&gt;&lt;span style="color: red"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue"&gt;="WebForm1.aspx.cs" &lt;/span&gt;&lt;span style="color: red"&gt;Inherits&lt;/span&gt;&lt;span style="color: blue"&gt;="WebApplication10.WebForm1" &lt;/span&gt;&lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: maroon"&gt;DOCTYPE &lt;/span&gt;&lt;span style="color: red"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;head &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="Head" &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;="server"&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;link &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;="css/westwind.css" &lt;/span&gt;&lt;span style="color: red"&gt;rel&lt;/span&gt;&lt;span style="color: blue"&gt;="stylesheet" &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;="text/css" /&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;="text/javascript"&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;="text/javascript"&amp;gt;
        if &lt;/span&gt;(&lt;span style="color: blue"&gt;typeof &lt;/span&gt;(jQuery) == &lt;span style="color: maroon"&gt;'undefined'&lt;/span&gt;)
            document.write(unescape(&lt;span style="color: maroon"&gt;"%3Cscript src='scripts/jquery.min.js' type='text/javascript'%3E%3C/script%3E"&lt;/span&gt;));
    &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;form &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="form1" &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;="server"&amp;gt;
    
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;h1&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;h1&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;

    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="toolbarcontainer"&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;="" &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="hoverpanel"&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;img &lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;="css/images/home.gif" /&amp;gt; &lt;/span&gt;Home&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;="" &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="hoverpanel"&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;img &lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;="css/images/refresh.gif" /&amp;gt; &lt;/span&gt;Refresh&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="containercontent"&amp;gt;
    
    
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;form&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now to create a template from this page:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Select the Web project or any file within it
&lt;li&gt;Click on File | Export Template and select Item Template&lt;br&gt;&lt;br&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/WizardStep1_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="WizardStep1" border="0" alt="WizardStep1" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/WizardStep1_thumb.png" width="619" height="540"&gt;&lt;/a&gt; &lt;/li&gt;&lt;br&gt;
&lt;li&gt;Click Next and select the file or files in the project to export&lt;br&gt;&lt;br&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/WizardStep2_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="WizardStep2" border="0" alt="WizardStep2" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/WizardStep2_thumb.png" width="619" height="540"&gt;&lt;/a&gt; &lt;br&gt;
&lt;li&gt;Fill in the info for the template:&lt;br&gt;&lt;br&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/WizardStep3_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="WizardStep3" border="0" alt="WizardStep3" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/WizardStep3_thumb.png" width="619" height="540"&gt;&lt;/a&gt; &lt;br&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;This ends up creating a new template in your My Documents folder:&lt;br&gt;&lt;strong&gt;&amp;lt;MyDocuments&amp;gt;\Visual Studio 2010\My Exported Templates &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&amp;lt;MyDocuments&amp;gt;\Visual Studio 2010\Templates\ItemTemplates\Visual C#&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You might want to move the latter file into the &lt;br&gt;&lt;/p&gt;&lt;span style="color: blue"&gt;&lt;/span&gt;
&lt;p&gt;&lt;strong&gt;C:\Users\rstrahl\Documents\Visual Studio 2010\Templates\ItemTemplates\Visual C#\Web&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;folder, so the template properly shows up in the Web folder which then looks like this in the Add New Item dialog:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/TemplateInVs_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="TemplateInVs" border="0" alt="TemplateInVs" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Changing-your-default-HTML-Templates-to-_15112/TemplateInVs_thumb.png" width="962" height="369"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;When you select the template it now produces your custom HTML for the template you created.&lt;/p&gt;
&lt;h3&gt;Templates A-GoGo&lt;/h3&gt;
&lt;p&gt;Templates are a nice way to create pre-fab content. I've found it useful for certain kinds of projects to create project specific templates just so some common content can be loaded into the page. While WebForm Master and Razor Content Pages remove some of the need to build large custom headers, for some situations having custom content pumped directly into pages is still useful. Templates make this task easy and save you from repetitive typing. It's worth the effort to spent a little time to customize those templates you use daily to fit your needs. Whether it's changing the existing templates or create brand new ones, you now have the tools to customize to your hearts' content. Go for it!&lt;/p&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category= ASP.NET'&gt; ASP.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=.NET'&gt;.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=HTML5'&gt;HTML5&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="Changing the default HTML Templates to HTML5 in Visual Studio" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2011/Dec/23/Changing-the-default-HTML-Templates-to-HTML5-in-Visual-Studio"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2011/Dec/23/Changing-the-default-HTML-Templates-to-HTML5-in-Visual-Studio" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ikySZ8t3VG4:Bg-FpkXY5F8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ikySZ8t3VG4:Bg-FpkXY5F8:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ikySZ8t3VG4:Bg-FpkXY5F8:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=ikySZ8t3VG4:Bg-FpkXY5F8:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ikySZ8t3VG4:Bg-FpkXY5F8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=ikySZ8t3VG4:Bg-FpkXY5F8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ikySZ8t3VG4:Bg-FpkXY5F8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=ikySZ8t3VG4:Bg-FpkXY5F8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ikySZ8t3VG4:Bg-FpkXY5F8:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/ikySZ8t3VG4" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2011/Dec/23/Changing-the-default-HTML-Templates-to-HTML5-in-Visual-Studio</feedburner:origLink></item>
    <item>
      <title>Debugging Application_Start and Module Initialization with IIS and Visual Studio</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/gfjlc3ECc2s/Debugging-ApplicationStart-and-Module-Initialization-with-IIS-and-Visual-Studio</link>
      <guid isPermaLink="false">1212032_201112151019</guid>
      <pubDate>Thu, 15 Dec 2011 10:19:04 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2011/Dec/15/Debugging-ApplicationStart-and-Module-Initialization-with-IIS-and-Visual-Studio#Comments</comments>
      <slash:comments>4</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1212032</wfw:commentRss>
      <category> ASP.NET</category>
      <category>IIS7</category>
      <category>IIS</category>
      <description>&lt;p&gt;Recently I've seen quite a few questions pop up in regards to debugging ASP.NET application initialization. Most commonly I see something along these lines:&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;I'm trying to debug my ASP.NET application and am starting the Debugger, but it won't stop on code in my Application_Start event &lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Yup, been there done that. &lt;/p&gt; &lt;h3&gt;What does Application_Start do?&lt;/h3&gt; &lt;p&gt;Application_Start is pseudo handler that ASP.NET checks for during initialization of an ASP.NET application - if one exists it's fired during the application's initialization process which occurs specifically when the HttpRuntime instance is started. Effectively Application_Start is fired &lt;em&gt;exactly once&lt;/em&gt; when the Web application's AppDomain starts. Application_Start is simply a short cut that makes it consistent with the other Application_ events that are available in global.asax.cs even though it is not actually an HttpApplication event like most other Application_ handlers. Remember that Global.asax is your application's subclass of the HttpApplication object:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Global &lt;/span&gt;: System.Web.HttpApplication
&lt;/pre&gt;
&lt;p&gt;so Global.asax.cs inherits the HttpApplication and this instance is what ASP.NET uses to run your application. You can always access this instance with HttpContext.Current.HttpApplicationInstance. ASP.NET creates a pool of multiple HttpApplication instances that are assigned to each request being processed, but Application_Start fires only exactly once during the Web application's lifetime not for each instance of HttpApplication started.&lt;/p&gt;
&lt;p&gt;Because Application_Start fires only when the HttpRuntime is initialized, it's a common hook point for application configuration tasks that need to happen only once during the lifetime of the application. It's perfect for setting static values that affect the entire running application or set up global configuration settings that get reused throughout the application. Application_Start can also be used to dynamically add new modules to the processing chain at runtime.&lt;/p&gt;
&lt;p&gt;Application initialization extends beyond Application_Start. There's other code that fires during the one time initialization as well. HttpModules and their Init() handlers that fire to hook up the actual module events, also fire during this initialization stage. Likewise you can't debug these Init() handlers when debugging your code in the full version of IIS. I'm sure there's other code in ASP.NET that fires during application initialization but these two are the most common scenarios you might run into with user code.&lt;/p&gt;
&lt;h3&gt;IIS and Application_Start Debugging from Visual Studio&lt;/h3&gt;
&lt;p&gt;Problems with debugging Application_Start come up when you try to debug this initialization code when running the full version of IIS. It doesn't happen when you run the Visual Studio Web Server or IIS Express - it only occurs when you run the full version of IIS.&lt;/p&gt;
&lt;p&gt;The problem is caused by the way that Visual Studio attaches the debugger to IIS when running ASP.NET applications, which occurs AFTER the application initialization phase and so is too late to actually stop on a breakpoint in Application_Start.&lt;/p&gt;
&lt;p&gt;The code of course still executes, but the debugger doesn't trigger any breakpoints if you simply press F5 to debug in Visual Studio. If you set a break point like this (when running in full IIS):&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/Application_StartDebugger_4.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Application_StartDebugger" border="0" alt="Application_StartDebugger" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/Application_StartDebugger_thumb_1.png" width="805" height="322"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;the breakpoint won't trigger.&lt;/p&gt;
&lt;p&gt;Likewise if you have code in an HttpModule's Init() handler which also fires as part of the runtime initialization code, the debugger won't trigger there either:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/HttpModule_init_4.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="HttpModule_init" border="0" alt="HttpModule_init" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/HttpModule_init_thumb_1.png" width="788" height="120"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;If you don't know what's happening it's easy to assume that this code is never firing, but that's not the case. The code fires, only the debugger is not triggered when the code is executed by IIS.&lt;/p&gt;
&lt;h3&gt;Easiest Solution: Use the Visual Studio Web Server or IIS Express&lt;/h3&gt;
&lt;p&gt;Debugging of Application_Start - and also HttpModule.Init() handlers - occurs only when running the full version of IIS. When running the Visual Studio built-in Web Server or &lt;a href="http://http://learn.iis.net/page.aspx/868/iis-express-overview/" target="_blank"&gt;IIS Express&lt;/a&gt; there's no problem debugging application initialization. So one easy solution to debugging Application_Init and Module Initialization code is to simply switch to using the built in Visual Studio Web Server (or IIS Express) as opposed to the full version of IIS:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/DebuggingInVSServer_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="DebuggingInVSServer" border="0" alt="DebuggingInVSServer" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/DebuggingInVSServer_thumb.png" width="839" height="450"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Now if you press F5 to debug from within Visual Studio the debugger happily triggers on your breakpoint. This works because Visual Studio actually launches the local server EXE manually and so can easily attach the debugger on startup. Inside of IIS the application pool is actually loaded from the IIS Admin service and Visual Studio has to attach to the Application Pool Exe after it's been started.&lt;/p&gt;
&lt;p&gt;If you're already running one of these local servers for all your debugging then you shouldn't really have run into this issue in the first place and this is certainly a good way to go. In many environments it makes perfect sense to run the local servers.&lt;/p&gt;
&lt;p&gt;Even if you're like me and you prefer to run the full version of IIS, and you happen to need to debug your Application_Start or other initialization code it might be worthwhile to switch temporarily to one of the local servers to debug the issue. It's the easiest way to get past any issues you might have during application initialization.&lt;/p&gt;
&lt;h3&gt;Debugging IIS Initialization in Visual Studio&lt;/h3&gt;
&lt;p&gt;If you definitely need to debug your startup code in the full version of IIS there are other ways to get the debugger attached as well. It won't work with the F5 run menu and it requires a little more work.&lt;/p&gt;
&lt;p&gt;You can use the System.Diagnostics.Debugger.Break() method to force the Web application to break into the debugger:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected void &lt;/span&gt;Application_Start(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
{
    &lt;span style="color: #2b91af"&gt;Debugger&lt;/span&gt;.Break();&lt;br&gt; &lt;/pre&gt;&lt;pre class="code"&gt;    …&lt;br&gt;}
&lt;/pre&gt;
&lt;p&gt;&lt;font color="#000000" face="Verdana"&gt;Add this code to your Application_Start or other initialization code like Module.Init() code where you want to break. &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Before you do anything else do:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;IISReset &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;from the Windows RUN box to ensure that IIS has been shut down and your Application Pool is in fact going to start up fresh.&lt;/p&gt;
&lt;p&gt;Then open a page on your Web site.&lt;/p&gt;
&lt;p&gt;When you hit this page, Visual Studio will pop up a debugger dialog like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/DebuggerDialog_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="DebuggerDialog" border="0" alt="DebuggerDialog" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/DebuggerDialog_thumb.png" width="408" height="438"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Click yes, and Visual Studio will then start up a new instance and display your code. If your code is set for debugging (ie. the PDB file exists) you'll be able to step through your code and see watch and locals information.&lt;/p&gt;
&lt;p&gt;Here's what the debugger session looks like:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/VSw3wpDebug_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="VSw3wpDebug" border="0" alt="VSw3wpDebug" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/Debugging-Application_Start_10299/VSw3wpDebug_thumb.png" width="891" height="546"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;You can see that now we're debugging the w3wp process directly. The debugger has attached using a different route bypassing the Visual Studio F5 startup and instead directly attaching the debugger to the process.&lt;/p&gt;
&lt;p&gt;One downside of this setup is that you can't see your entire project while debugging this way, but you can step into any code that symbols (PDB files) are available for. So if you needed to step into a module that resides in a separate assembly for example you can do that as long as that assembly and the .pdb file is available.&lt;/p&gt;
&lt;h3&gt;Summary&lt;/h3&gt;
&lt;p&gt;Hopefully you don't have a lot of code in your Web application's initialization code and hopefully you never actually need to debug it :-) Even if you do have to debug your startup code it's not likely to be a frequent task.&lt;/p&gt;
&lt;p&gt;But it does come in handy at times to be able to do so directly in IIS so you can see the entire IIS environment including parent hierarchy and modules that are loading up in your application. In the past I've had some nasty module configuration conflicts between my virtual and the parent directories and being able to step into the live environment directly in IIS was a lot easier than trying to write debug log information out to a log file.&lt;/p&gt;
&lt;p&gt;It's one of those edge case issues that comes up very rarely, but when you need it, you'll be glad you know how to debug your code in the live environment…&lt;/p&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category= ASP.NET'&gt; ASP.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=IIS7'&gt;IIS7&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=IIS'&gt;IIS&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="Debugging Application_Start and Module Initialization with IIS and Visual Studio" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2011/Dec/15/Debugging-ApplicationStart-and-Module-Initialization-with-IIS-and-Visual-Studio"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2011/Dec/15/Debugging-ApplicationStart-and-Module-Initialization-with-IIS-and-Visual-Studio" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gfjlc3ECc2s:XVyyf5ZW67k:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gfjlc3ECc2s:XVyyf5ZW67k:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gfjlc3ECc2s:XVyyf5ZW67k:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=gfjlc3ECc2s:XVyyf5ZW67k:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gfjlc3ECc2s:XVyyf5ZW67k:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=gfjlc3ECc2s:XVyyf5ZW67k:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gfjlc3ECc2s:XVyyf5ZW67k:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=gfjlc3ECc2s:XVyyf5ZW67k:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=gfjlc3ECc2s:XVyyf5ZW67k:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/gfjlc3ECc2s" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2011/Dec/15/Debugging-ApplicationStart-and-Module-Initialization-with-IIS-and-Visual-Studio</feedburner:origLink></item>
    <item>
      <title>HTML 5 Input Types - How useful is this really going to be?</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/fmykZafdcqM/HTML-5-Input-Types-How-useful-is-this-really-going-to-be</link>
      <guid isPermaLink="false">1207977_201112110513</guid>
      <pubDate>Sun, 11 Dec 2011 05:13:01 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2011/Dec/10/HTML-5-Input-Types-How-useful-is-this-really-going-to-be#Comments</comments>
      <slash:comments>16</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1207977</wfw:commentRss>
      <category>HTML</category>
      <category> ASP.NET</category>
      <category>HTML5</category>
      <description>&lt;p&gt;HTML5 introduces a number of new input types that are meant to provide a richer user input experience. It seems like this should be a good thing, given that we've basically been stuck with a very small and limited set of stock input controls in HTML.&lt;/p&gt; &lt;p&gt;HTML5 aims to change that with input type extensions that allow you to edit and validate special types of text input. The new input types that are available in HTML5 are the following:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;email  &lt;li&gt;url  &lt;li&gt;number  &lt;li&gt;range  &lt;li&gt;Date pickers (date, month, week, time, datetime, datetime-local)  &lt;li&gt;search  &lt;li&gt;color &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Some of these act as validators (email, url, number) while others are meant as helpers to provide richer functionality like DataPicker, Range, Color and Search options.&lt;/p&gt; &lt;h3&gt;What do you get?&lt;/h3&gt; &lt;p&gt;A number of newer browsers now support some of the new HTML 5 input types and provide limited user hints and validation on the client. For example in FireFox 8 if I type in an invalid email address into my login dialog and tab off I get this display:&lt;/p&gt; &lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/FireFoxEmail_thumb_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="FireFoxEmail_thumb" border="0" alt="FireFoxEmail_thumb" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/FireFoxEmail_thumb_thumb.png" width="413" height="218"&gt;&lt;/a&gt; &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;="email" &lt;/span&gt;&lt;span style="color: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;="&lt;/span&gt;&lt;span style="color: red"&gt;width&lt;/span&gt;&lt;span style="color: blue"&gt;:250px;" &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="Username" &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;="Username"&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and when I submit the form I get:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/ffErrorOnSubmit_thumb_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ffErrorOnSubmit_thumb" border="0" alt="ffErrorOnSubmit_thumb" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/ffErrorOnSubmit_thumb_thumb.png" width="408" height="215"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Chrome does something similar although frankly it looks a lot nicer than the klunky FireFox message.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/ChromeUnSubmitEmail_thumb_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="ChromeUnSubmitEmail_thumb" border="0" alt="ChromeUnSubmitEmail_thumb" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/ChromeUnSubmitEmail_thumb_thumb.png" width="408" height="215"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Note that in both cases the form won't submit until a valid email address has been entered.&lt;/p&gt;
&lt;h3&gt;Rich Interfaces? Maybe not&lt;/h3&gt;
&lt;p&gt;In theory browsers are supposed to implement rich interfaces to handle some of these interface types. For example, dates should be shown as a date picker. Current implementations in this department however are really sad at best. Check out the date field displays here in Chrome:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/DateInput_thumb_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="DateInput_thumb" border="0" alt="DateInput_thumb" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/DateInput_thumb_thumb.png" width="221" height="189"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The 'user interface' consists of little more than an up-down button which increases decreases the date by a day. Additionally the date is displayed in universal format rather than locale adjusted format for the current browser or language. AFAIK there's no real clean way to style any of this either.&lt;/p&gt;
&lt;p&gt;Opera on the other hand provides a number of editors, but boy are they ugly:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/OperaInput_thumb_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="OperaInput_thumb" border="0" alt="OperaInput_thumb" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/OperaInput_thumb_thumb.png" width="337" height="280"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The date picker that actually pops up also isn't any better looking. Even here the date and time are displayed in universal format. Again there appears to be no way to style this stuff so it's bound to clash hard with any UI layout you have.&lt;/p&gt;
&lt;p&gt;You can play around with some of these at:&lt;/p&gt;
&lt;p&gt;&lt;a title="http://www.west-wind.com/WestwindWebToolkit/samples/Ajax/html5andCss3/Html5InputControls.aspx" href="http://www.west-wind.com/WestwindWebToolkit/samples/Ajax/html5andCss3/Html5InputControls.aspx"&gt;http://www.west-wind.com/WestwindWebToolkit/samples/Ajax/html5andCss3/Html5InputControls.aspx&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Styling&lt;/h3&gt;
&lt;p&gt;You also need to remember that when you start styling the new HTML5 input controls that you need to include each one of them individually for each style:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: maroon"&gt;.containercontent input[type=text]&lt;/span&gt;,
&lt;span style="color: maroon"&gt;.containercontent input[type=email]&lt;/span&gt;,
&lt;span style="color: maroon"&gt;.containercontent input[type=search]&lt;/span&gt;,
&lt;span style="color: maroon"&gt;.containercontent input[type=url]
&lt;/span&gt;{
    &lt;span style="color: red"&gt;width&lt;/span&gt;: &lt;span style="color: blue"&gt;400px&lt;/span&gt;;
}
&lt;/pre&gt;
&lt;p&gt;This is both advantageous and tedious. Advantageous because it provides for semantic styling as you can reference exactly what each input element should look like. But it's also more tedious in that you now have to specify each of 15 or so input types that apply to what used to be text input. It was a whole lot easier to style just input[type=text]. This is not a big deal but just something to think of that will potentially require some extra work for the design layout process.&lt;/p&gt;
&lt;h3&gt;Good News Bad News&lt;/h3&gt;
&lt;p&gt;This existing browser behavior of HTML 5 input tags raises all sorts of questions. I already mentioned the styling is a big issue - how do you get the UI to match your HTML's existing layout. But more importantly it seems that the new HTML input element behaviors are going to conflict with existing validation and custom input control types. If you're already using a different date picker like say jQuery UI date picker using code like this:&lt;/p&gt;&lt;pre class="code"&gt;$().ready(&lt;span style="color: blue"&gt;function &lt;/span&gt;() {
    $(&lt;span style="color: maroon"&gt;"input[type=date]"&lt;/span&gt;)
            .datepicker({ showOn: &lt;span style="color: maroon"&gt;'button'&lt;/span&gt;,
                buttonImageOnly: &lt;span style="color: blue"&gt;true&lt;/span&gt;,
                buttonImage: &lt;span style="color: maroon"&gt;'../../images/calendar.png'&lt;/span&gt;,
                buttonText: &lt;span style="color: maroon"&gt;'Select date'&lt;/span&gt;,
                showButtonPanel: &lt;span style="color: blue"&gt;true                        
            &lt;/span&gt;});
})
&lt;/pre&gt;
&lt;p&gt;You can end up with lovely stuff like this:&lt;/p&gt;
&lt;h3&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/DateError_thumb_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="DateError_thumb" border="0" alt="DateError_thumb" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/HTML-5-Input-Types---How-useful-is-this-_10337/DateError_thumb_thumb.png" width="231" height="137"&gt;&lt;/a&gt; &lt;/h3&gt;
&lt;p&gt;Ouch! The date box doesn't support my local American date and I can't even submit the form. Major FAIL there.&lt;/p&gt;
&lt;p&gt;There are other similar scenarios - the url type attribute in Firefox will complain about an protocol-less url, while in Chrome it'll automatically append the http:// in front of the URL. Either of these behaviors can be problematic depending on what you're trying to achieve in your form.&lt;/p&gt;
&lt;p&gt;To be fair you can turn off the form validation on the form submission level:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;="submit" &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="btnSubmit" &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;="btnSubmit" &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;="Save" &lt;br&gt;       &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="submitbutton" &lt;/span&gt;&lt;span style="color: red"&gt;formnovalidate&lt;/span&gt;&lt;span style="color: blue"&gt;="formnovalidate" /&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;to avoid frivolous validation. But then you might as well use a input type=text control.&lt;/p&gt;
&lt;p&gt;So in the end use of the new HTML5 input elements may not be as useful as I had hoped. Lack of control over the input becomes a big problem and lack of styling makes these controls look out of place. Worst of all though the validation can seriously get in the way unless you disable the validation diminishing the value significantly. It certainly isn't going to replace existing client side validation schemes - since you most likely need more sophisticated validation than just these general types. And if you leave it on for the base validations and use custom validations for your custom logic, the two are likely to look very different for a very disjointed feature.&lt;/p&gt;
&lt;h3&gt;To HTML5 or not to HTML5&lt;/h3&gt;
&lt;p&gt;There's a big momentum behind HTML5 these days, but every time I look at part of the new features I'm just aghast with how poorly these specs are drafted and don't account for common usage scenarios. Or are too vague to let browser vendors skimp on feature sets that make the functionality work across browsers more consistently (video formats anyone?)&lt;/p&gt;
&lt;p&gt;I definitely think that it's high time HTML gets better input support, but the current HTML 5 state of affairs isn't going to help much. In fact, I think it's going to get in the way more than it actually helps… &lt;/p&gt;
&lt;p&gt;It seems to me it wouldn't be rocket science to build basic user input controls like a date picker that actually address the 90%+ scenario. Pick a date from a dropdown and display the date in local browser time format. How hard could that possibly be? Or have a URL validator that understands both full URLs and partial URLs. I mean that's a problem that's been solved many times over in just about any application that accepts URLs as user input, right? Heck mobile device browsers actually get this right.&lt;/p&gt;
&lt;p&gt;But no, on desktop browsers we get everything in a standards compliant geek format that you can't possibly push on to users. :-( Let's hope going forward that things will improve and more thought to the end user scenarios&amp;nbsp; will go into the actual browser implementations.&lt;/p&gt;
&lt;p&gt;I understand it's not an easy problem to solve - no one solution is going to fit every situation, but the crap that's currently shipping in desktop browsers is next to useless or worse disruptive and actually makes the controls unusable in many scenarios. At the very least these controls need full opt-out settings that leave the styling and validation to the designer/developer, but leaves the semantic markup in place. One can hope…&lt;/p&gt;
&lt;h3&gt;Your Turn&lt;/h3&gt;
&lt;p&gt;What do you think? Have you played with HTML 5 input controls? Do you think these new input control features would help you? What would you need to make them useful?&lt;/p&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category=HTML'&gt;HTML&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category= ASP.NET'&gt; ASP.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=HTML5'&gt;HTML5&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="HTML 5 Input Types - How useful is this really going to be?" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2011/Dec/10/HTML-5-Input-Types-How-useful-is-this-really-going-to-be"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2011/Dec/10/HTML-5-Input-Types-How-useful-is-this-really-going-to-be" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=fmykZafdcqM:RYURNs3_x80:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=fmykZafdcqM:RYURNs3_x80:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=fmykZafdcqM:RYURNs3_x80:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=fmykZafdcqM:RYURNs3_x80:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=fmykZafdcqM:RYURNs3_x80:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=fmykZafdcqM:RYURNs3_x80:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=fmykZafdcqM:RYURNs3_x80:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=fmykZafdcqM:RYURNs3_x80:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=fmykZafdcqM:RYURNs3_x80:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/fmykZafdcqM" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2011/Dec/10/HTML-5-Input-Types-How-useful-is-this-really-going-to-be</feedburner:origLink></item>
    <item>
      <title>HTML 5 Input Types on WebForms Controls</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/8uH9ox38o90/HTML-5-Input-Types-on-WebForms-Controls</link>
      <guid isPermaLink="false">1207943_201112110424</guid>
      <pubDate>Sun, 11 Dec 2011 04:24:48 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2011/Dec/10/HTML-5-Input-Types-on-WebForms-Controls#Comments</comments>
      <slash:comments>5</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1207943</wfw:commentRss>
      <category> ASP.NET</category>
      <description>&lt;p&gt;Did you know that you can use HTML5 input types with ASP.NET WebForms controls? I wasn't sure until I tried it today:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: maroon"&gt;TextBox &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;="server" &lt;/span&gt;&lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;="Username" &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;="250px" &lt;strong&gt; &lt;/strong&gt;&lt;/span&gt;&lt;span style="color: red"&gt;&lt;strong&gt;type&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;strong&gt;="email"&lt;/strong&gt; /&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;which properly produces this HTML5 compliant HTML output:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;&lt;strong&gt;type&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;strong&gt;="email"&lt;/strong&gt; &lt;/span&gt;&lt;span style="color: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;="&lt;/span&gt;&lt;span style="color: red"&gt;width&lt;/span&gt;&lt;span style="color: blue"&gt;:250px;" &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;="Username" &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;="Username"&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;That this works shouldn't come as a big surprise since ASP.NET always has supported 'extra' attributes on Web Controls to render into the HTML. However, the input type seems like a pretty core feature in input controls and the control does have a TextMode property that already partially addresses this task. Nevertheless, explicitly specifying the type attribute allows you to override the type and so provide custom HTML 5 input types.&lt;/p&gt;
&lt;p&gt;In short - nice! HTML5 input types are supported in WebForms without any framework changes or updates.&lt;/p&gt;&lt;font color="#c0504d" size="1"&gt;[updated from comments]&lt;/font&gt;&lt;br&gt;&lt;em&gt;Apparently, in order for this to work you need an &lt;/em&gt;&lt;a href="http://blogs.msdn.com/b/scothu/archive/2011/08/10/html-5-updates-for-net-4.aspx" target="_blank"&gt;&lt;em&gt;update for for the .NET 4 runtimes&lt;/em&gt;&lt;/a&gt;&lt;em&gt; which was shipped with Visual Studio 2010 SP1. However on live servers you might have to install the Hotfix described in the previous link. I say might, because this code worked on my live server and I didn't explicitly install the hotfix - it appears that some of the .NET 4 updates through Windows Update must have addressed this. To be sure you should test a page that holds controls with HTML5 types to ensure the server will handle this - if not install the hotfix.&lt;/em&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category= ASP.NET'&gt; ASP.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="HTML 5 Input Types on WebForms Controls" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2011/Dec/10/HTML-5-Input-Types-on-WebForms-Controls"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2011/Dec/10/HTML-5-Input-Types-on-WebForms-Controls" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=8uH9ox38o90:YGiFZOisPRQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=8uH9ox38o90:YGiFZOisPRQ:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=8uH9ox38o90:YGiFZOisPRQ:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=8uH9ox38o90:YGiFZOisPRQ:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=8uH9ox38o90:YGiFZOisPRQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=8uH9ox38o90:YGiFZOisPRQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=8uH9ox38o90:YGiFZOisPRQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=8uH9ox38o90:YGiFZOisPRQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=8uH9ox38o90:YGiFZOisPRQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/8uH9ox38o90" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2011/Dec/10/HTML-5-Input-Types-on-WebForms-Controls</feedburner:origLink></item>
    <item>
      <title>A Key Code Checker for DOM Keyboard Events</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/BIZG_ZE2guw/A-Key-Code-Checker-for-DOM-Keyboard-Events</link>
      <guid isPermaLink="false">1205379_201112081147</guid>
      <pubDate>Thu, 08 Dec 2011 11:47:20 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2011/Dec/08/A-Key-Code-Checker-for-DOM-Keyboard-Events#Comments</comments>
      <slash:comments>0</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1205379</wfw:commentRss>
      <category>JavaScript</category>
      <category>jQuery</category>
      <category>HTML</category>
      <description>&lt;p&gt;If you've ever written some code that needs to deal with individual keystrokes entered and to 'translate' or parse them, you've probably figured out that while on the surface it all looks pretty easy with DOM event processing, it's actually quite tricky to get accurate key information. There are several keyboard event properties available and - even with &lt;a href="http://www.jquery.com" target="_blank"&gt;jQuery&lt;/a&gt; - various browsers handle these event properties slightly differently.&lt;/p&gt; &lt;p&gt;In fact, I realized that almost every time I run into a problem with keyCodes in event handling code I add a bunch of console.log statements to figure out exactly what keyCodes are coming back for each of the available events. So tonight I sat down and threw together a small form that lets me see all this information at a glance on one page. &lt;/p&gt; &lt;p&gt;You can check out my Key Code Tester and play around with it in various browsers:&lt;/p&gt; &lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/JavaScript-and-DOM-Event-KeyCodes_12800/KeyCodeChecker_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="KeyCodeChecker" border="0" alt="KeyCodeChecker" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/JavaScript-and-DOM-Event-KeyCodes_12800/KeyCodeChecker_thumb.png" width="701" height="944"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;&lt;a href="http://www.west-wind.com/WestwindWebToolkit/samples/Ajax/html5andCss3/keycodechecker.aspx" target="_blank"&gt;&lt;strong&gt;Try the Key Code Checker Example&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;If you try this in various browsers, you'll find that you get a rather large divergence of values for the various events. &lt;/p&gt; &lt;p&gt;But before we look at the actual results and some suggestions, let's back up for a second and explain how key handling in the DOM works.&lt;/p&gt; &lt;h3&gt;Key Event Handling for the DOM&lt;/h3&gt; &lt;p&gt;The HTML DOM has support for keyboard events that fire when you press keys anywhere in the document. The most common place where the event trapping matters is on input controls like a textbox, but it also can work in any other DOM element like a &amp;lt;div&amp;gt;.&lt;/p&gt; &lt;p&gt;The DOM keyboard input event model has a number of events to handle key events that you can handle:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;keydown  &lt;li&gt;keypress  &lt;li&gt;keyup &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;These events are fairly self-explanatory on the surface and they fire in the order shown above. Each event can be hooked up to an event handler that receives a DOM event parameter which contains information like keyCode, charCode and which as well as shift,ctl, alt states you can look at. Most modern browsers support the keyCode, charCode and which properties. which is a special property that returns the 'significant' value from an event which in the case of the key events tends to be the keyCode or if that is empty the charCode. charCode and which are not available on old versions of IE pre version 9. This can get tricky to check for quickly.and this is where jQuery comes in and provides at least a minimal bit of normalization of these event values.&lt;/p&gt; &lt;h3&gt;jQuery Event Normalization of Key Event Properties&lt;/h3&gt; &lt;p&gt;jQuery's Event Object supports &lt;em&gt;keyCode&lt;/em&gt;, &lt;em&gt;charCode&lt;/em&gt; and &lt;em&gt;which&lt;/em&gt; properties as well, but you typically should use the which property with it. jQuery normalizes which based on keyCode or charCode. It checks keyCode first and if that's empty reads the charCode which should yield a valid keyboard code.&lt;/p&gt; &lt;p&gt;Back to the keyboard events: keydown fires as a key is pressed down. keypress is fired immediately after keydown, but unlike keydown and keyup it fires only on 'visible' characters that are printable (FireFox and Opera don't follow the spec and always fire). keyup fires after keypress when the keys are released.&lt;/p&gt; &lt;p&gt;At any point during the event handler processing for the keyboard events you can return false which causes the event bubbling to stop. When false is returned further processing stops and key events following the current key event won't fire. If you return false from keyDown, keypress and keyup will not be fired.&lt;/p&gt; &lt;p&gt;Using &lt;a href="http://jquery.com/" target="_blank"&gt;jQuery&lt;/a&gt; it's very easy to handle keyboard events. Here's an example for keydown handling:&lt;/p&gt;&lt;pre class="code"&gt;$(&lt;span style="color: maroon"&gt;"#txtKey"&lt;/span&gt;)
    .keydown(&lt;span style="color: blue"&gt;function &lt;/span&gt;(e) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;keyCode = e.keyCode;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;which = e.which;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;charCode = e.charCode;
    
    &lt;span style="color: #006400"&gt;// .. do something for key handling       

    // pass through key stroke with true 
    // keep keystroke from processing further (or get entered) with false 
    &lt;/span&gt;&lt;span style="color: blue"&gt;return true&lt;/span&gt;;
});
&lt;/pre&gt;
&lt;h3&gt;Sample Implementation&lt;/h3&gt;
&lt;p&gt;The above form was implemented using a little bit of script code that basically handles all three key events. It then calls a common function and displays the various key code values in the appropriate box in the page.&lt;/p&gt;
&lt;p&gt;The code to do this is pretty simple, but I post it here as it gives a little bit of insight on how the various key event properties work:&lt;/p&gt;&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;="text/javascript"&amp;gt;
        &lt;/span&gt;$( &lt;span style="color: blue"&gt;function &lt;/span&gt;() {
            $(&lt;span style="color: maroon"&gt;"#txtKey"&lt;/span&gt;)
                .focus()
                .keypress(handleKey)
                .keydown(handleKey)                
                .keyup(handleKey);
        });

        &lt;span style="color: blue"&gt;function &lt;/span&gt;handleKey(e) {            
            &lt;span style="color: blue"&gt;var &lt;/span&gt;keyCode = e.keyCode;
            &lt;span style="color: blue"&gt;var &lt;/span&gt;charCode = e.charCode;
            &lt;span style="color: blue"&gt;var &lt;/span&gt;which = e.which;

            &lt;span style="color: blue"&gt;var &lt;/span&gt;type = e.handleObj.origType;            
            &lt;span style="color: blue"&gt;var &lt;/span&gt;orig = e.orignalEvent;

            &lt;span style="color: blue"&gt;if &lt;/span&gt;(!keyCode)
                keyCode = &lt;span style="color: maroon"&gt;"0"&lt;/span&gt;;
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(!which)
                which = &lt;span style="color: maroon"&gt;"0"&lt;/span&gt;;

            &lt;span style="color: blue"&gt;if &lt;/span&gt;(type == &lt;span style="color: maroon"&gt;"keydown"&lt;/span&gt;) {
                $(&lt;span style="color: maroon"&gt;"#divDown .mainvalue"&lt;/span&gt;).text(keyCode);
                $(&lt;span style="color: maroon"&gt;"#divCharCodeDown .mainvalue"&lt;/span&gt;).text(charCode);
                $(&lt;span style="color: maroon"&gt;"#divWhichDown .mainvalue"&lt;/span&gt;).text(which);

                $(&lt;span style="color: maroon"&gt;"#divCharTyped .mainvalue:eq(1)"&lt;/span&gt;)
                    .text(&lt;span style="color: maroon"&gt;""&lt;/span&gt;)
                    .text(String.fromCharCode(keyCode));

                &lt;span style="color: #006400"&gt;// also clear out key press values in case it doesn't fire                
                &lt;/span&gt;$(&lt;span style="color: maroon"&gt;"#divCharTyped .mainvalue:eq(0)"&lt;/span&gt;)
                    .html(&lt;span style="color: maroon"&gt;"&amp;amp;nbsp;"&lt;/span&gt;)
                $(&lt;span style="color: maroon"&gt;"#divPress .mainvalue"&lt;/span&gt;).text(&lt;span style="color: maroon"&gt;""&lt;/span&gt;);
                $(&lt;span style="color: maroon"&gt;"#divCharCodePress .mainvalue"&lt;/span&gt;).text(&lt;span style="color: maroon"&gt;""&lt;/span&gt;);
                $(&lt;span style="color: maroon"&gt;"#divWhichPress .mainvalue"&lt;/span&gt;).text(&lt;span style="color: maroon"&gt;""&lt;/span&gt;);
            }
            &lt;span style="color: blue"&gt;else if &lt;/span&gt;(type == &lt;span style="color: maroon"&gt;"keypress"&lt;/span&gt;) {                
                $(&lt;span style="color: maroon"&gt;"#divPress .mainvalue"&lt;/span&gt;).text(keyCode);
                $(&lt;span style="color: maroon"&gt;"#divCharCodePress .mainvalue"&lt;/span&gt;).text(charCode);
                $(&lt;span style="color: maroon"&gt;"#divWhichPress .mainvalue"&lt;/span&gt;).text(which);
                
                $(&lt;span style="color: maroon"&gt;"#divCharTyped .mainvalue:eq(0)"&lt;/span&gt;)
                    .text(String.fromCharCode(which));
            }
            &lt;span style="color: blue"&gt;else if &lt;/span&gt;(type == &lt;span style="color: maroon"&gt;"keyup"&lt;/span&gt;) {
                $(&lt;span style="color: maroon"&gt;"#divUp .mainvalue"&lt;/span&gt;).text(keyCode);
                $(&lt;span style="color: maroon"&gt;"#divCharCodeUp .mainvalue"&lt;/span&gt;).text(charCode);
                $(&lt;span style="color: maroon"&gt;"#divWhichUp .mainvalue"&lt;/span&gt;).text(which);

                $(&lt;span style="color: maroon"&gt;"#divCharTyped .mainvalue:eq(2)"&lt;/span&gt;)
                    .text(&lt;span style="color: maroon"&gt;""&lt;/span&gt;)
                    .text(String.fromCharCode(keyCode));                
            }

            

            &lt;span style="color: #006400"&gt;// must return true in order for keypress to fire
            // in all browsers. Remove character using setTimeout 
            // to delay and clear text after the fact.
            &lt;/span&gt;setTimeout(&lt;span style="color: blue"&gt;function &lt;/span&gt;() { $(&lt;span style="color: maroon"&gt;"#txtKey"&lt;/span&gt;).val(&lt;span style="color: maroon"&gt;""&lt;/span&gt;); }, 2);

            &lt;span style="color: #006400"&gt;// always return true so keypress fires
            &lt;/span&gt;&lt;span style="color: blue"&gt;return true&lt;/span&gt;;
        }        
    &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Browser Divergence&lt;/h3&gt;
&lt;p&gt;I mentioned earlier that various browsers handle the various key codes differently. Check out the following when pressing the 'a' key in FireFox, Chrome, IE (8 standards mode), Opera respectively:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/JavaScript-and-DOM-Event-KeyCodes_12800/browserDifferences_4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="browserDifferences" border="0" alt="browserDifferences" src="http://www.west-wind.com/Weblog/images/200901/Windows-Live-Writer/JavaScript-and-DOM-Event-KeyCodes_12800/browserDifferences_thumb_1.png" width="1024" height="398"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;You can see the divergence here. FireFox doesn't get a good keyCode in the keyPress event, and instead gives a charCode. All other browsers tested return a valid keycode in all events. Notice that all browsers return consistent results with jQuery using the e.which property. The which property is clearly what you should use to get a reliable and consistent keyCode value that works across all browsers.&lt;/p&gt;
&lt;h3&gt;Suggestions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Use e.which when checking for Key Codes&lt;br&gt;&lt;/strong&gt;When using jQuery it's best to use e.which in key events to check for key codes. e.which normalizes between all key combinations that I saw and provides the most consistent value across browsers.&lt;br&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;If you need a Character Value from a Key Code use keypress Event&lt;br&gt;&lt;/strong&gt;When you need to get a character value from a key event handle the keypress event as it's the only one that can receive a translated key code that reflects the actual character that the user typed and that would appear in the text box. You can use &lt;em&gt;String.fromCharCode(e.which)&lt;/em&gt; to get the printable character.&lt;br&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Watch out for keypress Event Differences&lt;br&gt;&lt;/strong&gt;Keep in mind that the keypress only fires on printable characters and that the key code value for e.which will be different than those in keydown and keyup. If you need to handle both printable and special keys you might have to implement both keyDown and keyPress.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Hopefully this utility plus some of these suggestions will prove useful to some of you. I know it will be to me the next time I have a need to manipulate keystrokes in my JavaScript code - information like this has a half life of about 2 days with me after it files out of my brain again :-). This blog post should help me remember next time…&lt;/p&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category=JavaScript'&gt;JavaScript&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=jQuery'&gt;jQuery&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=HTML'&gt;HTML&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="A Key Code Checker for DOM Keyboard Events" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2011/Dec/08/A-Key-Code-Checker-for-DOM-Keyboard-Events"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2011/Dec/08/A-Key-Code-Checker-for-DOM-Keyboard-Events" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=BIZG_ZE2guw:Tr3kTdOLltI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=BIZG_ZE2guw:Tr3kTdOLltI:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=BIZG_ZE2guw:Tr3kTdOLltI:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=BIZG_ZE2guw:Tr3kTdOLltI:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=BIZG_ZE2guw:Tr3kTdOLltI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=BIZG_ZE2guw:Tr3kTdOLltI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=BIZG_ZE2guw:Tr3kTdOLltI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=BIZG_ZE2guw:Tr3kTdOLltI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=BIZG_ZE2guw:Tr3kTdOLltI:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/BIZG_ZE2guw" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2011/Dec/08/A-Key-Code-Checker-for-DOM-Keyboard-Events</feedburner:origLink></item>
    <item>
      <title>Creating a Dynamic DataReader for easier Property Access</title>
      <link>http://feedproxy.google.com/~r/RickStrahl/~3/ldPMnHozrSM/Creating-a-Dynamic-DataReader-for-easier-Property-Access</link>
      <guid isPermaLink="false">1203452_201112061304</guid>
      <pubDate>Tue, 06 Dec 2011 13:04:04 GMT</pubDate>
      <dc:creator>Rick Strahl</dc:creator>
      <comments>http://www.west-wind.com/weblog/posts/2011/Dec/06/Creating-a-Dynamic-DataReader-for-easier-Property-Access#Comments</comments>
      <slash:comments>3</slash:comments>
      <wfw:commentRss>http://west-wind.com/weblog/commentrss.aspx?id=1203452</wfw:commentRss>
      <category>.NET</category>
      <category>CSharp</category>
      <category> ADO.NET</category>
      <description>&lt;p&gt;I've been thrown back to using plain old ADO.NET for a bit in a legacy project I'm helping one of my customers with and in the process am finding a few new ways to take advantage of .NET 4 language features to make a number of operations easier. Specifically I'm finding that the new Dynamic type in .NET 4.0 can make a number of operations easier to use and considerably cleaner to read and type. A couple of weeks ago I posted an example of a &lt;a href="http://www.west-wind.com/weblog/posts/2011/Nov/24/Creating-a-Dynamic-DataRow-for-easier-DataRow-Syntax" target="_blank"&gt;DynamicDataRow class that uses dynamic to expose DataRow column values as properties&lt;/a&gt;. &lt;/p&gt; &lt;p&gt;In this post I do something similar with for the ADO.NET DataReader by exposing a custom dynamic type that retrieves values from the DataReader field collection and exposes them as properties.&lt;/p&gt; &lt;h3&gt;Why hack the DataReader?&lt;/h3&gt; &lt;p&gt;DbDataReader is one of the lowest level ADO.NET data structures - it returns raw firehose data from the database and exposes it via a simple reader interface that relies on simple looping and internal collection of field keys and values. &lt;/p&gt; &lt;p&gt;Here's a small example that demonstrates the basics using a DataAccess helper &lt;a href="http://www.west-wind.com:8080/svn/WestwindWebToolkit/trunk/Westwind.Utilities/Data/" target="_blank"&gt;SqlDataAccess from Westwind.Utilities&lt;/a&gt; from the &lt;a href="http://www.west-wind.com/WestwindWebToolkit/" target="_blank"&gt;West Wind Web Toolkit&lt;/a&gt; to keep this example small.&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;BasicDataReaderTimerTests()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;data = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlDataAccess&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"WebStore_ConnectionString"&lt;/span&gt;);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;reader = data.ExecuteReader(&lt;span style="color: #a31515"&gt;"select * from wws_items"&lt;/span&gt;);
    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsNotNull(reader, &lt;span style="color: #a31515"&gt;"Query Failure: " &lt;/span&gt;+ data.ErrorMessage);
    
    &lt;span style="color: #2b91af"&gt;StringBuilder &lt;/span&gt;sb = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StringBuilder&lt;/span&gt;();
    &lt;span style="color: green"&gt;
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Stopwatch &lt;/span&gt;watch = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Stopwatch&lt;/span&gt;();
    watch.Start();
    
    &lt;span style="color: blue"&gt;while &lt;/span&gt;(reader.Read())
    {
&lt;strong&gt;        &lt;span style="color: blue"&gt;string &lt;/span&gt;sku  = reader[&lt;span style="color: #a31515"&gt;"sku"&lt;/span&gt;] &lt;span style="color: blue"&gt;as string&lt;/span&gt;;
        &lt;span style="color: blue"&gt;string &lt;/span&gt;descript = reader[&lt;span style="color: #a31515"&gt;"descript"&lt;/span&gt;] &lt;span style="color: blue"&gt;as string&lt;/span&gt;;

        &lt;span style="color: blue"&gt;decimal&lt;/span&gt;? price;
        &lt;span style="color: blue"&gt;object &lt;/span&gt;t = reader[&lt;span style="color: #a31515"&gt;"Price"&lt;/span&gt;];
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(t == &lt;span style="color: #2b91af"&gt;DBNull&lt;/span&gt;.Value)
            price = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;else
            &lt;/span&gt;price = (&lt;span style="color: blue"&gt;decimal&lt;/span&gt;)t;&lt;/strong&gt;
        
        
        sb.AppendLine(sku + &lt;span style="color: #a31515"&gt;" " &lt;/span&gt;+ descript + &lt;span style="color: #a31515"&gt;" " &lt;/span&gt;+ price.Value.ToString(&lt;span style="color: #a31515"&gt;"n2"&lt;/span&gt;));                    
    }

    watch.Stop();

    reader.Close();

    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(watch.ElapsedMilliseconds.ToString());
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(sb.ToString());                                
}&lt;/pre&gt;
&lt;p&gt;The code is pretty straight forward. SqlDataAccess takes a connection string or connection string name from the config file as a parameter in the constructor to initialize the DAL. Once instantiated you can run a number of data access commands to create a DataReader (or DataSet/DataTable) or execute commands in various ways on this connection. Here I use ExecuteReader() to produce a DataReader I can loop through. The code then loops through the records using reader.Read() which returns false when the end of the result set is reached.&lt;/p&gt;
&lt;p&gt;Inside of the loop I can then access each of the fields. Notice that each field has to be explicitly cast to a specific type (as string or (decimal) here). In addition if you support NULL values in the database you also have to explicitly check for DBNull values showing up in the DataReader fields which is messy.&lt;/p&gt;
&lt;p&gt;It's not terribly complicated to do any of this - just a bit of extra typing and - aesthetically - the code looks a bit messy.&lt;/p&gt;
&lt;h3&gt;Using DynamicDataReader&lt;/h3&gt;
&lt;p&gt;Personally I prefer standard object.property syntax when dealing with data and a custom dynamic type can actually make that easy. While it won't give me a full strongly typed .NET type, I can at least get the standard object syntax with this implementation.&lt;/p&gt;
&lt;p&gt;Here's the code that does exactly the same thing using the DynamicDataReader:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;BasicDynamicDataReaderTimerTest()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;data = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlDataAccess&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"WebStore_ConnectionString"&lt;/span&gt;);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;reader = data.ExecuteReader(&lt;span style="color: #a31515"&gt;"select * from wws_items"&lt;/span&gt;);

    &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsNotNull(reader, &lt;span style="color: #a31515"&gt;"Query Failure: " &lt;/span&gt;+ data.ErrorMessage);

    &lt;strong&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;dreader = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicDataReader&lt;/span&gt;(reader);
&lt;/strong&gt;
&lt;span style="color: #2b91af"&gt;    Stopwatch &lt;/span&gt;watch = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Stopwatch&lt;/span&gt;();
    watch.Start();


    &lt;span style="color: blue"&gt;while &lt;/span&gt;(reader.Read())
    {
&lt;strong&gt;        &lt;span style="color: blue"&gt;string &lt;/span&gt;sku = dreader.Sku;
        &lt;span style="color: blue"&gt;string &lt;/span&gt;descript = dreader.Descript;
        &lt;span style="color: blue"&gt;decimal&lt;/span&gt;? price = dreader.Price;
&lt;/strong&gt;
        sb.AppendLine(sku + &lt;span style="color: #a31515"&gt;" " &lt;/span&gt;+ descript + &lt;span style="color: #a31515"&gt;" " &lt;/span&gt;+ price.Value.ToString(&lt;span style="color: #a31515"&gt;"n2"&lt;/span&gt;));
    }

    watch.Stop();&lt;/pre&gt;&lt;pre class="code"&gt;    reader.Close();

    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(watch.ElapsedMilliseconds.ToString());
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(sb.ToString());
}&lt;/pre&gt;
&lt;p&gt;The code is nearly the same except in the two places highlighted in bold: a new DynamicDataReader() instance is created and the values assigned are read from this dreader instance as property values.&lt;/p&gt;
&lt;p&gt;Even though this is a simple example that only uses 3 fields, it still is quite a bit cleaner than the first example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It uses cleaner object.property syntax 
&lt;li&gt;All the type casting is gone 
&lt;li&gt;The DBNull to .NET NULL assignment is automatic &lt;/li&gt;&lt;/ul&gt;
&lt;h3&gt;Good News, Bad News&lt;/h3&gt;
&lt;p&gt;It's important to understand that what you're seeing is a dynamic type, not a strongly typed .NET type. Dynamic means you get to use object.property syntax in this case, and you get automatic casting, but you &lt;em&gt;do not&lt;/em&gt; get strong typing and so no compiler type checking or Intellisense on those properties. They are dynamic and so essentially syntactic sugar around dynamic invocation and Reflection implemented through the Dynamic Language Runtime (DLR).&lt;/p&gt;
&lt;p&gt;Because the type is &lt;em&gt;dynamic&lt;/em&gt; there's also a performance penalty. Specifically first time access of the dynamic properties tends to be slow. Once the DLR is spun up and a dynamic type created from the DataReader and you've iterated over each property once, the parsing is fairly swift on repeated calls/conversions. In informal testing it looks like the dynamic code takes roughly three times as long as the raw code from a cold start, and is a little over 1.5 times slower once the dynamic type has been created once. Not sure why that is because the implementation just does look ups into the DataReader field collection (no Reflection caching for PropertyInfo data), but nevertheless repeated requests are significantly faster than first time access.&lt;/p&gt;
&lt;p&gt;Even through performance was nearly twice as slow using the dynamic type, the numbers were still very fast taking less than 8 milliseconds for rendering 500 records compared to 4-5 with raw DataReader access. Hardly a deal breaker in all but the most critical scenarios especially when you figure in the cost of data access (which the example code doesn't for the timings).&lt;/p&gt;
&lt;h3&gt;How does DynamicDataReader work?&lt;/h3&gt;
&lt;p&gt;The DLR makes it very easy to abstract data structures and wrap them into an object based syntax. Using DynamicObject as a base class to implement custom types, you can basically implement 'method missing' or 'property missing' functionality by simply overriding the TryGetMember() method and TryInvokeMember() methods. &lt;/p&gt;
&lt;p&gt;Here's the implementation of DynamicDataReader:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;This class provides an easy way to use object.property
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;syntax with a DataReader by wrapping a DataReader into
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;a dynamic object.
&lt;/span&gt;&lt;span style="color: gray"&gt;/// 
/// &lt;/span&gt;&lt;span style="color: green"&gt;The class also automatically fixes up DbNull values
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;(null into .NET and DbNUll)
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicDataReader &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DynamicObject
&lt;/span&gt;{
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Cached Instance of DataReader passed in
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDataReader &lt;/span&gt;DataReader;
    
    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Pass in a loaded DataReader
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name="dataReader"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;DataReader instance to work off&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;DynamicDataReader(&lt;span style="color: #2b91af"&gt;IDataReader &lt;/span&gt;dataReader)
    {
        DataReader = dataReader;
    }

   &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
   /// &lt;/span&gt;&lt;span style="color: green"&gt;Returns a value from the current DataReader record
   &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;If the field doesn't exist null is returned.
   &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;DbNull values are turned into .NET nulls.
   &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
   /// &amp;lt;param name="binder"&amp;gt;&amp;lt;/param&amp;gt;
   /// &amp;lt;param name="result"&amp;gt;&amp;lt;/param&amp;gt;
   /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TryGetMember(&lt;span style="color: #2b91af"&gt;GetMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result)
    {
        result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;

        &lt;span style="color: green"&gt;// 'Implement' common reader properties directly
        &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(binder.Name == &lt;span style="color: #a31515"&gt;"IsClosed"&lt;/span&gt;)            
            result = DataReader.IsClosed;                            
        &lt;span style="color: blue"&gt;else if &lt;/span&gt;(binder.Name == &lt;span style="color: #a31515"&gt;"RecordsAffected"&lt;/span&gt;)            
            result = DataReader.RecordsAffected;                         
        &lt;span style="color: green"&gt;// lookup column names as fields
        &lt;/span&gt;&lt;span style="color: blue"&gt;else
        &lt;/span&gt;{
 &lt;strong&gt;           &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;try
            &lt;/span&gt;{
                result = DataReader[binder.Name];
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(result == &lt;span style="color: #2b91af"&gt;DBNull&lt;/span&gt;.Value)
                    result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;                    
            }
            &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;catch 
            &lt;/span&gt;{
                result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
                &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
            }
&lt;/strong&gt;        }

        &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
    }

    &lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TryInvokeMember(&lt;span style="color: #2b91af"&gt;InvokeMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] args, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result)
    {
        &lt;span style="color: green"&gt;// Implement most commonly used method
        &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(binder.Name == &lt;span style="color: #a31515"&gt;"Read"&lt;/span&gt;)
            result = DataReader.Read();
        &lt;span style="color: blue"&gt;else if &lt;/span&gt;(binder.Name == &lt;span style="color: #a31515"&gt;"Close"&lt;/span&gt;)
        {
            DataReader.Close();
            result = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        }
        &lt;span style="color: blue"&gt;else
            &lt;/span&gt;&lt;span style="color: green"&gt;// call other DataReader methods using Reflection (slow - not recommended)
            // recommend you use full DataReader instance
            &lt;/span&gt;result = &lt;span style="color: #2b91af"&gt;ReflectionUtils&lt;/span&gt;.CallMethod(DataReader, binder.Name, args);

        &lt;span style="color: blue"&gt;return true&lt;/span&gt;;            
    }
}&lt;/pre&gt;
&lt;p&gt;As you can see the implementation is super simple. The implementation inherits from Dynamic object and overrides TryGetMember() and TryInvokeMember(). The constructor is set up so it passes in a DataReader instance which is stored internally. TryGetMember() is then called when an 'unknown' property is accessed on the dynamic type and tries to find a matching value in the DataReader field collection based on the binder.Name property.&lt;/p&gt;
&lt;p&gt;The class also implements a couple of DataReader's common properties (IsClosed, RecordsAffected) and methods (Read, Close) to match a full DataReader's functionality so it can behave like a full DataReader instance, so you can write code like this:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;reader = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicDataReader&lt;/span&gt;(data.ExecuteReader(&lt;span style="color: #a31515"&gt;"select * from wws_items"&lt;/span&gt;));&lt;br&gt;&lt;span style="color: blue"&gt;while &lt;/span&gt;(reader.Read()) &lt;br&gt;{ … }&lt;/pre&gt;
&lt;p&gt;The key feature however is that values from the DataReader fields collection are turned into properties which is handled by &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;font face="Courier New"&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;try &lt;br&gt;&lt;/span&gt;{ &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = DataReader[binder.Name]; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if &lt;/span&gt;(result == &lt;span style="color: #2b91af"&gt;DBNull&lt;/span&gt;.Value) &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/strong&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;strong&gt;result = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;br&gt;} &lt;br&gt;&lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;catch &lt;/span&gt;{ result = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;span style="color: blue"&gt;return false&lt;/span&gt;; } &lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Note that the code handles the null conversion and the assignment of the result value the value from the DataReader field. TryGetMember() expects an out parameter for result and the value set is what effectively becomes the property value that is used when accessing the object.property syntax.&lt;/p&gt;
&lt;p&gt;It's neat how easy it is to implement custom behavior in TryGetMember(). Note that I can check for explicit values (like IsClosed and RecordsAffected) as well as checking the fields collection for matching values. IOW, you can have a single dynamic return values for properties from any number of 'data sources' easily, be it from static translations, an internal data structure like a DataReader or DataRow, an XML document or even via Reflection from additional properties on the object. You have full control over this implementation which is very powerful and opens up many more avenues to simplify structured data access.&lt;/p&gt;
&lt;h3&gt;Even easier: Get a DynamicDataReader directly from the DAL&lt;/h3&gt;
&lt;p&gt;With DynamicDataReader available, it's now a cinch to extend my DAL to directly return a dynamic data reader instance. We earlier saw the SqlDataAccess.ExecuteReader() method which returned a DataReader. In the SqlDataAccessBase class I can now implement a dynamic version of ExecuteReader that directly returns a DynamicDataReader instance as a dynamic type:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/span&gt;&lt;span style="color: green"&gt;Executes a Sql statement and returns a dynamic DataReader instance 
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;that exposes each field as a property
&lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;param name="sql"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Sql String to executeTable&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
/// &amp;lt;param name="parameters"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;Array of DbParameters to pass&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/param&amp;gt;
/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;public virtual dynamic &lt;/span&gt;ExecuteDynamicDataReader(&lt;span style="color: blue"&gt;string &lt;/span&gt;sql, &lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DbParameter&lt;/span&gt;[] parameters)
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;reader = ExecuteReader(sql, parameters);
    &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicDataReader&lt;/span&gt;(reader);
}&lt;/pre&gt;
&lt;p&gt;which directly returns me a dynamic DataReader instance. Note that the type returned from ExecuteDynamicDataReader() is not DynamicDataReader but &lt;em&gt;dynamic! &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I can now fire off a query simply like this and use my object.property syntax without any conversion:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;data = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlDataAccess&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"WebStore_ConnectionString"&lt;/span&gt;);
&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;reader = data.ExecuteDynamicDataReader(&lt;span style="color: #a31515"&gt;"select * from wws_items"&lt;/span&gt;);

&lt;span style="color: #2b91af"&gt;StringBuilder &lt;/span&gt;sb = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StringBuilder&lt;/span&gt;();

&lt;span style="color: blue"&gt;while &lt;/span&gt;(reader.Read())
{
    &lt;span style="color: blue"&gt;string &lt;/span&gt;sku = reader.Sku;
    &lt;span style="color: blue"&gt;string &lt;/span&gt;descript = reader.Descript;
    &lt;span style="color: blue"&gt;decimal&lt;/span&gt;? price = reader.Price;

    sb.AppendLine(sku + &lt;span style="color: #a31515"&gt;" " &lt;/span&gt;+ descript + &lt;span style="color: #a31515"&gt;" " &lt;/span&gt;+ price.Value.ToString(&lt;span style="color: #a31515"&gt;"n2"&lt;/span&gt;));
}
reader.Close();&lt;/pre&gt;
&lt;p&gt;Note that Read() and Close() work on the &lt;em&gt;dynamic&lt;/em&gt; because I explicitly implemented them in TryInvokeMember() based on the method name.&lt;/p&gt;
&lt;h3&gt;Summary&lt;/h3&gt;
&lt;p&gt;Sweet. This makes it super easy and transparent to access data with clean syntax! Personally I much prefer object.property syntax over collection syntax plus type casting and so I'm sold on this concept of using custom dynamic types for wrapping non object data structures into object syntax dynamic types. &lt;/p&gt;
&lt;p&gt;Clearly this is not something you want to use for all occasions. Were performance is is of utmost importance, raw DataReader access is still a better choice. But for smallish result sets or one-off queries in application (especially admin interfaces) this can be a nice enhancement to make code easier to read and maintain.&lt;/p&gt;
&lt;p&gt;Also, direct DataReader access - for me at least - seems to be going the way of the Dodo, with ORM based data access mostly replacing raw ADO.NET data access. But there are occasions even with an ORM that I fall back to DataReaders with complex queries or maintenance tasks where mapping a one of query to a type is simply overkill. Using this class though I can at least have ORM-like syntax in my code even if strong typing is not available.&lt;/p&gt;
&lt;p&gt;And as I can attest to at the moment - old code dies only slowly - and I still find myself digging around in 10 year old code from time to time that uses low level ADO.NET data access and it's nice to have some tools to modernize that old code with, with minimal effort. This tool fits the bill for me.&lt;/p&gt;
&lt;h3&gt;Resources&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://nuget.org/packages/Westwind.Utilities" target="_blank"&gt;&lt;strong&gt;Westwind.Utilities from NuGet&lt;/strong&gt;&lt;/a&gt; 
&lt;li&gt;&lt;a href="http://www.west-wind.com/WestwindWebToolkit/" target="_blank"&gt;&lt;strong&gt;West Wind Web &amp;amp; AJAX Toolkit&lt;/strong&gt;&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;div style='margin: 10px 0px'&gt;&lt;small&gt;© Rick Strahl, West Wind Technologies, 2005-2012&lt;/small&gt;&lt;/div&gt;&lt;div&gt;Posted in &lt;b&gt;&lt;a href='/Weblog/ShowPosts.aspx?Category=.NET'&gt;.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category=CSharp'&gt;CSharp&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href='/Weblog/ShowPosts.aspx?Category= ADO.NET'&gt; ADO.NET&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;&lt;/div&gt;
&lt;div style='margin-top: 5px;'&gt;
&lt;a href="https://twitter.com/share" class="twitter-share-button" data-text="Creating a Dynamic DataReader for easier Property Access" data-via="RickStrahl" data-lang="en" data-hashtags="" data-url="http://www.west-wind.com/weblog/posts/2011/Dec/06/Creating-a-Dynamic-DataReader-for-easier-Property-Access"&gt;Tweet&lt;/a&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");&lt;/script&gt;

&lt;g:plusone size="medium" href="http://www.west-wind.com/weblog/posts/2011/Dec/06/Creating-a-Dynamic-DataReader-for-easier-Property-Access" "&gt;&lt;/g:plusone&gt;
&lt;script type="text/javascript"&gt;
  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();
&lt;/script&gt;

&lt;/div&gt;&lt;/small&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ldPMnHozrSM:YyKFloczjmk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ldPMnHozrSM:YyKFloczjmk:dnMXMwOfBR0"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=dnMXMwOfBR0" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ldPMnHozrSM:YyKFloczjmk:D7DqB2pKExk"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=ldPMnHozrSM:YyKFloczjmk:D7DqB2pKExk" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ldPMnHozrSM:YyKFloczjmk:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=ldPMnHozrSM:YyKFloczjmk:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ldPMnHozrSM:YyKFloczjmk:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?i=ldPMnHozrSM:YyKFloczjmk:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/RickStrahl?a=ldPMnHozrSM:YyKFloczjmk:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/RickStrahl?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/RickStrahl/~4/ldPMnHozrSM" height="1" width="1"/&gt;</description>
    <feedburner:origLink>http://www.west-wind.com/weblog/posts/2011/Dec/06/Creating-a-Dynamic-DataReader-for-easier-Property-Access</feedburner:origLink></item>
  </channel>
</rss>

