FREE BOOK

Chapter 8: C# 4.0 Features

Posted by Addison Wesley Free Book | C# Language February 02, 2010
This chapter looks at the new features added into C# 4.0 that combine to improve code readability and extend your ability to leverage LINQ to Object queries over dynamic data sources.

Adding a COM-Interop Reference

COM-Interop in C# 4.0 is greatly assisted by an improved COM backing class that is created when you add a COM reference to a project. The improved backing class makes use of optional and named parameters and the dynamic type features that were introduced earlier this chapter. They allowed clumsy code like the following C# 3.0 COM-Interop to Microsoft Excel:

var excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook =
excel.Workbooks.Open(
fileName,

Type
.Missing, Type.Missing, Type.Missing, Type.Missing,
Type
.Missing, Type.Missing, Type.Missing, Type.Missing,
Type
.Missing, Type.Missing, Type.Missing, Type.Missing,
Type
.Missing, Type.Missing);

This code can be rewritten in C# 4.0 simply as

var excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook =

    excel.Workbooks.Open(fileName);

To understand how this simplification (the backing class generated by Visual Studio 2010 when a COM reference was added to the Microsoft Excel 12 Object Library) occurred, the following declaration was generated for the Open method, showing the use of dynamic types and optional and named parameters:

Microsoft.Office.Interop.Excel.Workbook Open( string Filename,
dynamic UpdateLinks = null,
dynamic ReadOnly = nu,
ll dynamic Format = null,
dynamic Password = null,
dynamic WriteResPassword = null,
dynamic IgnoreReadOnlyRecommended = null,
dynamic Origin = null,
dynamic Delimiter = nul,
l dynamic Editable = null,
dynamic Notify = null,
dynamic Converter = nul,
l dynamic AddToMru = null,
dynamic Local = null,
dynamic CorruptLoad = null)

The addition of the default value turned all but one of the parameters (the Filename argument) into optional parameters, and therefore all but the filename can be omitted when this method is called. The same COM method backing class in Visual Studio 2008 has no such feature set, meaning that all arguments must be passed when this method is called.

Microsoft.Office.Interop.Excel.Workbook Open( string Filename, object UpdateLinks, object ReadOnly, object Format, object Password, object WriteResPassword, object IgnoreReadOnlyRecommended, object Origin, object Delimiter, object Editable, object Notify, object Converter, object AddToMru, object Local, object CorruptLoad)

Adding a COM reference is painless in Visual Studio. The step-by-step process is

[lb] Open the Visual Studio C# project that the COM reference is being added to.
[lb] Choose Project-Add Reference… from the main menu (or right-click the References icon in the Solution Explorer and click Add Reference… .
[lb] Click the COM tab and find the COM Object you need as a reference as seen in
Figure 8-3.

[lb] Click the OK button for the Add Reference dialog box.

Figure 8-3: The Add Reference dialog box in Visual Studio 2010.

New Feature: Not Deploying Primary Interop Assemblies

Primary Interop Assemblies are large pre-built .NET assemblies built for certain COM interfaces (like MS Excel and MS Word) to allow strongly typed coding at design time. These assemblies often were larger than the application being built (and loaded separately at runtime) and often caused version issues because they were deployed independently to the compiled application.

C# 4.0 introduces a no-pia feature that compiles only the parts of the Primary Interop Assembly actually used into the assembly (much smaller) and avoids having to load a separate assembly at runtime (much faster).

This is the default behavior in Visual Studio 2010. To return to deploying the full PIA assembly (the default in previous versions of Visual Studio), set the Embed Interop Types property on the reference in question as shown in Figure 8-4.

Figure 8-4: Set the Embed Interop Types to control whether the no-pia feature is used (the default behavior, true) or the previous Visual Studio behavior is used (false).

Total Pages : 11 7891011

comments