<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
  <id>http://www.codesprouts.com/</id>
  <title>Code Sprouts</title>
  <updated>2009-12-03T00:03:20+00:00</updated>
  <link href="http://www.codesprouts.com/" />
  
  <subtitle>Wholesome Bits Of Tech</subtitle>
  <author>
    <name>Colin Whitlatch</name>
  </author>
  <generator uri="http://dotnetblogengine.net/" version="1.0.0.0">BlogEngine.Net Syndication Generator</generator>
  <blogChannel:blogRoll>http://www.codesprouts.com/opml.axd</blogChannel:blogRoll>
  <blogChannel:blink>http://feeds2.feedburner.com/CodeSprouts</blogChannel:blink>
  <dc:creator>Colin Whitlatch</dc:creator>
  <dc:description>Wholesome Bits Of Tech</dc:description>
  <dc:language>en-US</dc:language>
  <dc:title>Code Sprouts</dc:title>
  <geo:lat>0.000000</geo:lat>
  <geo:long>0.000000</geo:long>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/CodeSprouts" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="codesprouts" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <id>http://www.codesprouts.com/post/View-LINQ-To-SQL-Statements-Using-A-Debug-TextWriter.aspx</id>
    <title>View LINQ To SQL Statements Using A Debug TextWriter</title>
    <updated>2009-12-03T00:02:43+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=af1ced2c-3b2e-40f3-895a-5f103381e3fe" />
    <link href="http://www.codesprouts.com/post/View-LINQ-To-SQL-Statements-Using-A-Debug-TextWriter.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;When developing a LINQ to SQL data access layer, a common debugging task is to view the actual SQL statements developed by the LINQ to SQL runtime.&amp;#160; There are a variety of ways to do it, such as running a SQL trace or using the &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx" target="_blank"&gt;LINQ to SQL Debug Visualizer&lt;/a&gt;, but the one I find most practical and easy to use is to write the T-SQL directly to the Visual Studio output window.&amp;#160; &lt;/p&gt;  &lt;p&gt;The LINQ To SQL team had the foresight to allow the capture of statements, which is simply done by attaching a class implementing System.IO.TextWriter to the .Log property of the DataContext.&lt;/p&gt;  &lt;p&gt;A simple implementation of this is the following DebugTextWriter:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DebugTextWriter &lt;/span&gt;: System.IO.&lt;span style="color: #2b91af"&gt;TextWriter
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Write(&lt;span style="color: blue"&gt;char&lt;/span&gt;[] buffer, &lt;span style="color: blue"&gt;int &lt;/span&gt;index, &lt;span style="color: blue"&gt;int &lt;/span&gt;count)
    {
        System.Diagnostics.&lt;span style="color: #2b91af"&gt;Debug&lt;/span&gt;.Write(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;(buffer, index, count));
    }

    &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Write(&lt;span style="color: blue"&gt;string &lt;/span&gt;value)
    {
        System.Diagnostics.&lt;span style="color: #2b91af"&gt;Debug&lt;/span&gt;.Write(value);
    }

    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Encoding &lt;/span&gt;Encoding
    {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;System.Text.&lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.Default; }
    }
}&lt;/pre&gt;

&lt;p&gt;To use the DebugTextWriter and direct the output to the window, all that is needed is to instantiate a DebugTextWriter object and set it to the .Log property:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;#if &lt;/span&gt;DEBUG
            &lt;span style="color: #2b91af"&gt;MyDataContext&lt;/span&gt; dataContext = &lt;span style="color: blue"&gt;new &lt;span style="color: #2b91af"&gt;MyDataContext&lt;/span&gt;&lt;/span&gt;();
            dataContext.Log = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DebugTextWriter&lt;/span&gt;();
&lt;span style="color: blue"&gt;#endif&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The purpose of the #if DEBUG ( and ending #endif ) is to ensure that this code is only compiled into the debug version of the assembly.&amp;#160; It is not compiled when the Release configuration is enabled.&amp;#160; &lt;/p&gt;

&lt;p&gt;Once this code has been put in place, executing any LINQ to SQL query will result in the actual T-SQL statements executed being written in the output window when you attached to the debugger:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.codesprouts.com/image.axd?picture=WindowsLiveWriter/ViewLINQToSQLStatementsUsingADebugTextWr/30C9EE9C/image.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://www.codesprouts.com/image.axd?picture=WindowsLiveWriter/ViewLINQToSQLStatementsUsingADebugTextWr/6D323D79/image_thumb.png" width="571" height="313" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;I find that enabling this code at the start of a project makes it a breeze to look at the native T-SQL being sent to the database during all of my debugging sessions.&lt;/p&gt;

