<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Rob's Blog</title>
        <link>http://robrich.org/Default.aspx</link>
        <description>Rob's Blog</description>
        <language>en-US</language>
        <copyright>Rob Richardson</copyright>
        <generator>Subtext Version 2.5.0.3</generator>
        <image>
            <title>Rob's Blog</title>
            <url>http://robrich.org/images/RSS2Image.gif</url>
            <link>http://robrich.org/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Extending ASP.NET MVC</title>
            <category>Events</category>
            <link>http://robrich.org/archive/2011/11/17/Extending-ASP-NET-MVC.aspx</link>
            <description>It is my pleasure to speak tonight at the SEVDNUG about extending ASP.NET MVC.  This talk grew out of an impromptu talk I gave at Desert Code Camp 2011.2.  The speaker didn't show for a class titled "Real world MVC", so I took it and ran with it.&lt;br /&gt;
&lt;br /&gt;
In this talk we'll look at some common and some less-common extension points for ASP.NET MVC:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Routes&lt;/li&gt;
    &lt;li&gt;Route Constraint&lt;/li&gt;
    &lt;li&gt;Html Helper&lt;/li&gt;
    &lt;li&gt;Action Filter&lt;/li&gt;
    &lt;li&gt;Value Provider&lt;/li&gt;
    &lt;li&gt;Action Result&lt;/li&gt;
    &lt;li&gt;Model Metadata Provider&lt;/li&gt;
    &lt;li&gt;Dependency Resolver&lt;/li&gt;
