C# 4.0 Gets dynamic

I’ve been watching some of the PDC 2008 videos online.  One of the highlights is The Future of C# by Anders Hejlsberg, and I highly recommend it to anyone that’s interested in .NET.  Most of the session focuses on the dynamic capabilities that are coming to C# 4.0.  The .NET Common Language Runtime (CLR) was built primarily with static typing (aka “strong typing”) in mind, in which every variable must be explicitly declared and assigned a type (string, integer, etc.).  Microsoft recently built the Dynamic Language Runtime (DLR) to support dynamic (non-static) languages, such as Python and Ruby, and they are using the DLR to bring some dynamic features to C#.

To you strong-typing zealots, there’s no need to be alarmed.  C# is not transforming itself from a static to a dynamic language.  There is a gap between static and dynamic languages, and the focus is on closing that gap and improving the interaction.  It just so happens that FoxPro is a dynamic language…

To illustrate the problem, here is a simple VFP class I wrote that finds a customer and creates an object for the customer using Scatter:

Define Class Customers As Session OlePublic

oCustomer = NULL

Procedure GetCustomer(lcCustomerID As String)
Local loCustomer

If !Used("Customers")
 Select 0
 Use (Home(2) + "Northwind\Customers")
Else
 Select Customers
EndIf
Locate For CustomerID = lcCustomerID
Scatter Name This.oCustomer Memo

EndProc

Procedure Destroy

Use In Select("Customers")

Endproc

Enddefine

I compiled the class into a COM DLL and imported it into my C# project.  (For more on that, see Rick Strahl’s article: .NET Interop for VFP Applications.)  Since This.oCustomer is created dynamically at runtime, FoxPro cannot include it in the type library.  Therefore, to access any properties of the oCustomer in C#, you have to use “reflection”.  Here’s the C# code:

string CustomerID = "ALFKI";
netcomtest.Customers oCustomers = new netcomtest.Customers();

oCustomers.GetCustomer(CustomerID);
object oCust = oCustomers.OCUSTOMER;
//use reflection to access object properties
object CompanyName = oCust.GetType().InvokeMember("COMPANYNAME", BindingFlags.GetProperty, null, oCust, null);
object ContactName = oCust.GetType().InvokeMember("CONTACTNAME", BindingFlags.GetProperty, null, oCust, null);
//display company and contact name
MessageBox.Show(CompanyName.ToString() + ContactName.ToString());

What a mess!  If I wanted to access all of the properties on the object, this would get very tedious.  C# 4.0 makes the process easier by introducing a new “dynamic” type.  This tells C# to resolve the type of the variable at runtime (just like Fox would), rather than at compile time.  If I understand Anders correctly, here’s what the new code should look like :

string CustomerID = "ALFKI";
netcomtest.Customers oCustomers = new netcomtest.Customers();

oCustomers.GetCustomer(CustomerID);
//create dynamic reference to object
dynamic oCust = oCustomers.OCUSTOMER;
//display company and contact name
MessageBox.Show(oCust.CompanyName + oCust.ContactName);

Now, isn’t that better?  I can actually understand this code.  I can access all the properties of oCust directly, just like I would in FoxPro.  It’s nice to see that COM Interop is one of the things that Microsoft is still improving.

Anders also showed some things they are working on beyond C# 4.0.  Fast forward to the 60 minute mark in the video to see the cutting-edge stuff they are working on.  In short, they are rewriting the compiler, so they can do stuff like put code into a string variable and evaluate/execute it at runtime.  Fantastic!  Anders took it a step further and showed C# commands being executed as he entered them into a window.  Unbelievable!  It’s amazing what they can do these days!  Seriously, should you find yourself working with C# in the future, it will be nice to have some capabilities we’ve grown to love in VFP.

One Reply to “C# 4.0 Gets dynamic”

Leave a Reply

Your email address will not be published. Required fields are marked *