&lt;p&gt;- Colin&lt;/p&gt;</summary>
    <published>2009-12-03T00:02:43+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/View-LINQ-To-SQL-Statements-Using-A-Debug-TextWriter.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=af1ced2c-3b2e-40f3-895a-5f103381e3fe</pingback:target>
    <slash:comments>5</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=af1ced2c-3b2e-40f3-895a-5f103381e3fe</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/View-LINQ-To-SQL-Statements-Using-A-Debug-TextWriter.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=af1ced2c-3b2e-40f3-895a-5f103381e3fe</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/Resolve-An-Absolute-Url-To-A-Relative-Url.aspx</id>
    <title>Resolve An Absolute Url To A Relative Url</title>
    <updated>2009-11-04T23:33:18+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=dcc05151-242c-4e31-9965-43d29562ff66" />
    <link href="http://www.codesprouts.com/post/Resolve-An-Absolute-Url-To-A-Relative-Url.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;blockquote&gt;   &lt;p&gt;Typically Url resolving is done by going from relative to absolute, but there are a few cases where you need to go in the opposite direction.&amp;#160; One of those that I commonly see is when an application is using the Server.Transfer method, which only accepts relative Url’s.&lt;/p&gt;    &lt;p&gt;To handle converting absolute url’s to relative ones, I’ve been using an extension method similar to the following:&lt;/p&gt;    &lt;div align="left"&gt;     &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static string &lt;/span&gt;ResolveAbsoluteUrlToRelativeUrl(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Control &lt;/span&gt;control, &lt;span style="color: blue"&gt;string &lt;/span&gt;absoluteUrl)
{
    &lt;span style="color: blue"&gt;string &lt;/span&gt;relativeUrl = &lt;span style="color: blue"&gt;string&lt;/span&gt;.Empty;

    &lt;span style="color: green"&gt;/* Build protocol + server name + port + application path */
    &lt;/span&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;protocol = control.Page.Request.ServerVariables[&lt;span style="color: #a31515"&gt;&amp;quot;SERVER_PORT_SECURE&amp;quot;&lt;/span&gt;];
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(protocol == &lt;span style="color: blue"&gt;null &lt;/span&gt;|| protocol == &lt;span style="color: #a31515"&gt;&amp;quot;0&amp;quot;&lt;/span&gt;)
        protocol = &lt;span style="color: #a31515"&gt;&amp;quot;http://&amp;quot;&lt;/span&gt;;
    &lt;span style="color: blue"&gt;else
        &lt;/span&gt;protocol = &lt;span style="color: #a31515"&gt;&amp;quot;https://&amp;quot;&lt;/span&gt;;

    &lt;span style="color: green"&gt;/* If the port is null, default http or https, we don't need to add it */
    &lt;/span&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;port = control.Page.Request.ServerVariables[&lt;span style="color: #a31515"&gt;&amp;quot;SERVER_PORT&amp;quot;&lt;/span&gt;];
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(port == &lt;span style="color: blue"&gt;null &lt;/span&gt;|| port == &lt;span style="color: #a31515"&gt;&amp;quot;80&amp;quot; &lt;/span&gt;|| port == &lt;span style="color: #a31515"&gt;&amp;quot;443&amp;quot;&lt;/span&gt;)
        port = &lt;span style="color: #a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;
    &lt;span style="color: blue"&gt;else
        &lt;/span&gt;port = &lt;span style="color: #a31515"&gt;&amp;quot;:&amp;quot; &lt;/span&gt;+ port;

    &lt;span style="color: blue"&gt;string &lt;/span&gt;server = control.Page.Request.ServerVariables[&lt;span style="color: #a31515"&gt;&amp;quot;SERVER_NAME&amp;quot;&lt;/span&gt;];
    &lt;span style="color: blue"&gt;string &lt;/span&gt;applicationPath = control.Page.Request.ApplicationPath;

    &lt;span style="color: blue"&gt;string &lt;/span&gt;serverPath = protocol + server + port + applicationPath;

    &lt;span style="color: green"&gt;/* If we don't find the serverPath at the beginning of the Url, 
     * we'll just return the passed in absolute */
    &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(absoluteUrl.ToLower().StartsWith(serverPath.ToLower()))
        relativeUrl = &lt;span style="color: #a31515"&gt;&amp;quot;~&amp;quot; &lt;/span&gt;+ absoluteUrl.Remove(0, serverPath.Length);
    &lt;span style="color: blue"&gt;else
        &lt;/span&gt;relativeUrl = absoluteUrl;

    &lt;span style="color: blue"&gt;return &lt;/span&gt;relativeUrl;
}&lt;/pre&gt;
  &lt;/div&gt;

  &lt;p&gt;The code is relatively* straight-forward, and gracefully returns the absolute Url if the relative one cannot be formed.&amp;#160; Since the code requires access to the current request object, making the entire method an extension keeps the code a bit more elegant and enforces that it can only be called in the correct environment.&lt;/p&gt;

  &lt;p&gt;- Colin&lt;/p&gt;

  &lt;p&gt;* pun intended!&lt;/p&gt;&lt;/blockquote&gt;</summary>
    <published>2009-11-04T23:33:18+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/Resolve-An-Absolute-Url-To-A-Relative-Url.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=dcc05151-242c-4e31-9965-43d29562ff66</pingback:target>
    <slash:comments>0</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=dcc05151-242c-4e31-9965-43d29562ff66</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/Resolve-An-Absolute-Url-To-A-Relative-Url.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=dcc05151-242c-4e31-9965-43d29562ff66</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/Mass-Delete-Comments-in-DotNetBlogEngine.aspx</id>
    <title>Mass-Delete Comments in DotNetBlogEngine</title>
    <updated>2009-09-21T22:43:37+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=fb8667d2-099b-4ebb-9cc6-d98f17796456" />
    <link href="http://www.codesprouts.com/post/Mass-Delete-Comments-in-DotNetBlogEngine.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;I’ve recently been under heavy comment spam, and along with taking preventive steps to keep the spam from occurring ( posts are now moderated ), I also built an admin page to allow me to mass-delete comments:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.codesprouts.com/image.axd?picture=image_1.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="328" alt="image" src="http://www.codesprouts.com/image.axd?picture=image_thumb_1.png" width="585" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Right now it’s just a simple aspx page w/ code-behind, all you need to do is drop it into the /admin/pages directory of your site, and if you’re logged in as an admin, you’ll be able to access it by going to &lt;a href="http://www.yoursiteurl/admin/pages/CommentManager.aspx"&gt;http://www.yoursiteurl/admin/pages/CommentManager.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The page files can be &lt;a href="http://www.codesprouts.com/Files/CommentManager.zip"&gt;downloaded here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Depending upon how you have your site configured, you may need to set the web.config to compile under 3.5, which can be done by adding the following to your web.config, nested directly in the &amp;lt;configuration&amp;gt; section:&lt;/p&gt;  &lt;p&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.codedom&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;compilers&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;compiler&lt;/span&gt;&lt;span style="color: red"&gt;language&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;c#;cs;csharp&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;extension&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: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Microsoft.CSharp.CSharpCodeProvider,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/span&gt;&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: red"&gt;warningLevel&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;4&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;providerOption &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;CompilerVersion&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;v3.5&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;providerOption &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;WarnAsError&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;false&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;compiler&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;compiler &lt;/span&gt;&lt;span style="color: red"&gt;language&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;vb;vbs;visualbasic;vbscript&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;extension&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;.vb&lt;/span&gt;&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Microsoft.VisualBasic.VBCodeProvider, System,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/span&gt;&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: red"&gt;warningLevel&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;4&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;providerOption &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;CompilerVersion&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;v3.5&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;providerOption &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;OptionInfer&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;value&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;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;providerOption &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;WarnAsError&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;false&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;compiler&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;compilers&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.codedom&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;assemblyBinding &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;urn:schemas-microsoft-com:asm.v1&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;dependentAssembly&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;assemblyIdentity &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;System.Web.Extensions&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;publicKeyToken&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;31bf3856ad364e35&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;bindingRedirect &lt;/span&gt;&lt;span style="color: red"&gt;oldVersion&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;1.0.0.0-1.1.0.0&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;newVersion&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;3.5.0.0&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;dependentAssembly&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;dependentAssembly&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;assemblyIdentity &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;System.Web.Extensions.Design&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;publicKeyToken&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;31bf3856ad364e35&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;bindingRedirect &lt;/span&gt;&lt;span style="color: red"&gt;oldVersion&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;1.0.0.0-1.1.0.0&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;newVersion&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;3.5.0.0&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;dependentAssembly&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;assemblyBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;     &lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;runtime&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;If anyone is interested in using the comment manager as a plugin, let me know, it’d be a fun exercise to go through ( and blog about! ).&lt;/p&gt;  &lt;p&gt;- Colin&lt;/p&gt;</summary>
    <published>2009-09-21T22:43:37+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/Mass-Delete-Comments-in-DotNetBlogEngine.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=fb8667d2-099b-4ebb-9cc6-d98f17796456</pingback:target>
    <slash:comments>1</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=fb8667d2-099b-4ebb-9cc6-d98f17796456</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/Mass-Delete-Comments-in-DotNetBlogEngine.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=fb8667d2-099b-4ebb-9cc6-d98f17796456</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/ASPNet-MVC-2-Preview-1.aspx</id>
    <title>ASP.Net MVC 2 Preview 1</title>
    <updated>2009-08-03T22:11:05+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=063484ba-6658-4ca5-91a2-255767961f4e" />
    <link href="http://www.codesprouts.com/post/ASPNet-MVC-2-Preview-1.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;I’m a few days late with relaying the announcement, but Scott Guthrie posted up on preview 1 of ASP.Net MVC V2.&amp;#160; It’s quite early in the cycle, but I”m really looking forward to areas, data annotations, and editor templates.&lt;/p&gt;  &lt;p&gt;Read more on &lt;a href="http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-preview-1-released.aspx"&gt;ScottGu’s blog&lt;/a&gt;&lt;/p&gt;</summary>
    <published>2009-08-03T22:11:05+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/ASPNet-MVC-2-Preview-1.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=063484ba-6658-4ca5-91a2-255767961f4e</pingback:target>
    <slash:comments>0</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=063484ba-6658-4ca5-91a2-255767961f4e</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/ASPNet-MVC-2-Preview-1.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=063484ba-6658-4ca5-91a2-255767961f4e</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/Perform-An-Action-Using-The-Thread-Pool.aspx</id>
    <title>Perform An Action Using The Thread Pool</title>
    <updated>2009-07-31T19:48:03+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=3f8f76b0-c90f-4620-b7ec-7a88f06b634a" />
    <link href="http://www.codesprouts.com/post/Perform-An-Action-Using-The-Thread-Pool.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;I ran across a scenario where I needed to call dynamically created Action’s in a threaded manner, and was surprised at how simple the code was to do it:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;QueueAction(&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;action)
{
    System.Threading.&lt;span style="color: #2b91af"&gt;WaitCallback &lt;/span&gt;callback = state =&amp;gt; action();
    System.Threading.&lt;span style="color: #2b91af"&gt;ThreadPool&lt;/span&gt;.QueueUserWorkItem(callback);
}&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Which can then be called in a variety of ways:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;action = &lt;span style="color: blue"&gt;delegate&lt;/span&gt;() { System.Threading.&lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;.Sleep(1000); };
QueueThread(action);
            