&lt;/ul&gt;
The goal is to keep your code DRY (Don't Repeat Yourself), and many of these tools make it much easier to centralize cross-cutting concerns.&lt;br /&gt;
&lt;br /&gt;
Grab the code and slides we'll work with from &lt;a target="_blank" href="/content/ExtendingMVC.zip"&gt;here&lt;/a&gt;.&lt;img src="http://robrich.org/aggbug/81.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2011/11/17/Extending-ASP-NET-MVC.aspx</guid>
            <pubDate>Fri, 18 Nov 2011 00:03:32 GMT</pubDate>
            <comments>http://robrich.org/archive/2011/11/17/Extending-ASP-NET-MVC.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/81.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Desert Code Camp 2011.2</title>
            <category>Events</category>
            <link>http://robrich.org/archive/2011/11/04/Desert-Code-Camp-2011-2.aspx</link>
            <description>Desert Code Camp is always such a pleasure, and I'm pleased to be able to speak twice this time.  I'm teaching two topics: Backbone and Qunit.&lt;br /&gt;
&lt;br /&gt;
Backbone is an MVC framework for JavaScript.  Much like you wouldn't stick your entire program in a click even handler, why would you stick all your client-side code in a jQuery click event handler?  Backbone gives you the structure to create well architected client-side code.  Backbone is also light enough and modular enough and not opinionated enough to get you quickly to an on-ramp.  Download the slides and sample code we'll play with &lt;a href="/content/backbone.zip"&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Qunit is the JavaScript unit test framework used by the jQuery team to test jQuery.  It is a great red/green test harness for doing very elegant testing of client-side business logic.  Download the slides and sample code we'll play with &lt;a href="/content/qunit.zip"&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
If you enjoyed this talk, leave me a comment.  If you didn't like it, tell me how I can do better.&lt;br /&gt;
&lt;br /&gt;
Rob&lt;img src="http://robrich.org/aggbug/80.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2011/11/04/Desert-Code-Camp-2011-2.aspx</guid>
            <pubDate>Sat, 05 Nov 2011 05:27:37 GMT</pubDate>
            <comments>http://robrich.org/archive/2011/11/04/Desert-Code-Camp-2011-2.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/80.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Web Forms to Razor</title>
            <category>Events</category>
            <link>http://robrich.org/archive/2011/07/28/Web-Forms-to-Razor.aspx</link>
            <description>I'm honored to present at SEVDNUG tonight.  Our topic is "Web Forms to Razor".  Our primary focus is getting familiar with the new mechanisms in Razor and MVC, and learning how we can transition a product from one platform to another.&lt;br /&gt;
&lt;br /&gt;
We'll run with the solution &lt;a href="/content/WebFormsToRazor.zip"&gt;here&lt;/a&gt;, comparing the Web project (MVC) to the WebForms project.  Both include identical functionality and look &amp;amp; feel.  When we're done, we'll have a good introduction to MVC concepts and in particular Razor concepts -- enough to begin you thinking.  Over the course of the evening, we'll reference Phil Hack's Razor Cheat Sheet found at &lt;a href="http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx"&gt;http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Razor and MVC provide a newer, cleaner mechanism for handling web scenarios.  The former paradigm of abstracting HTML and JavaScript away from us has gotten in the way.  MVC gives us a foundation for a clean separation of concerns where it matters, and Razor provides a format for cleanly rendering html.  It's a very elegant stack, and I hope you enjoy this evening's demo.&lt;br /&gt;
&lt;br /&gt;
Rob&lt;img src="http://robrich.org/aggbug/79.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2011/07/28/Web-Forms-to-Razor.aspx</guid>
            <pubDate>Thu, 28 Jul 2011 22:35:59 GMT</pubDate>
            <comments>http://robrich.org/archive/2011/07/28/Web-Forms-to-Razor.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/79.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Entity Framework Code First</title>
            <category>Code</category>
            <link>http://robrich.org/archive/2011/05/30/Entity-Framework-Code-First.aspx</link>
            <description>I keep finding myself referring to two links on Entity Framework Code First:&lt;br /&gt;
&lt;ol&gt;
    &lt;li&gt;&lt;a href="http://www.hanselman.com/blog/PDC10BuildingABlogWithMicrosoftUnnamedPackageOfWebLove.aspx"&gt;http://www.hanselman.com/blog/PDC10BuildingABlogWithMicrosoftUnnamedPackageOfWebLove.aspx&lt;/a&gt;  This is Scott Hanselman's introductory demo to "the unnamed package of love" where he demos EF Code First ("Magic Unicorn"), MvcScaffolding ("HanselScaffold"), Sql Server Compact Edition, and Ninject.  It's one of those videos where I finish feeling like I can take on the world.  It is mega cool.&lt;br /&gt;
    &lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx"&gt;http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx&lt;/a&gt;  This is the Entity Framework team's quick walk-through of EF Code First.  It includes the list of conventions, a quick tutorial on getting going, and nice links to other content.&lt;/li&gt;
&lt;/ol&gt;
Those are they.  Now maybe I'll be able to find them easier next time I go looking.  :D&lt;img src="http://robrich.org/aggbug/78.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2011/05/30/Entity-Framework-Code-First.aspx</guid>
            <pubDate>Mon, 30 May 2011 20:59:47 GMT</pubDate>
            <comments>http://robrich.org/archive/2011/05/30/Entity-Framework-Code-First.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/78.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Dependency Injection with Ninject and Mocking with MoQ</title>
            <category>Code</category>
            <category>Events</category>
            <link>http://robrich.org/archive/2011/04/28/Dependency-Injection-with-Ninject-and-Mocking-with-MoQ.aspx</link>
            <description>Today it's my great pleasure to lead the discussion about Dependency Injection with Ninject and Mocking with MoQ at the SEVDNUG.&lt;br /&gt;
&lt;br /&gt;
Our purpose as software developers is to orchestrate together loosely coupled, well tested components.  Yet far too often, our code turns into a big ball of mud.  These two tools and their supporting techniques can create loosely coupled, eagerly testable code that will make you much more productive.&lt;br /&gt;
&lt;br /&gt;
Our starting place is &lt;a href="/content/TodoTightlyCoupled.zip"&gt;this&lt;/a&gt;, an n-tier app with hard-coded dependencies.  This is very hard to unit test.  I grant there's good separation of concerns here, and that typically that's harder than it looks.  After a brief discussion of the architecture of this app, we'll look for seams in the code -- places where we can insert interfaces and separate the dependencies.  We'll make the adjustments with the help of Resharper, then get Ninject to wire up the dependencies for us.  With dependencies solved, we'll look at unit testing various pieces (time permitting).  We'll instantiate the code under test, and mock the dependencies using MoQ.  Once we're done, we'll end up with something similar to &lt;a href="/content/TodoLooslyCoupled.zip"&gt;this&lt;/a&gt;, an app with loosly bound dependencies, a clean separation of concerns, and good test coverage.  Even more important, we will understand the purpose and benefits of Dependency Injection, and have a firm, practical grasp of how to accomplish it.  We'll also have a firm understanding of mocking, and practical tools at hand to make unit testing easy.&lt;br /&gt;
&lt;br /&gt;
Where can you go from here?  Let's look at unit testing routes, integration testing, continuous integration, etc.  The sky's the limit.&lt;br /&gt;
&lt;br /&gt;
Rob&lt;img src="http://robrich.org/aggbug/77.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2011/04/28/Dependency-Injection-with-Ninject-and-Mocking-with-MoQ.aspx</guid>
            <pubDate>Thu, 28 Apr 2011 23:37:04 GMT</pubDate>
            <comments>http://robrich.org/archive/2011/04/28/Dependency-Injection-with-Ninject-and-Mocking-with-MoQ.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/77.aspx</wfw:commentRss>
        </item>
        <item>
            <title>HtmlHelper for something that isn't the Model in ASP.NET MVC</title>
            <category>Code</category>
            <link>http://robrich.org/archive/2011/04/22/HtmlHelper-for-something-that-isnt-the-Model-in-ASP-NET.aspx</link>
            <description>I'm in the midst of a Razor View, and I want to use things like&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;@Html.EditorFor(model =&amp;gt; model.ProductId)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
but  the model isn't a &lt;span style="font-family: Courier New;"&gt;Product&lt;/span&gt;, it's a composite type for this view that looks like this:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;public class SomeViewModel {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    public Product Product { get; set; }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    public List&amp;lt;Irrelevant&amp;gt; IrrelevantList { get; set; }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Well, I can say&lt;br /&gt;
&lt;pre style="font-family: Courier New;"&gt;@Html.EditorFor(model =&amp;gt; model.Product.ProductId)&lt;/pre&gt;
but now the markup on my page says&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;&amp;lt;input type="irrelevant" id="Product_ProductId" name="Product.ProductId" /&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Two problems here:&lt;br /&gt;
1. That's ugly.&lt;br /&gt;
2. The post ActionMethod takes in a &lt;span style="font-family: Courier New;"&gt;Product&lt;/span&gt;, so the model binder doesn't work right.&lt;br /&gt;
&lt;br /&gt;
I really want an &lt;span style="font-family: Courier New;"&gt;HtmlHelper&amp;lt;Product&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
So, I gin one up like so:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family:Courier New;"&gt;var ProductHtml = new HtmlHelper&amp;lt;User&amp;gt;( this.ViewContext, new ViewDataContainer2 { ViewData = new ViewDataDictionary&amp;lt;Product&amp;gt;( Model.Product ) } );&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
and now I can do this:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;@ProductHtml.EditorFor(model =&amp;gt; model.ProductId)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
System.Web.Mvc.Html.TemplateHelpers.ViewDataContainer would be perfect, but it's private, so I cloned it as ViewDataContainer2 like so:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;public class ViewDataContainer2 : IViewDataContainer {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    public ViewDataDictionary ViewData { get; set; }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Presto, it works.  Both problems are resolved.  Awesome.&lt;br /&gt;
&lt;br /&gt;
Rob&lt;img src="http://robrich.org/aggbug/76.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2011/04/22/HtmlHelper-for-something-that-isnt-the-Model-in-ASP-NET.aspx</guid>
            <pubDate>Sat, 23 Apr 2011 03:54:40 GMT</pubDate>
            <comments>http://robrich.org/archive/2011/04/22/HtmlHelper-for-something-that-isnt-the-Model-in-ASP-NET.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/76.aspx</wfw:commentRss>
        </item>
        <item>
            <title>CruiseControl.NET home page link to latest build</title>
            <category>Code</category>
            <category>Tools</category>
            <link>http://robrich.org/archive/2011/04/01/CruiseControl-NET-home-page-link-to-latest-build.aspx</link>
            <description>I often find that I hit the CruiseControl.NET home page, see red on a build, and specifically want the latest build report.  I could click the Project Name link then the latest build link, but that intermediate page isn't buying me much.  What I'd really like is the last build time to be a link to this latest build report.&lt;br /&gt;
&lt;br /&gt;
I cracked open the webdashboard folder, and inside the templates directory was the magic sauce.  I modified ProjectGrid.vm line 127 from:              &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;    &amp;lt;td&amp;gt;$projectGridRow.LastBuildDate&amp;lt;/td&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
to &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;    &amp;lt;td&amp;gt;&amp;lt;a href="/server/$projectGridRow.ServerName/project/$projectGridRow.Name/ViewLatestBuildReport.aspx"&amp;gt;$projectGridRow.LastBuildDate&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
and I now click on the last build date link and get straight to the last build report.  Awesome.&lt;img src="http://robrich.org/aggbug/75.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2011/04/01/CruiseControl-NET-home-page-link-to-latest-build.aspx</guid>
            <pubDate>Fri, 01 Apr 2011 21:16:56 GMT</pubDate>
            <comments>http://robrich.org/archive/2011/04/01/CruiseControl-NET-home-page-link-to-latest-build.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/75.aspx</wfw:commentRss>
        </item>
        <item>
            <title>A cool List&lt;T&gt; .Sort() extension method via lambda</title>
            <category>Code</category>
            <link>http://robrich.org/archive/2010/11/27/A-cool-ListT-Sort-extension-method-via-lambda.aspx</link>
            <description>&lt;p&gt;&lt;font face=""&gt;Here's an awesome List&amp;lt;T&amp;gt;.Sort() extension method:&lt;/font&gt;&lt;/p&gt;
&lt;pre&gt;    &lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; Sort&amp;lt;T,U&amp;gt; ( &lt;span style="COLOR: blue"&gt;this&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt; Source, &lt;span style="COLOR: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T,U&amp;gt; OrderFunc ) {&lt;br /&gt;        &lt;span style="COLOR: blue"&gt;return&lt;/span&gt; Source.OrderBy( OrderFunc ).ToList();&lt;br /&gt;    }&lt;/pre&gt;
&lt;p&gt;Use it with a lambda like this:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    MyList&amp;lt;SomeClass&amp;gt; mylist = PopulateList();&lt;br /&gt;
    mylist = mylist.Sort( i =&amp;gt; i.Sequence );  &lt;/font&gt;ow that's an awesome sort function!&lt;/p&gt;
&lt;p&gt;Or like this for non-:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    MyList&amp;lt;int&amp;gt; mylist = PopulateList();&lt;br /&gt;
    mylist = mylist.Sort( i =&amp;gt; i );&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Now that's a cool sort function!&lt;/p&gt;
&lt;p&gt;Rob&lt;/p&gt;&lt;img src="http://robrich.org/aggbug/74.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2010/11/27/A-cool-ListT-Sort-extension-method-via-lambda.aspx</guid>
            <pubDate>Sat, 27 Nov 2010 17:20:00 GMT</pubDate>
            <comments>http://robrich.org/archive/2010/11/27/A-cool-ListT-Sort-extension-method-via-lambda.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/74.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Google Maps JavaScript API v3 Demo</title>
            <category>Events</category>
            <link>http://robrich.org/archive/2010/11/18/Google-Maps-JavaScript-API-v3-Demo.aspx</link>
            <description>I was honored to speak at the Google Technology User Group last night.  To combine a love of JavaScript, a very powerful tool, and an audience of enthusiastic fans is an incredible joy.&lt;br /&gt;
&lt;br /&gt;
The topic was Google Maps, and specifically the Google Maps JavaScript API version 3.  v3 simplifies and streamlines the api in some very significant ways over v 2.  Most notably, you no longer need an API key, you only need to include a script tag.  That is awesome!&lt;br /&gt;
&lt;br /&gt;
We started from a wonderfully simple "hello world" map, built up through creating Markers and pop-ups (called InfoWindows), worked through geolocation (address to Lat/Lng) and reverse geolocation (Lat/Lng to address), using the user's location (browser permitting), and handling map events.  The crescendo of our activity was events.html:  When the user clicked the map, we created a marker and sidebar content.  Clicking either the marker or the sidebar content opened the InfoWindow and changed the sidebar's style.  Both the pop-up InfoWindow and the sidebar content included a geolocated address.  Dragging the marker would adjust the address in the pop-up and sidebar.  An [X] button in the sidebar could show and hide the points.  It was truly awesome!  All the code we explored is available &lt;a href="http://robrich.org/content/GoogleMapsPlayground.zip"&gt;here&lt;/a&gt;.  There were no slides as everything was a live demo from the code.  The bottom few items on the index page include links to helpful resources, including the official Google Maps Javascript API docs found &lt;a href="http://code.google.com/apis/maps/documentation/javascript/" target="_blank"&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Rob&lt;img src="http://robrich.org/aggbug/73.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2010/11/18/Google-Maps-JavaScript-API-v3-Demo.aspx</guid>
            <pubDate>Thu, 18 Nov 2010 18:13:30 GMT</pubDate>
            <comments>http://robrich.org/archive/2010/11/18/Google-Maps-JavaScript-API-v3-Demo.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/73.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Get all Enum values as a List&lt;MyEnumType&gt;</title>
            <category>Code</category>
            <link>http://robrich.org/archive/2010/11/18/Get-all-Enum-values-as-a-ListMyEnumType.aspx</link>
            <description>Often times it's helpful to get a list of all defined values for an enum.  &lt;span style="font-family: Courier New;"&gt;Enum.GetValues( typeof( MyEnumType ) )&lt;/span&gt; returns an &lt;span style="font-family: Courier New;"&gt;Array&lt;/span&gt;.  This is less than helpful.  An untyped Array is harder to query against than something much more &lt;span style="font-family: Courier New;"&gt;IQueriable&amp;lt;T&amp;gt;&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
So I typically write something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-family: Courier New;"&gt;Type t = typeof(MyEnumType);&lt;br /&gt;
Array vals = Enum.GetValues( t );&lt;br /&gt;
List&amp;lt;MyEnumType&amp;gt; enumvals = new List&amp;lt;MyEnumType&amp;gt;();&lt;br /&gt;
foreach ( T val in vals ) {&lt;br /&gt;
    enumvals.Add( val );&lt;br /&gt;
}&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
After writing this code a dozen times, it occurred to me that an extension method was in order.  Here's the extension method:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;public static List&amp;lt;T&amp;gt; GetListOfEnum&amp;lt;T&amp;gt;() {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Type t = typeof(T);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    if ( !t.IsEnum ) {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        throw new ArgumentOutOfRangeException( t.FullName + " is not an Enum" );&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Array vals = Enum.GetValues( t );&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    List&amp;lt;T&amp;gt; enumvals = new List&amp;lt;T&amp;gt;();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    foreach ( T val in vals ) {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        enumvals.Add( val );&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    return enumvals;&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Call it like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-family: Courier New;"&gt;List&amp;lt;MyEnum&amp;gt; vals = ExtensionMethodClass.GetListOfEnum&amp;lt;MyEnum&amp;gt;();&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
(yeah, since I didn't have a &lt;span style="font-family: Courier New;"&gt;this&lt;/span&gt; parameter, and didn't really want to create a value to call an extension method, it's really just a static method in some utility class.  That's fine though.)&lt;br /&gt;
&lt;br /&gt;
Ideally I'd like to say &lt;span style="font-family: Courier New;"&gt;where T : Enum&lt;/span&gt; (so I'd get a compile error if I passed in a non-Enum T) but &lt;span style="font-family: Courier New;"&gt;where&lt;/span&gt; specifically blocks &lt;span style="font-family: Courier New;"&gt;Array, Delegate, Enum, ValueType&lt;/span&gt;, and &lt;span style="font-family: Courier New;"&gt;object&lt;/span&gt;.  Rats.  Many have argued this should be allowed, but it isn't.  Thus the check / throw at the top.  Some have suggested to use &lt;span style="font-family: Courier New;"&gt;where T : struct&lt;/span&gt; or &lt;span style="font-family: Courier New;"&gt;where T : IConvertable&lt;/span&gt; first to get 1/2 way there before the run-time check.  In the end, I suppose the answer is always the same: have good unit tests.&lt;br /&gt;
&lt;br /&gt;
The last time I built this (non)&lt;span style="text-decoration: line-through;"&gt;extension&lt;/span&gt; method, ReSharper asked if it could help.  I said yes, and it turned it into this:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;public static List&amp;lt;T&amp;gt; GetListOfEnum&amp;lt;T&amp;gt;() {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Type t = typeof(T);&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    if ( !t.IsEnum ) {&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        throw new ArgumentOutOfRangeException( t.FullName + " is not an Enum" );&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    }&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    return Enum.GetValues( typeof(T) ).Cast&amp;lt;T&amp;gt;().ToList();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Now that's pretty compact!  Nice.&lt;br /&gt;
&lt;br /&gt;
Rob&lt;img src="http://robrich.org/aggbug/72.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Richardson</dc:creator>
            <guid>http://robrich.org/archive/2010/11/18/Get-all-Enum-values-as-a-ListMyEnumType.aspx</guid>
            <pubDate>Thu, 18 Nov 2010 17:28:21 GMT</pubDate>
            <comments>http://robrich.org/archive/2010/11/18/Get-all-Enum-values-as-a-ListMyEnumType.aspx#feedback</comments>
            <wfw:commentRss>http://robrich.org/comments/commentRss/72.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>
