Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<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> <p>That's pretty much it. If <code>dynamic</code> usage can be avoided, it should be avoided. Compile time checking, performance, etc.</p> <p>A few weeks ago, I remembered <a href="http://blogs.msdn.com/lucabol/archive/2009/02/05/simulating-inumeric-with-dynamic-in-c-4-0.aspx" rel="noreferrer">this article</a>. When I first read it, I was frankly apalled. But what I hadn't realised is that I didn'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> <pre><code>dynamic c = 10; int b = c * c; </code></pre> <p>Using regular reflection, you can't use defined operators. It generated quite a bit of code, using some stuff from a <code>Microsoft</code> namespace. Let's just say the above code is a lot easier to read :) It'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> <p><strong>Edit</strong> - generated code, for those interested:<br/></p> <pre><code>if (&lt;Test&gt;o__SiteContainer0.&lt;&gt;p__Sitea == null) &lt;Test&gt;o__SiteContainer0.&lt;&gt;p__Sitea = CallSite&lt;Func&lt;CallSite, object, object, object&gt;&gt;.Create( new CSharpBinaryOperationBinder(ExpressionType.Multiply, false, false, new CSharpArgumentInfo[] { new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null), new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null) })); b = &lt;Test&gt;o__SiteContainer0.&lt;&gt;p__Site9.Target( &lt;Test&gt;o__SiteContainer0.&lt;&gt;p__Site9, &lt;Test&gt;o__SiteContainer0.&lt;&gt;p__Sitea.Target( &lt;Test&gt;o__SiteContainer0.&lt;&gt;p__Sitea, c, c)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload