dynamic

Introduced in C# 4.0 / Visual Studio 2010
Visual Studio 2010 introduced a new datatype called "dynamic"
This datatype tells the compiler that a variable's type might change and will not be known until run-time.
This is a static type which operates like the 'object' datatype in most situations
If the code is not valid, errors are caught at run-time
Any object can be converted to a dynamic datatype
You can use "dynamic" to access late binding

dynamic Properties=doc.customdocumentproperties 
for each (dynamic p in Properties)
{
}

This functionality is brought to you by the introduction of dynamic types.


The keyword dynamic is used in place of a type dynamic is not a datatype


Everywhere a type can be used, dynamic can be used instead
This can be used as a generic type
This can be used in parameter lists
This can be used in a class field


declare a variable as dynamic and SS the intellisense


The IDispatch interface is used in office automation utilities the dynamic dispatch approach




Late Binding

Visual Studio 2010 and Later

VBA can determine properties of objects at runtime.
A lot of the Office objects return the type object which must be explicitley converted to the correct type.
VBA can determine properties of objects at run-time, effectively providing late binding between your code and the objects themselves.
A new datatype called "dynamic" was added in Visual Studio 2010 to simplify late binding.
C# does not handle late binding

dynamic objDialog = Application.Dialogs[Word.wdWordDialog.wdDialogFilePageSetup]; 
objDialog.PageWidth = 10;
objDialog.PageHeight = 10;
objDialog.Execute();




Visual Studio 2008 and Earlier

You can use Reflection which allows you to dynamically determine available members of types and perform a type of late binding.
To handle late-bound objects you can use a simple wrapper to set the property values.

Word.Dialog objDialog; 
objDialog = Application.Dialogs(Word.wdWordDialog.wdDialogFilePageSetup);
InvokeHelper(objDialog, "Page Width", 10);
InvokeHelper(objDialog, "Page Height", 10);
objDialog.Execute();

private void InvokeHelper(
Word.Dialog objDialog,
string smember,
object objvalue)
{
System.Reflection.Type objType = typeof(Word.Dialog);
objType.InvokeMember(smember,
     System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance,
     null,
     objDialog,
     new object[] {objDialog.ToString()} );
}



Explicit Casting

Visual Studio 2010 and Later

Datatypes > Dynamic Types


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 datatype called "dynamic" was added in Visual Studio 2010 to simplify this.
By introducing this new datatype 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];




© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext