COM Objects

Visual Studio 2010 and Later

This functionality is brought to you from the Microsoft.CSharp.dll
This assembly is added by default when you use Visual Studio 2010 to create a project that targets .NET Framework 4.0.
This contains the C# runtime binder and allows you to use the dynamic keyword
The dynamic type is a static type
An object defined as a dynamic type bypasses static type checking
In most cases this will behave the same as the object type.
At compile type, an element of type dynamic is assumed to support any operation.

A new data type called "dynamic" was added in Visual Studio 2010 to simplify this.
By introducing this new data type COM objects can be treated as if they were dynamic which means they don't have to be explicitly casted.

excelapp.Cells[1,1].Value = "text"; 
Excel.Range range = excepapp.Cells[1,1];
Excel.Worksheet wsh = Globals.ThisAddIn.Application.Activesheet;

Visual Studio 2008 and Earlier

Many COM methods allow for variation in argument types and return types by designating the types as object.
Because of this we have had to explicitly cast the values to strongly typed variables in C#

((Excel.Range)excelapp.Cells[1,1]).Value2 = "text"; 
Excel.Range range = (Excel.Range)excelapp.Cells[1,1];

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext