<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tales from the Evil Empire : .NET</title><link>http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx</link><description>Tags: .NET</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Fun with C# 4.0’s dynamic</title><link>http://weblogs.asp.net/bleroy/archive/2009/09/17/fun-with-c-4-0-s-dynamic.aspx</link><pubDate>Thu, 17 Sep 2009 07:50:29 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7209018</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7209018</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/09/17/fun-with-c-4-0-s-dynamic.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="right" src="http://weblogs.asp.net/blogs/bleroy/DSCN2236Touched_14096A29.jpg" width="244" height="184" /&gt;There’s been &lt;a href="http://haacked.com/archive/2009/08/26/method-missing-csharp-4.aspx"&gt;some&lt;/a&gt; &lt;a href="http://haacked.com/archive/2009/08/31/7-stages-of-language-keyword-grief.aspx"&gt;debate&lt;/a&gt; &lt;a href="http://blog.wekeroad.com/blog/magical-dynamic-mystery-tour/"&gt;recently&lt;/a&gt; on &lt;a href="http://msdn.microsoft.com/en-us/library/dd264736(VS.100).aspx"&gt;the new “dynamic” keyword in C# 4.0&lt;/a&gt;. As has been the case with many features before it, some love it, some hate it, some say it bloats the language, yadda yadda yadda. People said that about lambdas. Me, I’ll just use it where I see a use case, thank you very much.&lt;/p&gt;  &lt;p&gt;In the case of dynamic, another frequent comment is that a statically-typed language should not try to look like a dynamic language. Well, I just don’t believe in that distinction.&lt;/p&gt;  &lt;p&gt;Being dynamic is a &lt;em&gt;trait&lt;/em&gt; that a language can have, and some have it more than others. But as soon as a language has a dictionary type or indexers, and most modern languages do, it starts having dynamicity. What people call a dynamic language is just one where it’s the only available type.&lt;/p&gt;  &lt;p&gt;Now there is type safety of course, but to me that’s a different feature that is not as coupled with “statically-typed-ness” as we usually tend to think. Case in point, many modern JavaScript engines analyze the code in order to apply many of the optimization techniques that static languages have been applying at compile-time.&lt;/p&gt;  &lt;p&gt;So just how dynamic was C# before 4.0? Well, there are dictionaries and indexers, as I’ve said, but there are also other interesting and less known features such as &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.customtypedescriptor.aspx"&gt;custom type descriptors&lt;/a&gt;. Go &lt;a href="http://msdn.microsoft.com/en-us/library/ms171819.aspx"&gt;look them up&lt;/a&gt; if you don’t know about them. Type descriptors are for example used to provide a level of indirection between a design surface and the components being edited.&lt;/p&gt;  &lt;p&gt;But type descriptors can also be built completely dynamically and decide to expose a completely virtual object model that is entirely built at runtime. Of course, the client code for that object has to go through the right APIs to make use of that quasi-dynamic model. And of course those APIs are not simple. Implementing a custom type descriptor in itself is not exactly trivial.&lt;/p&gt;  &lt;p&gt;If anything, the dynamic keyword is going to provide a friendlier syntax for this sort of thing. One problem is that it has no relationship whatsoever with custom type descriptors.&lt;/p&gt;  &lt;p&gt;So I thought I’d try to bridge that gap. How can we take a type that exposes a custom type descriptor, such as &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.datarowview.aspx"&gt;DataRowView&lt;/a&gt;, and transform that into something that plays nice with the dynamic keyword? What we want to be able to write is something like the following:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;table = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataTable&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;People&amp;quot;&lt;/span&gt;);
table.Columns.Add(&lt;span style="color: #a31515"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;));
table.Columns.Add(&lt;span style="color: #a31515"&gt;&amp;quot;LastName&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;));
table.Columns.Add(&lt;span style="color: #a31515"&gt;&amp;quot;Children&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;int&lt;/span&gt;));
table.LoadDataRow(&lt;br /&gt;    &lt;span style="color: blue"&gt;new object&lt;/span&gt;[] {&lt;span style="color: #a31515"&gt;&amp;quot;Bertrand&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Le Roy&amp;quot;&lt;/span&gt;, 2},&lt;br /&gt;    &lt;span style="color: #2b91af"&gt;LoadOption&lt;/span&gt;.OverwriteChanges);
&lt;span style="color: blue"&gt;var &lt;/span&gt;tableView = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataView&lt;/span&gt;(table);&lt;br /&gt;
&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;dynRecord = &lt;br /&gt;    &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TypeDescriptorDynamicWrapper&lt;/span&gt;(tableView[0]);&lt;span style="color: #2b91af"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;{0} {1} has {2} children.&amp;quot;&lt;/span&gt;, 
    dynRecord.FirstName, 
    dynRecord.LastName, 
    dynRecord.Children);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;To do that, I built the TypeDescriptorDynamicWrapper that you see being used above. What is does is pretty simple, as it just inherits from DynamicObject and implements TryGetMember as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public override bool &lt;/span&gt;TryGetMember(&lt;br /&gt;    &lt;span style="color: #2b91af"&gt;GetMemberBinder &lt;/span&gt;binder, &lt;span style="color: blue"&gt;out object &lt;/span&gt;result) {&lt;br /&gt;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;name = binder.Name;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;property = _properties.Find(
        p =&amp;gt; p.Name.Equals(name,
            binder.IgnoreCase ?
            &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.OrdinalIgnoreCase :
            &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.Ordinal));
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(property == &lt;span style="color: blue"&gt;null&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;;
    }
    result = property.GetValue(&lt;br /&gt;        _descriptor.GetPropertyOwner(&lt;span style="color: blue"&gt;null&lt;/span&gt;));
    &lt;span style="color: blue"&gt;return true&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, the method just finds the right property on the type descriptor and delegates to it if it finds it. I didn’t implement the setting of properties but it would be fairly easy to do so.&lt;/p&gt;

&lt;p&gt;Using that simple wrapper, the program above just works and displays:&lt;/p&gt;

&lt;p style="background-color: black; font-family: courier; color: white"&gt;Bertrand Le Roy has 2 children.&lt;/p&gt;

&lt;p&gt;That was easy. Now what about the reverse, getting any dynamic object to expose a custom type descriptor?&lt;/p&gt;

&lt;p&gt;Well, that’s where things get tricky.&lt;/p&gt;

&lt;p&gt;The dynamic capabilities of C# 4.0 are targeted at two things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Providing syntactic sugar for getting to dynamic members that looks like accessing statically-defined ones.&lt;/li&gt;

  &lt;li&gt;An easy way to create dynamic types by implementing a form of method_missing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But there is one thing that exists in &lt;a href="http://dlr.codeplex.com/"&gt;DLR&lt;/a&gt; but didn’t make it to C# yet, which is the ability to easily reflect on dynamic objects (other than by using the #1 syntactic sugar above).&lt;/p&gt;

&lt;p&gt;For example, in JavaScript, a.foo and a[“foo”] are exactly the same thing, which enables easy reflection: you can enumerate the members of an arbitrary object and do stuff like a[someStringVariable].&lt;/p&gt;

&lt;p&gt;Doing the same thing with C# on dynamic objects is much more difficult. The equivalent of a.foo is easy, but not the equivalent of a[someStringVariable].&lt;/p&gt;

&lt;p&gt;Forget about using reflection, it doesn’t work on dynamic objects because &lt;em&gt;dynamic really is a compiler trick&lt;/em&gt;. There is no such thing as a “dynamic” type to reflect on. It’s a keyword that tells the compiler to relax type-checking and generate code to access those dynamic members at runtime. If you try to reflect on an instance of one of the dynamic-friendly type, such as ExpandoObject, you’ll see the statically-defined members of that type, but not the dynamic ones. In a way, you’re looking at the messenger instead of the message.&lt;/p&gt;

&lt;p&gt;This is a problem because in order to wrap an arbitrary dynamic object into a custom type descriptor, we need to be able to reflect on the dynamic members of that object.&lt;/p&gt;

&lt;p&gt;To solve the problem, and with some help from the nice folks who are building the DLR, I compiled a simple program that sets and gets a property on a dynamic object, an then I looked at the generated code from Reflector (“luckily”, Reflector is not yet aware of dynamic and shows the generated code instead of something closer to the actual code you wrote).&lt;/p&gt;

&lt;p&gt;I was then able to refactor that code and replace the string constants for the accessed member with a method parameter, thus creating a generic way to access dynamic object properties:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static object &lt;/span&gt;GetValue(&lt;br /&gt;    &lt;span style="color: blue"&gt;object &lt;/span&gt;dyn, &lt;span style="color: blue"&gt;string &lt;/span&gt;propName) {&lt;br /&gt;
    &lt;span style="color: green"&gt;// Warning: this is rather expensive,&lt;br /&gt;    // and should be cached in a real app
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;GetterSite = &lt;br /&gt;        &lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;&lt;br /&gt;        .Create(
          &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpGetMemberBinder&lt;/span&gt;(propName,
            dyn.GetType(),
            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;[] {
              &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;(&lt;br /&gt;                  &lt;span style="color: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/span&gt;.None, &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            }));
    &lt;span style="color: blue"&gt;return &lt;/span&gt;GetterSite.Target(GetterSite, dyn);
}

&lt;span style="color: blue"&gt;public static void &lt;/span&gt;SetValue(&lt;br /&gt;    &lt;span style="color: blue"&gt;object &lt;/span&gt;dyn, &lt;span style="color: blue"&gt;string &lt;/span&gt;propName, &lt;span style="color: blue"&gt;object &lt;/span&gt;val) {&lt;br /&gt;
    &lt;span style="color: green"&gt;// Warning: this is rather expensive,&lt;br /&gt;    // and should be cached in a real app
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;SetterSite = &lt;br /&gt;      &lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;CallSite&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;&lt;br /&gt;      .Create(
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpSetMemberBinder&lt;/span&gt;(propName,
          dyn.GetType(),
          &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;[] {
            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;(&lt;br /&gt;              &lt;span style="color: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/span&gt;.None, &lt;span style="color: blue"&gt;null&lt;/span&gt;),
            &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CSharpArgumentInfo&lt;/span&gt;(&lt;br /&gt;              &lt;span style="color: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/span&gt;.LiteralConstant |
              &lt;span style="color: #2b91af"&gt;CSharpArgumentInfoFlags&lt;/span&gt;.UseCompileTimeType,&lt;br /&gt;              &lt;span style="color: blue"&gt;null&lt;/span&gt;)
          }));
    SetterSite.Target(SetterSite, dyn, val);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Once we have that, implementing the custom type provider is relatively straightforward. The only part that is not immediately obvious is how to enumerate the properties of the dynamic object.&lt;/p&gt;

&lt;p&gt;This is done as follows. Dynamic objects implement &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.idynamicmetaobjectprovider(VS.100).aspx"&gt;IDynamicMetaObjetProvider&lt;/a&gt;, which has a &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.idynamicmetaobjectprovider.getmetaobject(VS.100).aspx"&gt;GetMetaObject&lt;/a&gt; method, which returns a &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicmetaobject(VS.100).aspx"&gt;DynamicMetaObject&lt;/a&gt; object on which you can call &lt;a href="http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicmetaobject.getdynamicmembernames(VS.100).aspx"&gt;GetDynamicMemberNames&lt;/a&gt; to get the list of members.&lt;/p&gt;

&lt;p&gt;We can now take any dynamic object and for example present it in a property grid, which is one of those components that know how to handle custom type descriptors but not the new dynamic objects:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;dynamic &lt;/span&gt;person = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ExpandoObject&lt;/span&gt;();
person.FirstName = &lt;span style="color: #a31515"&gt;&amp;quot;Bertrand&amp;quot;&lt;/span&gt;;
person.LastName = &lt;span style="color: #a31515"&gt;&amp;quot;Le Roy&amp;quot;&lt;/span&gt;;
person.Children = 2;
propertyGrid1.SelectedObject = &lt;br /&gt;    &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DynamicTypeDescriptorWrapper&lt;/span&gt;(person);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And here’s what the results look like:&lt;img style="border-bottom: 0px; border-left: 0px; margin: 10px auto; display: block; float: none; border-top: 0px; border-right: 0px" title="PropertyGrid displaying a dynamic object." border="0" alt="PropertyGrid displaying a dynamic object." src="http://weblogs.asp.net/blogs/bleroy/DynamicPropertyGrid_5E24750E.png" width="304" height="207" /&gt; &lt;/p&gt;

&lt;p&gt;In conclusion, I’d say that although we are having a healthy debate on the dynamic keyword (and we should have that debate), I’m confident that in one or two years, we’ll have some incredibly imaginative uses of the feature that will simply blow us away and will make the current conversation a little sillier than it seems today. The feature itself is also in its infancy and not only will the use cases emerge way beyond what even its designers envisioned, it will also grow and become more usable with future versions of the language, as is made clear by the gap in functionality that still exists today with the full DLR project.&lt;/p&gt;

&lt;p&gt;You can download the code for this article here:
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/Bleroy.Sample.Dynamic.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/Bleroy.Sample.Dynamic.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/Bleroy.Sample.Dynamic.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7209018" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Dynamic+languages/default.aspx">Dynamic languages</category></item><item><title>Hiring for new super-secret project</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/30/hiring-for-new-super-secret-project.aspx</link><pubDate>Thu, 30 Jul 2009 20:35:06 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7158262</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7158262</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/30/hiring-for-new-super-secret-project.aspx#comments</comments><description>&lt;p&gt;Well, I guess it’s not so super-secret anymore now but these last few months, I’ve been transitioning from ASP.NET Ajax to a new project that aims at helping ASP.NET communities build Open Source applications on ASP.NET. It’s a lot of fun and the good news is that you can join in. We are hiring a senior developer:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://careers.microsoft.com/JobDetails.aspx?ss=&amp;amp;pg=0&amp;amp;so=&amp;amp;rw=1&amp;amp;jid=4567&amp;amp;jlang=EN"&gt;https://careers.microsoft.com/JobDetails.aspx?ss=&amp;amp;pg=0&amp;amp;so=&amp;amp;rw=1&amp;amp;jid=4567&amp;amp;jlang=EN&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Please mention me (Bertrand Le Roy, bleroy at Microsoft) as the referral if you apply. ;)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7158262" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft/default.aspx">Microsoft</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Job/default.aspx">Job</category></item><item><title>Mocking indexer getters with Moq</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/16/mocking-indexer-getters-with-moq.aspx</link><pubDate>Thu, 16 Jul 2009 23:42:17 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7148938</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7148938</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/16/mocking-indexer-getters-with-moq.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy" border="0" alt="(c) Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN2687_61E2AA99.jpg" width="244" height="184" /&gt; This is a follow-up on that other post: &lt;a href="http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx"&gt;Mocking indexer setters with Moq&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;So thanks to that post, we now know how to intercept the setting of a particular indexed property (in our example, an application variable) and set a local variable with the value that was set by the tested code.&lt;/p&gt;  &lt;p&gt;Now if you want the application to return that same value when queried by the tested code, you also need to mock the indexer getter.&lt;/p&gt;  &lt;p&gt;This operation is also not entirely trivial. Here’s how you do it: you do a SetupGet chained with a Returns with a lambda expression as the parameter:&lt;/p&gt;  &lt;pre class="code"&gt;mockHttpContext&lt;br /&gt;    .SetupGet(c =&amp;gt; c.Application[&lt;span style="color: #2b91af"&gt;&amp;quot;foo&lt;span style="color: #2b91af"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;])
    .Returns(() =&amp;gt; (&lt;span style="color: blue"&gt;object&lt;/span&gt;)map);&lt;/pre&gt;

&lt;p&gt;That last point about using a lambda is pretty important. If you just use map as the parameter for Returns, Moq will hold on to a reference to whatever object map contained &lt;em&gt;at the time the call to Returns is made&lt;/em&gt;. This might very well be null, if you started with the code in our previous setter example. If you use a lambda on the other hand, Moq will not hold on to the value of map but to the expression that returns the value of map. The execution is deferred. Now every time the tested code exercises that path, Moq will evaluate the expression, which will return the &lt;em&gt;current&lt;/em&gt; value of map.&lt;/p&gt;

&lt;p&gt;Also of interest, whereas we had to explicitly have an index parameter for the callback lambda in the case of the setter, Returns needs no such thing, and this example actually has little code that is specific to indexed properties. The trick about the lambda in the Returns in particular applies just as well to mocking getters for non-indexed properties.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7148938" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MoQ/default.aspx">MoQ</category></item><item><title>A (less) simple include for ASP.NET</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/10/a-less-simple-include-for-asp-net.aspx</link><pubDate>Fri, 10 Jul 2009 22:57:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7145123</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7145123</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/10/a-less-simple-include-for-asp-net.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2003 Bertrand Le Roy" border="0" alt="(c) 2003 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN2140_787CAEA3.jpg" width="184" height="244" /&gt; In &lt;a href="http://weblogs.asp.net/bleroy/archive/2009/07/09/a-simple-include-for-asp-net.aspx"&gt;yesterday’s post&lt;/a&gt;, I published the code for a simple include method for ASP.NET that I’ve been using in a couple of places, only to realize that it was fine for what I was doing but probably not very useful beyond that.&lt;/p&gt;  &lt;p&gt;So I spent some time today broadening its scope. It now supports nested includes (I modified the original post to reflect that change) and also setting properties on the control.&lt;/p&gt;  &lt;p&gt;You can still do plain includes:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Include(&lt;span style="color: #a31515"&gt;&amp;quot;contents.ascx&amp;quot;&lt;/span&gt;); &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;But now you can also set properties on the included user control using an anonymous object:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Include(&lt;span style="color: #a31515"&gt;&amp;quot;contents.ascx&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;new &lt;/span&gt;{number=2, what=&lt;span style="color: #a31515"&gt;&amp;quot;A&amp;quot;&lt;/span&gt;}); &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The code for the helper considerably grew along the way but it’s still reasonable:&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.Linq;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Reflection;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Web.UI;

&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IncludeHelper &lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static void &lt;/span&gt;Include(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TemplateControl &lt;/span&gt;host,&lt;br /&gt;                               &lt;span style="color: blue"&gt;string &lt;/span&gt;virtualPath) {
        Include(host, virtualPath, &lt;span style="color: blue"&gt;null&lt;/span&gt;);
    }

    &lt;span style="color: blue"&gt;public static void &lt;/span&gt;Include(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TemplateControl &lt;/span&gt;host,&lt;br /&gt;                               &lt;span style="color: blue"&gt;string &lt;/span&gt;virtualPath,&lt;br /&gt;                               &lt;span style="color: blue"&gt;object &lt;/span&gt;properties) {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;resolvedPath = host.ResolveUrl(virtualPath);
        &lt;span style="color: blue"&gt;var &lt;/span&gt;include = host.LoadControl(resolvedPath);
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(properties != &lt;span style="color: blue"&gt;null&lt;/span&gt;) {
            &lt;span style="color: blue"&gt;var &lt;/span&gt;includeType = include.GetType();
            &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;fieldInfo &lt;span style="color: blue"&gt;in&lt;br /&gt;                         &lt;/span&gt;properties.GetType().GetProperties()) {
                &lt;span style="color: blue"&gt;var &lt;/span&gt;targetInfo = includeType.GetMember(&lt;br /&gt;                    fieldInfo.Name,
                    &lt;span style="color: #2b91af"&gt;MemberTypes&lt;/span&gt;.Field |&lt;br /&gt;                    &lt;span style="color: #2b91af"&gt;MemberTypes&lt;/span&gt;.Property,
                    &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.IgnoreCase |&lt;br /&gt;                    &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Instance |&lt;br /&gt;                    &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Public)
                    .FirstOrDefault();
                &lt;span style="color: blue"&gt;var &lt;/span&gt;targetPropertyInfo = targetInfo &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt;;
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(targetPropertyInfo != &lt;span style="color: blue"&gt;null&lt;/span&gt;) {
                    targetPropertyInfo.SetValue(&lt;br /&gt;                        include,&lt;br /&gt;                        fieldInfo.GetValue(properties, &lt;span style="color: blue"&gt;null&lt;/span&gt;),&lt;br /&gt;                        &lt;span style="color: blue"&gt;null&lt;/span&gt;);
                }
                &lt;span style="color: blue"&gt;else &lt;/span&gt;{
                    &lt;span style="color: blue"&gt;var &lt;/span&gt;targetFieldInfo = targetInfo &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FieldInfo&lt;/span&gt;;
                    &lt;span style="color: blue"&gt;if &lt;/span&gt;(targetFieldInfo != &lt;span style="color: blue"&gt;null&lt;/span&gt;) {
                        targetFieldInfo.SetValue(&lt;br /&gt;                            include,&lt;br /&gt;                            fieldInfo.GetValue(properties, &lt;span style="color: blue"&gt;null&lt;/span&gt;));
                    }
                    &lt;span style="color: blue"&gt;else &lt;/span&gt;{
                        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentException&lt;/span&gt;(
                            &lt;span style="color: #a31515"&gt;&amp;quot;There is no property of field with &amp;quot; +&lt;br /&gt;                            &amp;quot;that name on the target control.&amp;quot;&lt;/span&gt;,
                            fieldInfo.Name);
                    }
                }
            }
        }
        &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;writer =&lt;br /&gt;                   &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HtmlTextWriter&lt;/span&gt;(host.Page.Response.Output)) {
            include.RenderControl(writer);
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I hope this helps. Again, if you are OK with the regular syntax to include a user controls, you probably shouldn’t use this and stick to what you know. I built this because I’m not entirely satisfied with that syntax and because I have a clear use case where it makes more sense. Make your own opinion and follow it :)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; The control is still not going into the control tree, which breaks lots of stuff. I don't really care that much about it but it may be important to you so I thought I'd point it out.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7145123" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>A simple include for ASP.NET</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/09/a-simple-include-for-asp-net.aspx</link><pubDate>Thu, 09 Jul 2009 23:30:55 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7144049</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7144049</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/09/a-simple-include-for-asp-net.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="(c) 2005 Bertrand Le Roy" border="0" alt="(c) 2005 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/IMG_03402_64755BBB.jpg" width="164" height="244" /&gt; In &lt;a href="http://weblogs.asp.net/bleroy/archive/2009/07/08/are-master-pages-too-complex.aspx"&gt;yesterday’s post&lt;/a&gt;, I alluded to a simple include extension method that I like to use when I don’t care about designer support. In a comment, Andrew asked if I could share the code for it, so here it is.&lt;/p&gt;  &lt;p&gt;I never liked the regular way of including user controls in WebForms and how they require a registration &lt;strong&gt;and&lt;/strong&gt; a declaration, both of which are more verbose than they need to be. A plain #include would work but is a little outdated (and if I’m not mistaken it’s not even available by default in IIS7).&lt;/p&gt;  &lt;p&gt;My Include method is quite similar to MVC’s RenderPartial except that it doesn’t deal with view data. It’s a plain and simple include. Just give it the relative path to a user control:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Include(&lt;span style="color: #a31515"&gt;&amp;quot;Header.ascx&amp;quot;&lt;/span&gt;); &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here’s the code for that extension method:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Web;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Web.UI;

&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IncludeHelper &lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static void &lt;/span&gt;Include(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TemplateControl &lt;/span&gt;host,&lt;br /&gt;                               &lt;span style="color: blue"&gt;string &lt;/span&gt;virtualPath) {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;resolvedPath = host.ResolveUrl(virtualPath);
        &lt;span style="color: blue"&gt;var &lt;/span&gt;include = host.LoadControl(resolvedPath);
        &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;writer =&lt;br /&gt;                   &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HtmlTextWriter&lt;/span&gt;(host.Page.Response.Output)) {
            include.RenderControl(writer);
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;It just resolves the path, loads the control and renders it in place.&lt;/p&gt;

&lt;p&gt;To use that method, just drop the code as a cs file into App_code if you’re using a web site, or just add it to your project otherwise.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/bleroy/archive/2009/07/10/a-less-simple-include-for-asp-net.aspx"&gt;I played with the idea of enabling an anonymous object parameter to set properties on the user control&lt;/a&gt;, but I guess if you’re going to do that, the benefits of the helper get dimmer and going with the regular WebForms user control registration and tags is probably just easier (you’ll get IntelliSense on the properties for example).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer/update: &lt;/strong&gt;No, this does not run the control through the page lifecycle (although it could by adding it to the page’s control tree and removing it after rendering it). No, if you use master pages you probably don’t need it. No, you can’t set properties.&lt;/p&gt;

&lt;p&gt;This is just the simplest include I could build, and it does only that: including contents. If it seems useless to you, it probably is. I published it mainly because I was asked to. :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; I wrote a more complex version that can set properties on the included controls:

  &lt;br /&gt;&lt;a href="http://weblogs.asp.net/bleroy/archive/2009/07/10/a-less-simple-include-for-asp-net.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/07/10/a-less-simple-include-for-asp-net.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7144049" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Are Master Pages too complex?</title><link>http://weblogs.asp.net/bleroy/archive/2009/07/08/are-master-pages-too-complex.aspx</link><pubDate>Wed, 08 Jul 2009 23:38:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7143759</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7143759</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/07/08/are-master-pages-too-complex.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="MasterLock" border="0" alt="MasterLock" align="left" src="http://weblogs.asp.net/blogs/bleroy/MasterLock_thumb_07ECEFF7.jpg" width="198" height="210" /&gt;Master Pages are a wonderful concept that as developers we highly value. It’s the sort of pattern that just looks like the right thing to do (to our twisted, concept hungry developer minds) and that even makes you wonder why we haven’t done it that way since the beginning of time (&lt;a href="http://en.wikipedia.org/wiki/World_Wide_Web"&gt;1990&lt;/a&gt;). For the record, master pages were invented by &lt;a href="http://blogs.msdn.com/davidebb/default.aspx"&gt;David Ebbo&lt;/a&gt;, who is behind a lot of the smartest things in ASP.NET.&lt;/p&gt;  &lt;p&gt;Just in case you have no idea, what are Master Pages? Before master pages, sharing layout between pages was done using includes (or user controls). For example, your typical hello world page might have looked like this:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;C#&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;
&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Register &lt;/span&gt;&lt;span style="color: red"&gt;TagPrefix&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;include&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;TagName&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;header&amp;quot;&lt;br /&gt;             &lt;/span&gt;&lt;span style="color: red"&gt;Src&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;~/HelloHeader.ascx&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;
&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Register &lt;/span&gt;&lt;span style="color: red"&gt;TagPrefix&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;include&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;TagName&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;footer&amp;quot;&lt;br /&gt;             &lt;/span&gt;&lt;span style="color: red"&gt;Src&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;~/HelloFooter.ascx&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: #a31515"&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: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Hello world with includes&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;span style="color: blue"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;include&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;header &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot; /&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;/span&gt;Hello world!
&lt;span style="color: blue"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;include&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;footer &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot; /&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice that the page does contain markup that is not specific to the page (boilerplate markup mainly that you could put into includes as well, but only at the cost of breaking beginnings and ends of semantic blocks into separate includes).&lt;/p&gt;

&lt;p&gt;As a side note, I’ve always thought the user control syntax above was overly verbose when all you want is a plain include. It has the one advantage of enabling the Visual Studio designer but having a separate registration and declaration seems overkill. The #include directive still exists but I personally prefer to use a simple helper extension method to Page that brings the code down to this (MVC gives you Html.RenderPartial, which is roughly equivalent):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;C#&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: #a31515"&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: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Hello world with includes&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;meta &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;keywords&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;hello world&amp;quot; /&amp;gt;&lt;/span&gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Include(&lt;span style="color: #a31515"&gt;&amp;quot;HelloHeader.ascx&amp;quot;&lt;/span&gt;); &lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;    Hello world!&lt;br /&gt;    &lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Include(&lt;span style="color: #a31515"&gt;&amp;quot;HelloFooter.ascx&amp;quot;&lt;/span&gt;); &lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;With master pages, you can get rid of all common markup and really limit what you put into the page to just what varies from page to page. for example, the above hello world page might be written like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;C#&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;MasterPageFile&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;~/MasterPage.master&amp;quot;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: red"&gt;Title&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Hello world!&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;

&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Content &lt;/span&gt;&lt;span style="color: red"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;head&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Server&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;meta &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;keywords&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;content&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;hello world&amp;quot; /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Content &lt;/span&gt;&lt;span style="color: red"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;content&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Server&amp;quot;&amp;gt;
    &lt;/span&gt;Hello from the content page!
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Content&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;That is clean, but pretty puzzling the first time you see it. To understand where the markup comes from, you need to track that @Page directive and understand that you need to look into the corresponding master page file:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Master &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;C#&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: #a31515"&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: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Master page&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ContentPlaceHolder &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;head&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ContentPlaceHolder&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;h1&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Hello from a master page&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;h1&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ContentPlaceHolder &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;content&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;ContentPlaceHolder&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;Footer says hi.
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You need to understand that those content place holder ids correspond to other controls that are in the master page and that the framework will make the match.&lt;/p&gt;

&lt;p&gt;Master pages also come with the price of mangled ids, but that’s an implementation detail (one could imagine an implementation that wouldn’t suffer from that problem). The price we pay for master pages also comes in the form of a weird control tree: master pages are really implemented as user controls that get included by the page, a concept that is the &lt;em&gt;inverse&lt;/em&gt; of the model they seem to promote; in other words, the implementation is the reverse of the concept, in a way. There are also complications about putting contents in the head section (head must be runat=server, script tags are tricky, setting the title, etc.).&lt;/p&gt;

&lt;p&gt;So are master pages worth the price? It depends on who your audience is and on how your pages are built.&lt;/p&gt;

&lt;p&gt;By audience, I mean the person who is going to write the views. If it’s just you and you are a developer, they might actually be a pretty solid choice. But if external designers are going to build the views, maybe you need to pause and try not to think as a developer for a moment.&lt;/p&gt;

&lt;p&gt;The main problem with the include approach (and the reason why master pages were invented) is that the outer markup for the page needs to be on all pages. So if you decide to change that markup, you need to do so on all pages.&lt;/p&gt;

&lt;p&gt;But in more and more applications, in particular CMS, this problem becomes moot. If your application decouples the content from the view code, and if the layout or view to use for a given content can be determined at runtime by a themeing engine, you might end-up with templates that look like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;C#&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: #a31515"&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: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;= &lt;/span&gt;Title &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.RenderZone(&lt;span style="color: #a31515"&gt;&amp;quot;Header&amp;quot;&lt;/span&gt;); &lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;    &lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.RenderZone(&lt;span style="color: #a31515"&gt;&amp;quot;Contents&amp;quot;&lt;/span&gt;); &lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;    &lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.RenderZone(&lt;span style="color: #a31515"&gt;&amp;quot;Footer&amp;quot;&lt;/span&gt;); &lt;span style="background: #ffee62"&gt;%&amp;gt;
&lt;/span&gt;    &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This is actually no different in concept from master pages (there are place holders in generic markup and no specific contents) but the contents do not come from a content page. Instead, they are dynamically inserted by the application.&lt;/p&gt;

&lt;p&gt;The template file then becomes no more than layout. It is easy to understand and easier to assign a different layout to any given contents.&lt;/p&gt;

&lt;p&gt;So in general there is no perfect answer on whether you should use master pages or not, but if you are able, in your application, to decouple page layout from contents, there is an opportunity to have clean and easy to understand markup that also maximizes re-use.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7143759" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/CMS/default.aspx">CMS</category></item><item><title>You can do the TODOs today too!</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/16/you-can-do-the-todos-today-too.aspx</link><pubDate>Tue, 16 Jun 2009 23:08:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7127059</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>17</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7127059</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/16/you-can-do-the-todos-today-too.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Don&amp;#39;t just leave your junk lying around. (c) Bertrand Le Roy 2002" border="0" alt="Don&amp;#39;t just leave your junk lying around. (c) Bertrand Le Roy 2002" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN0304_249D594C.jpg" width="244" height="184" /&gt; If you’re anything like me, you probably litter your code with TODO comments, postponing random tasks for the sake of moving the project forward. And there is of course a non-zero probability that you are going to ship with those comments still in.&lt;/p&gt;  &lt;p&gt;So I want to propose the following call to action to all readers of this blog: &lt;strong&gt;open your current project and start implementing what’s in those TODOs today&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Finding the TODOs is easy. In Visual Studio, do CTRL+SHIFT+F (search in files), make sure you are searching across the whole solution and search for “TODO”. In the results window, you can click on each result to open the file at the TODO comment location.&lt;/p&gt;  &lt;p&gt;While working on a file, you can open the task list window (CTRL+\, CTRL+T), open that drop-down menu and choose “Comments”:   &lt;br /&gt;    &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="TaskList Comments" border="0" alt="TaskList Comments" src="http://weblogs.asp.net/blogs/bleroy/TaskListComments_37140328.png" width="292" height="92" /&gt; &lt;/p&gt;  &lt;p&gt;Once you’ve done that, Visual Studio brings you a list of all those TODO comments in the currently open files, which is easier on the eyes than the search results window:   &lt;br /&gt;    &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Task List" border="0" alt="Task List" src="http://weblogs.asp.net/blogs/bleroy/TaskList_667418CD.png" width="521" height="246" /&gt; &lt;/p&gt;  &lt;p&gt;You can now go through those one by one (double-clicking on the TODO in the task list takes you there) and delete the comments once you’ve implemented the feature they describe.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; some commenters pointed out that the todo list is limited in scope. I updated the post. Resharper also apparently has a greater feature around that.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7127059" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Mocking indexer setters with Moq</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx</link><pubDate>Mon, 15 Jun 2009 22:27:52 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7124027</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7124027</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/15/mocking-indexer-setters-with-moq.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2004" border="0" alt="(c) Bertrand Le Roy 2004" align="left" src="http://weblogs.asp.net/blogs/bleroy/DSCN2906_5A7A1A17.jpg" width="244" height="184" /&gt; I quite like &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt; because it makes sense for me. Shamefully, I’ve always had some trouble understanding test code that was using mocks built with other frameworks. With &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt;, I can just grok it for some reason. It’s just super-clear to me. It doesn’t mean I have any idea how it really works but for now I’m just happy with the magic.&lt;/p&gt;  &lt;p&gt;Anyway, yesterday I wanted to check that a controller action was setting some Application variable (let’s not get into the debate on whether or not it should do that at all). Something like this:&lt;/p&gt;  &lt;pre class="code"&gt;application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now how do I enable the object to be set by the tested code? Well, that one is easy, I can use &lt;a href="http://www.clariusconsulting.net/labs/moq/html/87C20A5E.htm"&gt;SetupSet&lt;/a&gt; on indexers just fine:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mockHttpContext = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();
mockHttpContext.SetupSet(&lt;br /&gt;    c =&amp;gt; c.Application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;());&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This call tells the mock that it can accept to run code that attempts to set the “Foo” application variable with any object.&lt;/p&gt;

&lt;p&gt;But now, what if I want to get a reference to that object from my test code in order to check the value of some of its properties?&lt;/p&gt;

&lt;p&gt;Well, &lt;a href="http://code.google.com/p/moq/"&gt;MoQ&lt;/a&gt; has a Callback method that you can hook to the result of any Setup call. The action that you provide it will be run whenever the setup code is called. The problem with that callback method is that its signature must match that of the setter exactly. Unfortunately, that signature is implicit. If you get it wrong, the test will fail more or less silently (it will just tell you setup failed with little details). To get this right, you need to know what the setter syntactic sugar compiles to, which kinda sucks, but the good news is that you only have to figure it out once, which I just spent some time doing for you (and for myself too, let’s be honest):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;mockHttpContext = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;HttpContextBase&lt;/span&gt;&amp;gt;();
&lt;span style="color: #2b91af"&gt;Foo &lt;/span&gt;map = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;br /&gt;mockHttpContext&lt;br /&gt;    .SetupSet(c =&amp;gt; c.Application[&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;] = &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;.IsAny&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;())
    .Callback((&lt;span style="color: blue"&gt;string &lt;/span&gt;name, &lt;span style="color: blue"&gt;object &lt;/span&gt;m) =&amp;gt; { map = (&lt;span style="color: #2b91af"&gt;Foo&lt;/span&gt;)m; });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Because this is an indexer setter, the compiled code actually takes a name and a value, which is reflected by the signature of the callback lambda. We can now call into the code to test, knowing that when it sets our “Foo” application variable, the local “map” variable of the test code will get set. The test code can then party on the object and assert whatever it wants.&lt;/p&gt;

&lt;p&gt;I hope this saves some time whoever is trying to do the same thing.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7124027" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MoQ/default.aspx">MoQ</category></item><item><title>Survey: Ajax usage among .NET developers</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/22/survey-ajax-usage-among-net-developers.aspx</link><pubDate>Fri, 22 May 2009 19:13:20 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7094884</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7094884</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/22/survey-ajax-usage-among-net-developers.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/Copenhagen_36DBEC47.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="left" src="http://weblogs.asp.net/blogs/bleroy/Copenhagen_thumb_55B26025.jpg" width="204" height="154" /&gt;&lt;/a&gt; If you haven’t already and you are a .NET developer, please take a couple minutes and answer this survey, whether you use Ajax or not. There are a number of Ajax surveys around, but Simone’s is the only one that focuses on .NET developers.&lt;/p&gt;  &lt;p&gt;The survey:   &lt;br /&gt;&lt;a title="http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H" href="http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H"&gt;http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Simone’s post:   &lt;br /&gt;&lt;a href="http://codeclimber.net.nz/archive/2009/05/21/ajax-usage-among-.net-developers-in-2009.aspx"&gt;http://codeclimber.net.nz/archive/2009/05/21/ajax-usage-among-.net-developers-in-2009.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7094884" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>New release of the Ajax Control Toolkit</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx</link><pubDate>Thu, 14 May 2009 05:19:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7086151</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>50</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7086151</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx#comments</comments><description>&lt;p&gt;A &lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326"&gt;new version of the AJAX Control Toolkit&lt;/a&gt; is now available for download from the CodePlex website. It contains three new controls:&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="HtmlEditor" border="0" alt="HtmlEditor" align="left" src="http://weblogs.asp.net/blogs/bleroy/HtmlEditor_1E6A1BAC.png" width="244" height="159" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/HTMLEditor/HTMLEditor.aspx"&gt;HTMLEditor&lt;/a&gt;&lt;/strong&gt; - allows you to easily create and edit HTML content. You can edit in WYSIWYG mode or in HTML source mode. The control exists as a server-side extender but can also be instantiated purely on the client-side with a single line of code. Many thanks to &lt;a href="http://www.obout.com"&gt;Obout&lt;/a&gt; for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="ComboBox" border="0" alt="ComboBox" align="right" src="http://weblogs.asp.net/blogs/bleroy/ComboBox_0834005A.png" width="162" height="244" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx"&gt;ComboBox&lt;/a&gt;&lt;/strong&gt; - provides a DropDownList of items, combined with TextBox. Different modes determine the interplay between the text entry and the list of items. this control behaves very much like a Windows combo. Many thanks to Dan Ludwig for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ColorPicker" border="0" alt="ColorPicker" align="left" src="http://weblogs.asp.net/blogs/bleroy/ColorPicker_23002666.png" width="238" height="189" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ColorPicker/ColorPicker.aspx"&gt;ColorPicker&lt;/a&gt;&lt;/strong&gt; - can be attached to any ASP.NET TextBox control to provide client-side color-picking functionality from a popup. Many thanks to Alexander Turlov for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;The ASP.NET website has been updated with new &lt;a href="http://www.asp.net/learn/ajax-videos"&gt;videos&lt;/a&gt; and &lt;a href="http://www.asp.net/learn/ajax"&gt;tutorials&lt;/a&gt; for these controls.&lt;/p&gt;  &lt;p&gt;This new release also includes fixes for over 20 of the most voted bugs in existing AJAX Control Toolkit controls and now features minimized release versions of the script files.&lt;/p&gt;  &lt;p&gt;The release can be downloaded as a server dll or as a set of files for use with pure client-side applications.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326"&gt;http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7086151" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category></item><item><title>Quantum computing done right</title><link>http://weblogs.asp.net/bleroy/archive/2009/04/01/quantum-computing-done-right.aspx</link><pubDate>Wed, 01 Apr 2009 23:44:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7021720</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7021720</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/04/01/quantum-computing-done-right.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2004 Bertrand Le Roy" border="0" alt="(c) 2004 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/IMG_1561_43B6A0BD.jpg" width="244" height="164" /&gt; I know as a good microsoftee I should be supportive of what my employer does no matter what it is, and I might get fired for this post, but &lt;a href="http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx"&gt;Eilon’s latest article&lt;/a&gt; is wrong on so many levels I have to step up with whatever integrity I have left and fix this mess.&lt;/p&gt;  &lt;p&gt;In his post, he exposes the new ShrödingOr&amp;lt;TDead, TAlive&amp;gt; type that will be introduced in .NET 5.0 as part of the System.QuantumEntanglement namespace. Well, let’s face it, the current implementation has nothing quantum about it.&lt;/p&gt;  &lt;p&gt;Here’s how I would have written it:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;System.QuantumEntanglement {
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SchrödingOr&lt;/span&gt;&amp;lt;TDead, TAlive&amp;gt; {
        &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex &lt;/span&gt;_howDead;
        &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex &lt;/span&gt;_howAlive;

        &lt;span style="color: blue"&gt;public &lt;/span&gt;SchrödingOr(&lt;span style="color: #2b91af"&gt;Complex &lt;/span&gt;howDead, &lt;span style="color: #2b91af"&gt;Complex &lt;/span&gt;howAlive) {
            _howDead = howDead;
            _howAlive = howAlive;
        }

        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;Measure() {
            &lt;span style="color: blue"&gt;double &lt;/span&gt;howAliveSquareModulus =
                _howAlive.Real * _howAlive.Real +
                _howAlive.Imaginary * _howAlive.Imaginary;
            &lt;span style="color: blue"&gt;double &lt;/span&gt;howDeadSquareModulus =
                _howDead.Real * _howDead.Real +
                _howDead.Imaginary * _howDead.Imaginary;
            &lt;span style="color: blue"&gt;double &lt;/span&gt;probabilityOfBeingAlive = howAliveSquareModulus /
                (howAliveSquareModulus + howDeadSquareModulus);

            &lt;span style="color: blue"&gt;if &lt;/span&gt;((&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Random&lt;/span&gt;()).NextDouble() &amp;lt; probabilityOfBeingAlive) {
                _howAlive = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(1, 0);
                _howDead = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(0, 0);
                &lt;span style="color: blue"&gt;return typeof&lt;/span&gt;(TAlive);
            } &lt;span style="color: blue"&gt;else &lt;/span&gt;{
                _howAlive = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(0, 0);
                _howDead = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(1, 0);
                &lt;span style="color: blue"&gt;return typeof&lt;/span&gt;(TDead);
            }
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This way, the state really is a (complex) linear combination of the dead and alive types, which are the eigenstates of the system. Once you’ve created the state, you can never get it back unless you do a measurement. When you do that, the object collapses its state to one of the eigenstates based on probabilities determined by the actual quantum state of the system.&lt;/p&gt;

&lt;p&gt;The first measurement is random but once you’ve measured it, the cat remains alive or dead forever (the entanglement is destroyed by the measure).&lt;/p&gt;

&lt;p&gt;Here’s a little console app that creates a cat, puts it in the box and then opens the box:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args) {
    &lt;span style="color: green"&gt;// put the cat in the box
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;cat = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SchrödingOr&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DeadCat&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;LiveCat&lt;/span&gt;&amp;gt;(
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(1, 0), &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(1, 0));
    &lt;span style="color: green"&gt;// Open the box
    &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.Write(cat.Measure() == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DeadCat&lt;/span&gt;) ?
        &lt;span style="color: #a31515"&gt;&amp;quot;Cat is dead.&amp;quot; &lt;/span&gt;: &lt;span style="color: #a31515"&gt;&amp;quot;Cat is alive&amp;quot;&lt;/span&gt;);
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadKey();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This, I believe, is a much more realistic and useful implementation of SchrödingOr.&lt;/p&gt;

&lt;p&gt;The code: &lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/Quantum.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/Quantum.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/Quantum.zip&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Eilon’s original article: &lt;a title="http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx" href="http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx"&gt;http://weblogs.asp.net/leftslipper/archive/2009/04/01/the-string-or-the-cat-a-new-net-framework-library.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7021720" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Science/default.aspx">Science</category></item><item><title>Some ASP.NET compiler black magic</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/30/some-asp-net-compiler-black-magic.aspx</link><pubDate>Mon, 30 Mar 2009 23:48:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7013147</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7013147</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/30/some-asp-net-compiler-black-magic.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="default" border="0" alt="(c) 2005 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/default_02BD11AD.jpg" width="244" height="164" /&gt; In the work we’ve been doing with Rob on the &lt;a href="http://blog.wekeroad.com/kona/kona-1/"&gt;Kona commerce app&lt;/a&gt;, our quest for extreme pluggability has led us to look at quite a few interesting features of ASP.NET compilation. Features I didn’t know about before &lt;a href="http://blogs.msdn.com/dmitryr/"&gt;Dmitry&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/davidebb/"&gt;David&lt;/a&gt; pointed them out for me. I thought I’d share…&lt;/p&gt;  &lt;p&gt;It starts with the &lt;a href="http://msdn.microsoft.com/en-us/library/d864zc1k(VS.80).aspx"&gt;&amp;lt;%@ Assembly src= %&amp;gt;&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/w70c655a(VS.80).aspx"&gt;&amp;lt;%@ Reference virtualpath= %&amp;gt;&lt;/a&gt; directives which you may have seen show up in IntelliSense when building a page. But what are they doing exactly and what differentiates them?&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="Works in Medium Trust" border="0" alt="Works in Medium Trust" align="right" src="http://weblogs.asp.net/blogs/bleroy/WorksInMediumTrust_41AE9248.gif" width="103" height="104" /&gt; &lt;/p&gt;  &lt;p&gt;They both enable you to reference code that is in a different file in the site. With both of them, you get full IntelliSense on the referenced code, but they don’t reference the same kinds of files. @Reference is meant to reference a specific class whereas @Assembly brings in an arbitrary code file. As a consequence, @Reference needs a file where there is a well-defined default class, such as a Page or a User Control. @Assembly on the other hand will enable you to reference an arbitrary code file with as many classes as you wish and get the compiler to dynamically build a neat assembly out of it. The difference really amounts to whether or not the build provider implements &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildprovider.getgeneratedtype.aspx"&gt;GetGeneratedType&lt;/a&gt;, which the aspx and ascx build provider implements, and which generic code file providers typically don’t. In other words, if you built your own &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildprovider.aspx"&gt;Build Provider&lt;/a&gt; and implemented that method, the files it compiles could be referenced using Reference. Without it, ASP.NET wouldn’t know which type to pick as the referenced one. But @Assembly will still work.&lt;/p&gt;  &lt;p&gt;Building an assembly from a virtual path was really important for us because it enables a file anywhere in the web site to be dynamically compiled and used, which is exactly how we wanted plug-ins to work: we could have put the plug-ins into App_code (which does dynamic compilation automatically) but the name is not exactly intuitive, and it limits your ability to mix languages within the same folder. Please note that there &lt;strong&gt;*is*&lt;/strong&gt; a way to mix languages in App_code:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.web&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;compilation &lt;/span&gt;&lt;span style="color: red"&gt;debug&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;codeSubDirectories&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;directoryName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;cs&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;add &lt;/span&gt;&lt;span style="color: red"&gt;directoryName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;js&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;codeSubDirectories&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;compilation&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.web&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This config setting instructs the compiler to compile the cs and js folders in App_code into separate assemblies, which is all we need to allow for multiple languages. But we can do better than that.&lt;/p&gt;

&lt;p&gt;It so happens that the compiler feature that enables the Assembly directive is also available as a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.getcompiledassembly.aspx"&gt;public API&lt;/a&gt; (that works in Medium Trust):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;BuildManager&lt;/span&gt;.GetCompiledAssembly&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Seriously, this is now officially &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.getcompiledassembly.aspx"&gt;my favorite API in the whole .NET framework&lt;/a&gt;. This quite remarkable API takes a code file within the site and compiles it into an assembly (if that hasn’t already been done by a previous call to that API or by an @Assembly directive). What you get back from it is an Assembly object, which you can reflect on (using public reflection, which works in Medium Trust) and use any way you want.&lt;/p&gt;

&lt;p&gt;This will enable us to dynamically compile the files in a Plugins top-level directory of the app. Doing so, we are getting multi-language support without config settings or special folders, nicer folder name and bonus points for not shutting down the app domain every effing time any file is touched. Bye bye App_code!&lt;/p&gt;

&lt;p&gt;Well, of course that’s now one assembly per code file, which could be a problem if you have 800 plug-ins in your app, but then again if that’s the case maybe it’s time you moved all those into a nice pre-compiled assembly. Or do some clean-up. Anyway, this will work just fine for the type of scale we have in mind.&lt;/p&gt;

&lt;p&gt;A question you may ask at this point is what exactly happens when one of those files is modified. Well, the BuildManager will generate a new assembly and “forget” about the old one. And when I say forget, I don’t mean it’s getting unloaded, just that it won’t get used anymore (unless you have code that held on to a reference). That means that potentially, there could be some assembly rot after a while if your files change often. To mitigate that, BuildManager has a set of rules that it uses to determine that it needs to restart the app domain after a while. Just like App_code, just a lot less often. Basically, the rules are pretty much the same as for aspx or ascx files.&lt;/p&gt;

&lt;p&gt;All right, so this is all quite useful (I know I’m going to use that stuff a lot, at least). How about a useless hack now? (if you don’t like a fun hack, feel free to skip the rest of this post)&lt;/p&gt;

&lt;p&gt;So think about all the neat stuff we could do by combining this with Virtual Path Providers... Except that in ASP.NET up to and including 3.5 SP1, &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx"&gt;Virtual Path Providers&lt;/a&gt; don’t work in Medium Trust. Neither do &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildprovider.aspx"&gt;Build Providers&lt;/a&gt;, which would also be quite neat to play with. Bummer.&lt;/p&gt;

&lt;p&gt;But wait, people in the team thought about that and wondered what harm exactly you could do with VPPs and BPs that you couldn’t already do by simply writing code and well, the answer is pretty much nothing. So the good news is that VPPs and BPs will work in Medium Trust in ASP.NET 4.0. Hurray!&lt;/p&gt;

&lt;p&gt;So just for the sake of it, here’s the real black magic part of this post. Please note that there is more than one better way to do what I’m about to do and pretty much all of them would be simpler. I’m just hacking here and it’s going to be relatively convoluted.&lt;/p&gt;

&lt;p&gt;And as &lt;a href="http://en.wikipedia.org/wiki/Adam_Savage"&gt;Adam Savage&lt;/a&gt; says, “don’t try any of what you’re about to see at home. Ever!”. Seriously, this is dangerous code that has too many holes to count and that I wouldn’t run on anything but Visual Studio’s built-in web server (which is limited to requests from the same machine). Again, just hacking for fun here.&lt;/p&gt;

&lt;p&gt;The thing I’ve done is build a VPP that takes an operation embedded in the file name and builds a function that executes that operation. So for example, if you ask it for “A+B.cs”, it will get you a cs file that contains a class that has a method that takes two arguments and returns their sum:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;Dynamic {
    &lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Calculator &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public static double &lt;/span&gt;Operation(&lt;span style="color: blue"&gt;double &lt;/span&gt;A, &lt;span style="color: blue"&gt;double &lt;/span&gt;B) {
            &lt;span style="color: blue"&gt;return &lt;/span&gt;A + B;
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Mix that with the amazing &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.getcompiledassembly.aspx"&gt;BuildManager.GetCompiledAssembly&lt;/a&gt; and we have dynamic compilation of a user-specified function. Which is the scariest part of course, but still fun, eh?&lt;/p&gt;

&lt;p&gt;The usual disclaimer to use this at your own risk applies… I also didn’t spend too much time encoding the file name in the VPP thingy so some operations won’t go through too well. Multiplications for example. But eh, &lt;a href="http://www.drhorrible.com/"&gt;even the brightest minds don’t get everything right the first time&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/VppTest.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/VppTest.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/VppTest.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7013147" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Some MIX talks</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/22/some-mix-talks.aspx</link><pubDate>Mon, 23 Mar 2009 04:14:22 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6994071</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6994071</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/22/some-mix-talks.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 15px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2005 Bertrand Le Roy" border="0" alt="(c) 2005 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/bleroy04_2F523812.jpg" width="244" height="164" /&gt; Stephen Walther just published links to the video, slides and sample code for his Ajax talk at MIX09:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://stephenwalther.com/blog/archive/2009/03/22/mix-slides-code-and-session-recording.aspx"&gt;http://stephenwalther.com/blog/archive/2009/03/22/mix-slides-code-and-session-recording.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It’s pretty cool to see all the work we put into Ajax this past year or so presented at MIX. This is a really nice presentation, like Stephen’s always are.&lt;/p&gt;  &lt;p&gt;Another presentation I had lots of fun watching (not just because the speaker is making an incredible impression of me but also because I’ve been spending a good part of my time lately contributing to the application he’s showing) is Rob Conery’s. Rob is showing an interesting way to develop ASP.NET applications, aimed at ease of use and customization rather than architectural purity. Check it out, let me know what you think…&lt;/p&gt;  &lt;p&gt;&lt;a href="http://videos.visitmix.com/MIX09/T62F"&gt;http://videos.visitmix.com/MIX09/T62F&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6994071" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category></item><item><title>How to reflect over a delegate’s signature</title><link>http://weblogs.asp.net/bleroy/archive/2009/01/23/how-to-reflect-over-a-delegate-s-signature.aspx</link><pubDate>Fri, 23 Jan 2009 22:41:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6858792</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6858792</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/01/23/how-to-reflect-over-a-delegate-s-signature.aspx#comments</comments><description>&lt;P&gt;I’ve done some tricky work with delegates recently and I’ve had a hard time trying to reflect over the signature of a delegate type. I feel a little silly now that the solution to this has been provided to me by &lt;A href="http://blogs.msdn.com/ericlippert/" mce_href="http://blogs.msdn.com/ericlippert/"&gt;Eric Lippert&lt;/A&gt;. It’s actually quite simple, just reflect over the Invoke method:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MethodInfo &lt;/SPAN&gt;invoke = &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&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;bool&lt;/SPAN&gt;&amp;gt;).GetMethod(&lt;SPAN style="COLOR: #a31515"&gt;"Invoke"&lt;/SPAN&gt;);
&lt;SPAN style="COLOR: #2b91af"&gt;Console&lt;/SPAN&gt;.WriteLine(invoke.ReturnType.Name);&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;I hope this helps others…&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6858792" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Wally’s introduction to 3.5 SP1</title><link>http://weblogs.asp.net/bleroy/archive/2008/12/29/wally-s-introduction-to-3-5-sp1.aspx</link><pubDate>Mon, 29 Dec 2008 19:36:18 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6808745</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6808745</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2008/12/29/wally-s-introduction-to-3-5-sp1.aspx#comments</comments><description>&lt;p&gt;Wally McClure, MVP extraordinaire and ASP Insider, just published &lt;a href="http://www.wrox.com/WileyCDA/WroxTitle/New-Features-in-ASP-NET-3-5-Service-Pack-1.productCd-0470457341.html"&gt;a short book about the new features in ASP.NET 3.5 SP1&lt;/a&gt;. It is a short and to the point read that should get you started in no time. I wouldn’t have shown web service access as the main advantage of &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; myself (the selector and animation support adds more value for ASP.NET Ajax developers) but that’s a minor thing, and there are &lt;a href="http://docs.jquery.com/Main_Page"&gt;plenty of&lt;/a&gt; &lt;a href="http://docs.jquery.com/Tutorials"&gt;other resources&lt;/a&gt; to learn about &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;. Topics in Wally’s book include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Ajax History&lt;/li&gt;    &lt;li&gt;Script Combining (including ScriptReferenceProfiler)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;Routing&lt;/li&gt;    &lt;li&gt;Entity Framework&lt;/li&gt;    &lt;li&gt;Dynamic Data&lt;/li&gt;    &lt;li&gt;ADO.NET Data Services&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://www.wrox.com/WileyCDA/WroxTitle/New-Features-in-ASP-NET-3-5-Service-Pack-1.productCd-0470457341.html"&gt;http://www.wrox.com/WileyCDA/WroxTitle/New-Features-in-ASP-NET-3-5-Service-Pack-1.productCd-0470457341.html&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6808745" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category></item></channel></rss>