{"id":29,"date":"2008-09-10T11:52:31","date_gmt":"2008-09-10T11:52:31","guid":{"rendered":"http:\/\/www.themissingdocs.net\/wordpress\/?p=29"},"modified":"2008-09-10T11:52:31","modified_gmt":"2008-09-10T11:52:31","slug":"generics-and-net-internals","status":"publish","type":"post","link":"https:\/\/www.themissingdocs.net\/?p=29","title":{"rendered":"Generics and .Net internals"},"content":{"rendered":"<p>I noticed the following method while on a journey through reflector the other day.<\/p>\n<p>RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter(Type otherType)<\/p>\n<p>Quite an odd method really.<\/p>\n<p>Example in use:<br \/>\nGenericEqualityComparer<int>.TypeHandle.<br \/>\nCreateInstanceForAnotherGenericParameter(typeof(T));<\/p>\n<p>This lets you make a GenericEqualityComparer&lt;T> instance.<\/p>\n<p>First thought is why wouldn&#8217;t you write<br \/>\nnew GenericEqualityComparer&lt;T>()<br \/>\n?<\/p>\n<p>As it turns out its because GenericEqualityComparer has a restriction which says T : IEquatable and the current generic type calling this code has no restriction on T.<\/p>\n<p>So this method is really useful.  It allows you to write a set of if statements checking what T implements and then defer processing to appropriate special case generics.<\/p>\n<p>Unfortunately the above method is internal, so you would have to do the equivelent with a bunch of reflection, probably a bit slower too.<\/p>\n<p>I am thinking that we should petition Microsoft to provide a language construct which is syntactic sugar for this method.<\/p>\n<p>var v = new GenericEqualityComparer&lt;T>() : where T is IEquatable;<br \/>\nCould throw an InvalidOperationException if you haven&#8217;t guarded for the case where T is not IEquatable;<\/p>\n<p>In other news 0.0.2.0 of TMD.Algo should be out soon, I have a dequeue, a couple more sorts (really basic ones) and an alternative Heap which supports O(1) Contains, and O(log n) Remove at the expense of some extra book keeping.  Useful for implementing Prim&#8217;s algorithm as the key reduction step can be done in O (log n) (as needed) by simply removing the entry and adding the new one after changing the key.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I noticed the following method while on a journey through reflector the other day. RuntimeTypeHandle.CreateInstanceForAnotherGenericParameter(Type otherType) Quite an odd method really. Example in use: GenericEqualityComparer.TypeHandle. CreateInstanceForAnotherGenericParameter(typeof(T)); This lets you make a GenericEqualityComparer&lt;T> instance. First thought is why wouldn&#8217;t you write new GenericEqualityComparer&lt;T>() ? As it turns out its because GenericEqualityComparer has a restriction which says &hellip; <a href=\"https:\/\/www.themissingdocs.net\/?p=29\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Generics and .Net internals<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-net-stuff"],"_links":{"self":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":0,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}