QueueThread(() =&amp;gt; System.Threading.&lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;.Sleep(1000));
            
QueueThread(randomUnknownMethod);&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;I won’t make the claim that this is “production worthy” code, but if you’re looking for a quick way to execute an Action on a new thread, it does a pretty fine job.&amp;#160; One thing to note: I used the ThreadPool in this example as it’s typically safer than firing up new Threads from the Thread object, but feel free to improvise.&lt;/p&gt;

&lt;p&gt;- Colin &lt;/p&gt;</summary>
    <published>2009-07-31T19:48:03+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/Perform-An-Action-Using-The-Thread-Pool.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=3f8f76b0-c90f-4620-b7ec-7a88f06b634a</pingback:target>
    <slash:comments>0</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=3f8f76b0-c90f-4620-b7ec-7a88f06b634a</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/Perform-An-Action-Using-The-Thread-Pool.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=3f8f76b0-c90f-4620-b7ec-7a88f06b634a</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/The-Exception-Catcher.aspx</id>
    <title>The Exception Catcher</title>
    <updated>2009-07-05T16:10:11+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=a53ef2d6-88b7-426b-8e72-c0b2eab7efbe" />
    <link href="http://www.codesprouts.com/post/The-Exception-Catcher.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;&lt;a href="http://www.codesprouts.com/files/ExceptionCatcherDemo.zip"&gt;Download the code here.&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;While working on a recent gig, one of the requirements for the code I was developing was to have extensive logging, on both new code being developed &amp;amp; on the existing codebase.&amp;#160;&amp;#160; Due to the mix of new &amp;amp; old code, a lot of files ended up being cluttered with try…catch blocks:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;try
