How will you use the C# 4 dynamic type?-Collection of common programming errors

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’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 it, I was frankly apalled. But what I hadn’t realised is that I didn’t know how to even use an operator on some unknown type. I started wondering what the generated code would be for something like this:

dynamic c = 10;
int b = c * c;

Using regular reflection, you can’t use defined operators. It generated quite a bit of code, using some stuff from a Microsoft namespace. Let’s just say the above code is a lot easier to read 🙂 It’s nice that it works, but it was also very slow: about 10,000 times slower than a regular multiplication (doh), and about 100 times slower than an ICalculator interface with a Multiply method.

Edit – generated code, for those interested:

if (o__SiteContainer0.p__Sitea == null)
  o__SiteContainer0.p__Sitea =
    CallSite.Create(
      new CSharpBinaryOperationBinder(ExpressionType.Multiply,
        false, false, new CSharpArgumentInfo[] {
          new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null),
          new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null) }));
b = o__SiteContainer0.p__Site9.Target(
      o__SiteContainer0.p__Site9,
      o__SiteContainer0.p__Sitea.Target(
        o__SiteContainer0.p__Sitea, c, c));