<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.dolittle.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Blog&amp;#39;A&amp;#39;Little : CLR, .net</title><link>http://www.dolittle.com/blogs/einar/archive/tags/CLR/.net/default.aspx</link><description>Tags: CLR, .net</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>.net (generic) type caching</title><link>http://www.dolittle.com/blogs/einar/archive/2008/05/21/net-generic-type-caching.aspx</link><pubDate>Wed, 21 May 2008 07:48:11 GMT</pubDate><guid isPermaLink="false">a744be0d-d88a-46a6-b249-55961ed4a125:3879</guid><dc:creator>Einar Ingebrigtsen</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dolittle.com/blogs/einar/rsscomments.aspx?PostID=3879</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dolittle.com/blogs/einar/commentapi.aspx?PostID=3879</wfw:comment><comments>http://www.dolittle.com/blogs/einar/archive/2008/05/21/net-generic-type-caching.aspx#comments</comments><description>&lt;p&gt;The runtime for .net has a type caching mechanism that is really great. Every so often I write code where you need specific data based upon a type. Normally one tend to revert to a Dictionary with Type as the key and the datatype as the valuetype for the generic parameters. &lt;/p&gt; &lt;p&gt;Typically:&lt;/p&gt; &lt;p&gt;&lt;a href="http://www.dolittle.com/blogs/einar/WindowsLiveWriter/54af7125539d.netgenerictypecaching_8A26/image_4.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="124" alt="image" src="http://www.dolittle.com/blogs/einar/WindowsLiveWriter/54af7125539d.netgenerictypecaching_8A26/image_thumb_1.png" width="377" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;This will then rely on he dictionary and its implementation. Sure, it is not a bad solution, but there is a more effective and last but not least; cooler way of going about achieving the same effect: &lt;/p&gt; &lt;p&gt;&lt;a href="http://www.dolittle.com/blogs/einar/WindowsLiveWriter/54af7125539d.netgenerictypecaching_8A26/image_8.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="162" alt="image" src="http://www.dolittle.com/blogs/einar/WindowsLiveWriter/54af7125539d.netgenerictypecaching_8A26/image_thumb_3.png" width="461" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;The beauty of this is that the static constructor will run once per type and one can do type specific stuff in the constructor&lt;/p&gt; &lt;p&gt;The two implementations above lack a lot, so lets go for a more complete sample: &lt;/p&gt; &lt;p&gt;&lt;a href="http://www.dolittle.com/blogs/einar/WindowsLiveWriter/54af7125539d.netgenerictypecaching_8A26/image_10.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="476" alt="image" src="http://www.dolittle.com/blogs/einar/WindowsLiveWriter/54af7125539d.netgenerictypecaching_8A26/image_thumb_4.png" width="621" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.dolittle.com/blogs/einar/archive/2008/05/21/net-generic-type-caching.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.dolittle.com/blogs/einar/archive/2008/05/21/net-generic-type-caching.aspx"border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt; &lt;a href="http://www.gamedevkicks.com/kick/?url=http://www.dolittle.com/blogs/einar/archive/2008/05/21/net-generic-type-caching.aspx"&gt;&lt;img src="http://www.gamedevkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.dolittle.com/blogs/einar/archive/2008/05/21/net-generic-type-caching.aspx"border="0" alt="kick it on GameDevKicks.com" /&gt;&lt;/a&gt; &lt;img src="http://www.dolittle.com/aggbug.aspx?PostID=3879" width="1" height="1"&gt;</description><category domain="http://www.dolittle.com/blogs/einar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/.net/default.aspx">.net</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/CLR/default.aspx">CLR</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/Optimizing/default.aspx">Optimizing</category></item></channel></rss>