&lt;/span&gt;{
    &lt;span style="color: green"&gt;/* Relatively Simple Operation(s) */
&lt;/span&gt;}
&lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex)
{
    &lt;span style="color: green"&gt;/* Logging Routines... */

    /* UI Error Display... */
&lt;/span&gt;}&lt;/pre&gt;

&lt;p&gt;While there’s nothing wrong with multiple try/catch blocks, in this particular application they were causing quite a bit of code duplication in the logging &amp;amp; UI display code.&amp;#160; To reduce the code duplication and make the code a bit more readable, I created an ExceptionCatcher class to wrap up the duplicated code and reduce most of the previous multiple line try/catch blocks to just a few lines ( typically turning into 1-liners).&amp;#160; &lt;/p&gt;

&lt;p&gt;The ExceptionCatcher is fairly simple, as it is essentially just a single method call that takes an Action, which it executes in the context of its own try/catch block.&lt;/p&gt;

&lt;p&gt;A typical line of code looks something like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ExceptionCatcher&lt;/span&gt;.Try&amp;lt;&lt;span style="color: #2b91af"&gt;BusinessException&lt;/span&gt;&amp;gt;(() =&amp;gt; { saveBusinessObject(); });&lt;/pre&gt;

&lt;p&gt;In this call, the ExceptionCatcher will catch BusinessExceptions and then perform any logic in its own Catch block.&amp;#160; The Try method does return an ExceptionCatcherResult to let the calling code know about any exception that occurred, however there typically wasn’t any necessity to do so.&amp;#160; There’s also an overload on the Try method to rethrow the Exception, to handle cases where you need to perform some action and then let the exception continue to bubble up through the code.&lt;/p&gt;

&lt;p&gt;I’ve included a sample project that demo’s a simpler version of the ExceptionCatcher.&amp;#160; The core functionality is housed in the ExceptionCatcher class, and contains a few overloads to handle the scenarios above.&amp;#160; &lt;/p&gt;

&lt;p&gt;You can &lt;a href="http://www.codesprouts.com/files/ExceptionCatcherDemo.zip"&gt;download the code here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to send any questions or comments to me using the comments link on this post!&lt;/p&gt;

&lt;p&gt;- Colin&lt;/p&gt;</summary>
    <published>2009-07-05T16:10:11+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/The-Exception-Catcher.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=a53ef2d6-88b7-426b-8e72-c0b2eab7efbe</pingback:target>
    <slash:comments>0</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=a53ef2d6-88b7-426b-8e72-c0b2eab7efbe</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/The-Exception-Catcher.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=a53ef2d6-88b7-426b-8e72-c0b2eab7efbe</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/Implement-An-Interface-With-An-Anonymous-Object.aspx</id>
    <title>Implement An Interface With An Anonymous Object</title>
    <updated>2009-06-01T21:06:20+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=38026000-a323-4efe-a7d7-755152b3aa52" />
    <link href="http://www.codesprouts.com/post/Implement-An-Interface-With-An-Anonymous-Object.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;I’m a big fan of anonymous objects ( the ASP.Net MVC framework seems to beckon for their use, especially when working with asynchronous server calls that return objects to serialize in JSON ).&amp;#160; However, one of the drawbacks is that they can only really be defined &amp;amp; used within the context of a single method.&amp;#160; Because of this, if you need to call another function with the contents of the object, you’ll either have to create a custom object or settle with passing delegates or lambda’s in to that function.&lt;/p&gt;  &lt;p&gt;I’ve frequently lamented to my fellow co-workers at Magenic that life would be oh-so-sweet if we were able to inherit interfaces with an anonymous object.&amp;#160; While I just assumed that I was at the mercy of the compiler team to make that happen, I realized that I can do it myself by dynamically building a type at runtime and populating it with the values inside my anonymous object.&lt;/p&gt;  &lt;p&gt;As a proof of concept, I wrote the following code that creates a dynamic implementation of an interface, and then assigns the values of an anonymous object to that dynamic implementation.&lt;/p&gt;  &lt;p&gt;The code is as follows ( commented for clarity! ):&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IRandom
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;int &lt;/span&gt;Number { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}

