A quick look at the C# dynamic keyword

Withe the release of .NET 4.0, C# has indeed gained "scripting like" functionality with a new keyword called dynamic.

Two common uses for this keyword are to simplify complex reflection code, and simplify how we interact with COM object models.

Assume for example, that you need to use the .NET reflection API to invoke a method using traditional late binding.  You could author the following code:

static void CreateUsingLateBinding(Assembly asm)

{

  try

  {

    // Get metadata for the Minivan type.

    Type miniVan = asm.GetType("CarLibrary.MiniVan");

 

    // Create the Minivan on the fly.

    object obj = Activator.CreateInstance(miniVan);

 

    // Get info for TurboBoost.

    MethodInfo mi = miniVan.GetMethod("TurboBoost");

 

    // Invoke method ('null' for no parameters).

    mi.Invoke(obj, null);

  }

  catch (Exception ex)

  {

    Console.WriteLine(ex.Message);

  }

}

While this method works as expected, you could simplify your code quite a bit if you use the C# 4.0 dynamic keyword:

static void InvokeMethodWithDynamicKeyword(Assembly asm)

{

  try

  {

    // Get metadata for the Minivan type.

    Type miniVan = asm.GetType("CarLibrary.MiniVan");

 

    // Create the Minivan on the fly and call method!

    dynamic obj = Activator.CreateInstance(miniVan);

    obj.TurboBoost();

  }

  catch (Exception ex)

  {

    Console.WriteLine(ex.Message);

  }

}

Notice how a bulk of the low level reflection code is out of site and out of mind.  In order for this to work, a number of new concepts have been introduced into .NET 4.0, including expression trees and the DLR (dynamic language runtime). I won't get into that right now, but the short answer is we have less code to author.

Like all "shortcuts" however, the dynamic keyword has one *big* limitation.  Because the underlying type and members are not know until runtime, any variable declared with the dynamic keyword will have *zero* IntelliSense!  Also, the compiler will *not* check anything whatsoever (spelling, casing, etc). Therefore, the following code will compile just fine:

 

    // Create the Minivan on the fly and call method!

    dynamic obj = Activator.CreateInstance(miniVan);

    obj.FOO_FOO_FOO("dkjhaskjdhsjdhks");

At runtime however, you will receive an exception from the DLR. 


Posted by: Andrew Troelsen
Posted on: 3/12/2010 at 9:37 AM
Tags:
Categories: .NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Subscribe to this BlogRSS comment feed

Add comment




biuquote
  • Comment
  • Preview
Loading


Contact Us 651-994-8558 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Prism / MVVM / MEF Training | Microsoft Web Development Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.