The C# 2010 dynamic keyword can be used for a variety of reasons, some of which are a tad esoteric. However, there is a very practical use of this new language feature, specifically the (radical) simplification of COM interop programming. By default, when you import a COM library into a VS 2010 project (configured to target .NET 4.0), every COM VARIANT is automatically mapped to a dynamic data type.
This behavior greatly decreases the need to cast data types and drill into low level interop primitives (such as the underlying get_ / set_ methods).
Furthermore, thanks to C# 2010 optional arguments, you no longer need to author reams of Type.Missing tokens when specifying missing values.
To showcase the distinction, assume you wish to map some data in a List to Microsoft Excel. Before .NET 4.0, you might author code such as the following:
Code Snippet
- static void ExportToExcel2008(List<Car> carsInStock)
- {
- Excel.Application excelApp = new Excel.Application();
-
- // Must mark missing params!
- excelApp.Workbooks.Add(Type.Missing);
-
- // Must cast Object as _Worksheet!
- Excel._Worksheet workSheet = (Excel._Worksheet)excelApp.ActiveSheet;
-
- // Must cast each Object as Range object then call
- // call low level Value2 property!
- ((Excel.Range)excelApp.Cells[1, "A"]).Value2 = "Make";
- ((Excel.Range)excelApp.Cells[1, "B"]).Value2 = "Color";
- ((Excel.Range)excelApp.Cells[1, "C"]).Value2 = "Pet Name";
-
- int row = 1;
- foreach (Car c in carsInStock)
- {
- row++;
- // Must cast each Object as Range and call low level Value2 prop!
- ((Excel.Range)workSheet.Cells[row, "A"]).Value2 = c.Make;
- ((Excel.Range)workSheet.Cells[row, "B"]).Value2 = c.Color;
- ((Excel.Range)workSheet.Cells[row, "C"]).Value2 = c.PetName;
- }
-
- // Must call get_Range method and then specify all missing args!.
- excelApp.get_Range("A1", Type.Missing).AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2,
- Type.Missing, Type.Missing, Type.Missing,
- Type.Missing, Type.Missing, Type.Missing);
-
- // Must specify all missing optional args!
- workSheet.SaveAs(string.Format(@"{0}\Inventory.xlsx", Environment.CurrentDirectory),
- Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
- Type.Missing, Type.Missing, Type.Missing);
- excelApp.Quit();
- MessageBox.Show("The Inventory.xslx file has been saved to your app folder", "Export complete!");
- }
Notice in particular the grimy casting operations (Excel.Range, and so forth).
Now, here is the same code once again, using the new features of C# 2010:
Code Snippet
- static void ExportToExcel(List<Car> carsInStock)
- {
- // Load up Excel, then make a new empty workbook.
- Excel.Application excelApp = new Excel.Application();
- excelApp.Workbooks.Add();
-
- // This example uses a single workSheet.
- Excel._Worksheet workSheet = excelApp.ActiveSheet;
-
- // Establish column headings in cells.
- workSheet.Cells[1, "A"] = "Make";
- workSheet.Cells[1, "B"] = "Color";
- workSheet.Cells[1, "C"] = "Pet Name";
-
- // Now, map all data in List<Car> to the cells of the spread sheet.
- int row = 1;
- foreach (Car c in carsInStock)
- {
- row++;
- workSheet.Cells[row, "A"] = c.Make;
- workSheet.Cells[row, "B"] = c.Color;
- workSheet.Cells[row, "C"] = c.PetName;
- }
-
- // Give our table data a nice look and feel.
- workSheet.Range["A1"].AutoFormat(
- Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);
-
- // Save the file, quit Excel and display message to user.
- workSheet.SaveAs(string.Format(@"{0}\Inventory.xlsx", Environment.CurrentDirectory));
- excelApp.Quit();
- MessageBox.Show("The Inventory.xslx file has been saved to your app folder", "Export complete!");
- }
Nice, eh? I'm sure you'd agree that simplification is always a good thing...
b0fd8d16-bedd-46f0-9363-35f777f1494c|0|.0