&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Program
&lt;/span&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: blue"&gt;try
        &lt;/span&gt;{
            &lt;span style="color: #2b91af"&gt;IRandom &lt;/span&gt;random = ConvertAnonymousTypeToInterface&amp;lt;&lt;span style="color: #2b91af"&gt;IRandom&lt;/span&gt;&amp;gt;(&lt;span style="color: blue"&gt;new &lt;/span&gt;{ Number = 3 });
            &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Value of random = &amp;quot; &lt;/span&gt;+ random.Number.ToString());
        }
        &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex)
        {
            &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Error: &amp;quot; &lt;/span&gt;+ ex.Message);
        }

        &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();
    }

    &lt;span style="color: blue"&gt;static &lt;/span&gt;T ConvertAnonymousTypeToInterface&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;object &lt;/span&gt;data) &lt;span style="color: blue"&gt;where &lt;/span&gt;T : &lt;span style="color: blue"&gt;class
    &lt;/span&gt;{
        &lt;span style="color: green"&gt;// A little error checking
        &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T).IsInterface)
            &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;InvalidOperationException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;T must be an interface&amp;quot;&lt;/span&gt;);

        &lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T).IsPublic)
            &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;InvalidOperationException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;T must be a public interface&amp;quot;&lt;/span&gt;);

        &lt;span style="color: blue"&gt;string &lt;/span&gt;typeName = &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T).Name;

        &lt;span style="color: green"&gt;// We need an assembly to generate our type in
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AssemblyName &lt;/span&gt;assemblyName = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AssemblyName&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;AnonymousInterfaceAssembly&amp;quot;&lt;/span&gt;);
        assemblyName.Version = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Version&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;1.0.0.0&amp;quot;&lt;/span&gt;);

        &lt;span style="color: green"&gt;// We need a few builders before we can build a type:
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AssemblyBuilder &lt;/span&gt;assemblyBuilder = &lt;span style="color: #2b91af"&gt;AppDomain&lt;/span&gt;.CurrentDomain.DefineDynamicAssembly
            (assemblyName, &lt;span style="color: #2b91af"&gt;AssemblyBuilderAccess&lt;/span&gt;.RunAndSave);
        &lt;span style="color: #2b91af"&gt;ModuleBuilder &lt;/span&gt;moduleBuilder = assemblyBuilder.DefineDynamicModule
            (&lt;span style="color: #a31515"&gt;&amp;quot;AnonymousInterfaceAssembly&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;AnonymousInterfaceAssembly.dll&amp;quot;&lt;/span&gt;);
        &lt;span style="color: #2b91af"&gt;TypeBuilder &lt;/span&gt;typeBuilder = moduleBuilder.DefineType
            (&lt;span style="color: #a31515"&gt;&amp;quot;AnonymousInterfaceAssembly.&amp;quot; &lt;/span&gt;+ typeName, &lt;span style="color: #2b91af"&gt;TypeAttributes&lt;/span&gt;.Public, &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;object&lt;/span&gt;));

        &lt;span style="color: green"&gt;// Now we can get on with building an object that implements the interface.
        // First we'll tell the runtime we want to implement the interface passed in
        &lt;/span&gt;typeBuilder.AddInterfaceImplementation(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T));

        &lt;span style="color: green"&gt;// We'll be fine with a default constructor
        &lt;/span&gt;typeBuilder.DefineDefaultConstructor(&lt;span style="color: #2b91af"&gt;MethodAttributes&lt;/span&gt;.Public);

        &lt;span style="color: green"&gt;// Implement interface members :o

        // Pull the properties &amp;amp; loop through them
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyDescriptorCollection &lt;/span&gt;interfaceProperties = 
            &lt;span style="color: #2b91af"&gt;TypeDescriptor&lt;/span&gt;.GetProperties(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T));

        &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;PropertyDescriptor &lt;/span&gt;interfaceProperty &lt;span style="color: blue"&gt;in &lt;/span&gt;interfaceProperties)
        {
            &lt;span style="color: green"&gt;// First we'll define a new field to hold our property
            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FieldBuilder &lt;/span&gt;propertyFieldBuilder = typeBuilder.DefineField
                (&lt;span style="color: #a31515"&gt;&amp;quot;_&amp;quot; &lt;/span&gt;+ typeName, interfaceProperty.PropertyType, &lt;span style="color: #2b91af"&gt;FieldAttributes&lt;/span&gt;.Private);

            &lt;span style="color: green"&gt;// Our get &amp;amp; set methods need a few attributes
            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodAttributes &lt;/span&gt;getSetMethodAttributes = 
                &lt;span style="color: #2b91af"&gt;MethodAttributes&lt;/span&gt;.Public | &lt;span style="color: #2b91af"&gt;MethodAttributes&lt;/span&gt;.Virtual;

            &lt;span style="color: green"&gt;// implementing our inteface requires a get method named 'get_PROPERTYNAME' 
            // ( where PROPERTYNAME is really the property name. )
            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodBuilder &lt;/span&gt;propertyGetMethod = typeBuilder.DefineMethod(
                &lt;span style="color: #a31515"&gt;&amp;quot;get_&amp;quot; &lt;/span&gt;+ interfaceProperty.Name, getSetMethodAttributes, 
                interfaceProperty.PropertyType, &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;.EmptyTypes);
            
            &lt;span style="color: #2b91af"&gt;ILGenerator &lt;/span&gt;getMethodGenerator = propertyGetMethod.GetILGenerator();

            &lt;span style="color: green"&gt;// our get consists of loading the field &amp;amp; returning
            &lt;/span&gt;getMethodGenerator.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldarg_0);
            getMethodGenerator.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldfld, propertyFieldBuilder);
            getMethodGenerator.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ret);

            &lt;span style="color: green"&gt;// that's it for the get method, on to the set
            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodBuilder &lt;/span&gt;propertySetMethod = typeBuilder.DefineMethod(
                &lt;span style="color: #a31515"&gt;&amp;quot;set_&amp;quot; &lt;/span&gt;+ interfaceProperty.Name, getSetMethodAttributes, 
                &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;Type&lt;/span&gt;[] { interfaceProperty.PropertyType });

            &lt;span style="color: #2b91af"&gt;ILGenerator &lt;/span&gt;setMethodGenerator = propertySetMethod.GetILGenerator();

            &lt;span style="color: green"&gt;// The code is basically the same ( just in the other direction ), 
            // however we'll have an actual argument coming in ( to set the value )
            &lt;/span&gt;setMethodGenerator.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldarg_0);
            setMethodGenerator.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ldarg_1);
            setMethodGenerator.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Stfld, propertyFieldBuilder);
            setMethodGenerator.Emit(&lt;span style="color: #2b91af"&gt;OpCodes&lt;/span&gt;.Ret);

            &lt;span style="color: green"&gt;// And we're finished!
        &lt;/span&gt;}

        &lt;span style="color: green"&gt;// Let's build our object

        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;anonymousInterfaceType = typeBuilder.CreateType();

        &lt;span style="color: green"&gt;// set values

        &lt;/span&gt;T instance = &lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(anonymousInterfaceType) &lt;span style="color: blue"&gt;as &lt;/span&gt;T;

        &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;PropertyDescriptor &lt;/span&gt;interfaceProperty &lt;span style="color: blue"&gt;in &lt;/span&gt;interfaceProperties)
        {
            &lt;span style="color: #2b91af"&gt;PropertyInfo &lt;/span&gt;dataProperty = data.GetType().GetProperty(interfaceProperty.Name);
            &lt;span style="color: blue"&gt;object &lt;/span&gt;value = dataProperty.GetValue(data, &lt;span style="color: blue"&gt;null&lt;/span&gt;);

            &lt;span style="color: green"&gt;// Find the set method we just built:
            &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodInfo &lt;/span&gt;setterMethod = anonymousInterfaceType.GetMethod(
                &lt;span style="color: #a31515"&gt;&amp;quot;set_&amp;quot; &lt;/span&gt;+ interfaceProperty.Name);

            setterMethod.Invoke(instance, &lt;span style="color: blue"&gt;new object&lt;/span&gt;[] { value });
        }

        &lt;span style="color: blue"&gt;return &lt;/span&gt;instance;
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A couple things to note:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I created this code only as a proof that the concept, and there’s quite a bit of error checking left out ( and potentially quite a few bugs left in )&lt;/li&gt;

  &lt;li&gt;What I’ve written above is basically ‘mocked’ the interface, so the same thing could be achieved through the use of any of the various mocking frameworks available.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;- Colin&lt;/p&gt;</summary>
    <published>2009-06-01T21:06:20+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/Implement-An-Interface-With-An-Anonymous-Object.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=38026000-a323-4efe-a7d7-755152b3aa52</pingback:target>
    <slash:comments>0</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=38026000-a323-4efe-a7d7-755152b3aa52</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/Implement-An-Interface-With-An-Anonymous-Object.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=38026000-a323-4efe-a7d7-755152b3aa52</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/Quick-Tip-Injecting-Script-From-C.aspx</id>
    <title>Quick Tip: Injecting Script From C#</title>
    <updated>2009-05-18T15:07:47+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=12c4e529-61c2-4824-8b34-9919bbeac2d1" />
    <link href="http://www.codesprouts.com/post/Quick-Tip-Injecting-Script-From-C.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;I’ve developed quite a few extensions working with the ASP.Net MVC framework ( don’t worry, I’ll roll them out as I clean them up and make the more useful for mass-consumption ).&amp;#160; I almost always need to inject some javascript into the page in these extensions, and I’ve found literal strings ( using @ before a string ) to be quite handy for development.&lt;/p&gt;  &lt;p&gt;For example, imagine a script block similar to the following:&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;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color: #a31515"&gt;'#divToShow'&lt;/span&gt;).show();
    $(&lt;span style="color: #a31515"&gt;'#divToHide'&lt;/span&gt;).hide();
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Without using string literals, you’ll need to do something like the following:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;script type=\&amp;quot;text/javascript\&amp;quot;&amp;gt;&amp;quot;&lt;/span&gt; + Environment.NewLine +&lt;br /&gt;&lt;span style="color: #006080"&gt;&amp;quot;   $('#divToShow).show();&amp;quot;&lt;/span&gt; + Environment.NewLine + &lt;br /&gt;&lt;span style="color: #006080"&gt;&amp;quot;   $('#divToHide).hide();&amp;quot;&lt;/span&gt; + Environment.NewLine +&lt;br /&gt;&lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;/script&amp;gt;&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;Using string literals:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #006080"&gt;@&amp;quot;&amp;lt;script type=&amp;quot;&lt;/span&gt;&lt;span style="color: #006080"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span style="color: #006080"&gt;&amp;quot;&amp;gt;&lt;br /&gt;    $('#divToShow').hide();&lt;br /&gt;    $('#divToHide').show();&lt;br /&gt;&amp;lt;/script&amp;gt;&amp;quot;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;Much cleaner!&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;It may look subtle, but using string literals throughout the development of my extensions has made things much easier to enhance, refine, and (gasp!) debug.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;What do you think?&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;- Colin&lt;/div&gt;</summary>
    <published>2009-05-18T15:07:47+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/Quick-Tip-Injecting-Script-From-C.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=12c4e529-61c2-4824-8b34-9919bbeac2d1</pingback:target>
    <slash:comments>0</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=12c4e529-61c2-4824-8b34-9919bbeac2d1</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/Quick-Tip-Injecting-Script-From-C.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=12c4e529-61c2-4824-8b34-9919bbeac2d1</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/Sorting-By-String-In-LINQ.aspx</id>
    <title>Sorting By String In LINQ</title>
    <updated>2009-05-15T15:44:06+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=224b336f-92ac-4eb9-a5cc-4cc646d1280e" />
    <link href="http://www.codesprouts.com/post/Sorting-By-String-In-LINQ.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;When implementing a sorting feature in most UI’s, the sorting expression is typically string based.&amp;#160; When you’re using LINQ this becomes a problem due to the sorting methods requiring a lambda function with a typed field name.&lt;/p&gt;  &lt;p&gt;While working on a project recently, a colleague and I came up with a pretty elegant solution that uses reflection to turn the fieldname in string form into an acceptable lambda expression.&amp;#160; &lt;/p&gt;  &lt;p&gt;The code is pretty compact, so I’ve included it below and added comments to what’s going on:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SortingExtensions
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;T&amp;gt; OrderByFieldName&amp;lt;T&amp;gt;
        (&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;T&amp;gt; query, &lt;span style="color: blue"&gt;string &lt;/span&gt;fieldName, &lt;span style="color: blue"&gt;bool &lt;/span&gt;isSortAscending)
    {
        &lt;span style="color: green"&gt;// Find the field we're sorting on and get it's type
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;fieldType = &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T).GetProperty(fieldName).PropertyType;

        &lt;span style="color: green"&gt;// Find the generic sort method. Note the binding flags 
        // since our particular sort method is marked as private &amp;amp; static
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MethodInfo &lt;/span&gt;sortItMethod = &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SortingExtensions&lt;/span&gt;)
            .GetMethod(&lt;span style="color: #a31515"&gt;&amp;quot;SortIt&amp;quot;&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.Static | &lt;span style="color: #2b91af"&gt;BindingFlags&lt;/span&gt;.NonPublic);

        &lt;span style="color: green"&gt;// The method returned is an open generic, 
        // so let's close it with the types we need for this operation
        &lt;/span&gt;sortItMethod = sortItMethod.MakeGenericMethod(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T), fieldType);

        &lt;span style="color: green"&gt;// Now that we have our closed generic type, 
        // we can invoke it with the specified parameters 
        &lt;/span&gt;query = (&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;T&amp;gt;)sortItMethod
            .Invoke(&lt;span style="color: blue"&gt;null&lt;/span&gt;, &lt;span style="color: blue"&gt;new object&lt;/span&gt;[] { query, fieldName, isSortAscending });

        &lt;span style="color: blue"&gt;return &lt;/span&gt;query;
    }

    &lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;T&amp;gt; SortIt&amp;lt;T, K&amp;gt;
        (&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;T&amp;gt; query, &lt;span style="color: blue"&gt;string &lt;/span&gt;sortFieldName, &lt;span style="color: blue"&gt;bool &lt;/span&gt;isSortAscending)
    {
        &lt;span style="color: green"&gt;// The orderby works by examining the lambda passed and looking that the body
        // for the property name and variable type.
        // In order to supply a lambda, we'll need to build one.

        // The parameter will be the left-hand side of the lambda expression
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ParameterExpression &lt;/span&gt;param = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Parameter(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T), &lt;span style="color: #a31515"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;);

        &lt;span style="color: green"&gt;// The body will be the right-hand side of the lambda expression
        &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MemberExpression &lt;/span&gt;body = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Property(param, sortFieldName);

        &lt;span style="color: green"&gt;// We'll build our lambda expression, 
        // first passing in the body and then the parameter
        &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;sortExpression = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Lambda&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T, K&amp;gt;&amp;gt;(body, param);

        &lt;span style="color: green"&gt;// All that's left to do is calling the correct extension method for ordering
        &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(isSortAscending)
            &lt;span style="color: blue"&gt;return &lt;/span&gt;query.OrderBy&amp;lt;T, K&amp;gt;(sortExpression);
        &lt;span style="color: blue"&gt;else
            return &lt;/span&gt;query.OrderByDescending&amp;lt;T, K&amp;gt;(sortExpression);
    }
}&lt;/pre&gt;

