IntelliSense and ‘dynamic’

Kirill Osenkov has posted a simple example showing the code using forthcoming "dynamic" keyword that will come with C# 4.0. His example uses COM. Kirill summarizes the change:

  1. Referencing EnvDTE.dll is not required anymore – you also don’t need using EnvDTE – don’t need to reference anything!
  2. You declare the ‘dte’ variable to be weakly typed with the new dynamic contextual keyword
  3. You don’t have to cast the instance returned by Activator.CreateInstance
  4. You don’t get IntelliSense as you type in the last line
  5. The calls to dte are resolved and dispatched at runtime

And concludes: "It’s a trade-off, but I still view dynamic as yet another useful tool in the rich C# programmer’s toolbox to choose from."

One of the comments points out: “I may know what the methods of DTE are, but the second solution will require *all* developers in my team to know it as well. While with IntelliSense they learn it as they need.”

I tend to agree. It’s not that I am conservative and don’t welcome language changes. I am for any innovation that fits language spirit and helps writing more compact and understandable code. But while “dynamic” concept will help in the cases where type cast is not available at compile time, I am not sure everyone will accept its trade-offs where use of strong types will be replaced with runtime type evaluation. Look at the Kirill’s C# 3.0 example. The VisualStudio DTE assembly is referenced and imported not because of DTE object instantiation – the call GetTypeFromProgID does not need any references, it goes directly to the registry. The whole purpose of referencing the assembly and importing EnvDTE namespace is to retrieve DTE type information at compile time. Will everyone easily sacrifice this? I am not sure about myself. Moreover, I can imagine coding guidelines in some companies where developers are instructed not to use dynamic types in the scenarios where type information can be obtained at compile time.

On the other hand, I like the idea of removing a reference to dynamically loaded assemblies. So what I would like to see is some kind of hint to an IntelliSense provider that could help with type information, something like this:

using System;
using dynamic("progid://VisualStudio.DTE.10.0")=DTE;

class Program
    static void Main(string[] args)
        Type visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
        dynamic dte = Activator.CreateInstance(visualStudioType) as DTE;
        dte.MainWindow.Visible = true;

I am no sure about the syntax, but you got the idea. You give a hint to IntelliSense about what kind of dynamic object you expect to create. If Visual Studio DTE assembly is installed and available on the development machine, IntelliSense will retrieve DTE methods and properties and list them for you as you type. If there is no such assembly, no IntelliSense help is provided, but the compilation will not fail. At runtime the code will work exactly as in Kirill’s example: generated code will not be affected. Again, I am not sure about the syntax, especially about new interpretation of "using" directive. On the other hand, I feel that "using" section is a right place to put IntelliSense instructions, because this section does not affect runtime code execution.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s