{"id":3940,"date":"2014-03-30T06:27:35","date_gmt":"2014-03-30T06:27:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-will-you-use-the-c-4-dynamic-type-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:27:35","modified_gmt":"2014-03-30T06:27:35","slug":"how-will-you-use-the-c-4-dynamic-type-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-will-you-use-the-c-4-dynamic-type-collection-of-common-programming-errors\/","title":{"rendered":"How will you use the C# 4 dynamic type?-Collection of common programming errors"},"content":{"rendered":"<p>Wherever old-fashioned reflection is used now and code readability has been impaired. And, as you say, some Interop scenarios (I occasionally work with COM).<\/p>\n<p>That&#8217;s pretty much it. If <code>dynamic<\/code> usage can be avoided, it should be avoided. Compile time checking, performance, etc.<\/p>\n<p>A few weeks ago, I remembered this article. When I first read it, I was frankly apalled. But what I hadn&#8217;t realised is that I didn&#8217;t know how to even <em>use<\/em> an operator on some unknown type. I started wondering what the generated code would be for something like this:<\/p>\n<pre><code>dynamic c = 10;\nint b = c * c;\n<\/code><\/pre>\n<p>Using regular reflection, you can&#8217;t use defined operators. It generated quite a bit of code, using some stuff from a <code>Microsoft<\/code> namespace. Let&#8217;s just say the above code is a lot easier to read \ud83d\ude42 It&#8217;s nice that it works, but it was also <em>very<\/em> slow: about 10,000 times slower than a regular multiplication (doh), and about 100 times slower than an <code>ICalculator<\/code> interface with a Multiply method.<\/p>\n<p><strong>Edit<\/strong> &#8211; generated code, for those interested:<\/p>\n<pre><code>if (o__SiteContainer0.p__Sitea == null)\n  o__SiteContainer0.p__Sitea =\n    CallSite.Create(\n      new CSharpBinaryOperationBinder(ExpressionType.Multiply,\n        false, false, new CSharpArgumentInfo[] {\n          new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null),\n          new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null) }));\nb = o__SiteContainer0.p__Site9.Target(\n      o__SiteContainer0.p__Site9,\n      o__SiteContainer0.p__Sitea.Target(\n        o__SiteContainer0.p__Sitea, c, c));\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Wherever old-fashioned reflection is used now and code readability has been impaired. And, as you say, some Interop scenarios (I occasionally work with COM). That&#8217;s pretty much it. If dynamic usage can be avoided, it should be avoided. Compile time checking, performance, etc. A few weeks ago, I remembered this article. When I first read [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3940","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3940"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3940\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}