&lt;p&gt;The code works in all the situations we’ve tested it in ( LINQ to objects, LINQ to SQL ), and considering reflection is used in the operation, we’re rather proud of it.&amp;#160; A possible performance enhancement left out of this solution would be to cache the MethodInfo for the open generic type to avoid having to reflect every time to find the method, but the overhead is rather minimal in this situation.&lt;/p&gt;

&lt;p&gt;Side Note: &lt;/p&gt;

&lt;p&gt;After we developed our solution, we ran across &lt;a href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" target="_blank"&gt;a post from Scott Guthrie&lt;/a&gt;, that enlightened us to the existence of the Dynamic LINQ Library, which adds string-based support similar to what we’ve done here, however this code is more narrow focused for sorting operations, and is a bit different than the Dynamic LINQ Library.&lt;/p&gt;

&lt;p&gt;- Colin&lt;/p&gt;</summary>
    <published>2009-05-15T15:44:06+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/Sorting-By-String-In-LINQ.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=224b336f-92ac-4eb9-a5cc-4cc646d1280e</pingback:target>
    <slash:comments>0</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=224b336f-92ac-4eb9-a5cc-4cc646d1280e</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/Sorting-By-String-In-LINQ.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=224b336f-92ac-4eb9-a5cc-4cc646d1280e</wfw:commentRss>
  </entry>
  <entry>
    <id>http://www.codesprouts.com/post/ASPNet-MVC-Repeater-Extension.aspx</id>
    <title>ASP.Net MVC Repeater Extension</title>
    <updated>2009-05-08T12:58:33+00:00</updated>
    <link rel="self" href="http://www.codesprouts.com/post.aspx?id=1d57493f-7ade-4d31-bda8-2d1154b8e318" />
    <link href="http://www.codesprouts.com/post/ASPNet-MVC-Repeater-Extension.aspx" />
    <author>
      <name>ColinW</name>
    </author>
    <summary type="html">&lt;p&gt;&lt;a href="http://www.codesprouts.com/Files/RepeaterHarness.zip"&gt;Download the code&lt;/a&gt;!&lt;/p&gt;  &lt;p&gt;When working in the View of an MVC project, you’ll frequently need to iterate through a list of items.&amp;#160; If all you need to do is output the exact same markup for every item, a foreach will serve you well.&amp;#160; However, it’s extremely common that you’ll need to generate some sort of alternating sequence for those items, and the foreach quickly turns rather unwieldy ( normally turning into a simpler for loop and having quite a few conditional statements and temporary variables ).&lt;/p&gt;  &lt;p&gt;Since I frequently run into the situation that I need to generate alternating ‘templates’ for collections of data, I’ve created a simple Repeater extension to handle the task.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Repeater Extension&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The Repeater is a relatively simple extension, however it does contain the following functionality:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Works with any enumerable class &lt;/li&gt;    &lt;li&gt;Accepts a string or action to generate markup &lt;/li&gt;    &lt;li&gt;Supports adding custom markup for the beginning &amp;amp; ending of an ‘item’, allowing you to use the same template for both all items. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Syntax:&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;div &lt;/span&gt;&lt;span style="color: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="color: red"&gt;width&lt;/span&gt;: &lt;span style="color: blue"&gt;100px&lt;/span&gt;;&lt;span style="color: blue"&gt;&amp;quot;&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;.Html.Repeater((&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;)&lt;span style="color: blue"&gt;this&lt;/span&gt;.ViewData[&lt;span style="color: #a31515"&gt;&amp;quot;ItemData&amp;quot;&lt;/span&gt;])
       .BeginTemplate(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;div style='border: solid 1px blue;'&amp;gt;&amp;quot;&lt;/span&gt;)
       .BeginAlternatingTemplate(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;div style='border: solid 1px red;'&amp;gt;&amp;quot;&lt;/span&gt;)
       .Template(item =&amp;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: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="color: red"&gt;margin&lt;/span&gt;: &lt;span style="color: blue"&gt;2px&lt;/span&gt;; &lt;span style="color: red"&gt;padding&lt;/span&gt;: &lt;span style="color: blue"&gt;4px&lt;/span&gt;; &lt;span style="color: red"&gt;border&lt;/span&gt;: &lt;span style="color: blue"&gt;dashed 1px black&lt;/span&gt;;&lt;span style="color: blue"&gt;&amp;quot;&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;item&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;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; 
        })
       .AlternatingTemplate(item =&amp;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: red"&gt;style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="color: red"&gt;text-align&lt;/span&gt;: &lt;span style="color: blue"&gt;right&lt;/span&gt;;&lt;span style="color: blue"&gt;&amp;quot;&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;item &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;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%
&lt;/span&gt;       })
       .EndTemplate(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;lt;/div&amp;gt;&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;)
       .Render();
    &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;/span&gt;&lt;/pre&gt;

&lt;p&gt;Output from the above example:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.codesprouts.com/image.axd?picture=WindowsLiveWriter/ASP.NetMVCRepeaterExtension/52B85E31/image.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://www.codesprouts.com/image.axd?picture=WindowsLiveWriter/ASP.NetMVCRepeaterExtension/71229F1A/image_thumb.png" width="244" height="209" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While there’s support for a custom alternating template, I’ve found that any style changes you want to make between regular/alternating items can be done through BeginTemplate / BeginAlternatingTemplate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using The Repeater In Your Own Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.codesprouts.com/Files/RepeaterExtension.zip"&gt;Download the code&lt;/a&gt; first.&lt;/p&gt;

&lt;p&gt;The Repeater is contained in the MvcExtensions project.&amp;#160; You’ll need to either add the project to your solution or pull out the dll it generates and add then add a reference to the project or dll:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.codesprouts.com/image.axd?picture=WindowsLiveWriter/ASP.NetMVCRepeaterExtension/425CE678/image.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://www.codesprouts.com/image.axd?picture=WindowsLiveWriter/ASP.NetMVCRepeaterExtension/4F56C689/image_thumb.png" width="244" height="206" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Add a namespace entry to your root web.config namespaces element:&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;add &lt;/span&gt;&lt;span style="color: red"&gt;namespace&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MvcExtensions.Repeater&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;After the reference has been added and the namespace is inserted into the web.config, you should see it show up in auto-complete when writing script code in the view ( might take a bit for autocomplete to recognize the extension):&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.codesprouts.com/image.axd?picture=WindowsLiveWriter/ASP.NetMVCRepeaterExtension/559D9D17/image.png"&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="image" border="0" alt="image" src="http://www.codesprouts.com/image.axd?picture=WindowsLiveWriter/ASP.NetMVCRepeaterExtension/1BAE5A2B/image_thumb.png" width="371" height="119" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you’re ready to go!&lt;/p&gt;

&lt;p&gt;If you have any questions or comments, feel free to leave a comment or &lt;a href="mailto:colinw@codesprouts.com"&gt;send me an email&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;- Colin&lt;/p&gt;</summary>
    <published>2009-05-08T12:58:33+00:00</published>
    <link rel="related" href="http://www.codesprouts.com/post/ASPNet-MVC-Repeater-Extension.aspx#comment" />
    <dc:publisher>ColinW</dc:publisher>
    <pingback:server>http://www.codesprouts.com/pingback.axd</pingback:server>
    <pingback:target>http://www.codesprouts.com/post.aspx?id=1d57493f-7ade-4d31-bda8-2d1154b8e318</pingback:target>
    <slash:comments>0</slash:comments>
    <trackback:ping>http://www.codesprouts.com/trackback.axd?id=1d57493f-7ade-4d31-bda8-2d1154b8e318</trackback:ping>
    <wfw:comment>http://www.codesprouts.com/post/ASPNet-MVC-Repeater-Extension.aspx#comment</wfw:comment>
    <wfw:commentRss>http://www.codesprouts.com/syndication.axd?post=1d57493f-7ade-4d31-bda8-2d1154b8e318</wfw:commentRss>
  </entry>
</feed>
