I am reading Silverlight 4 in Action and simultaneouly trying examples in it. However, i can’t seem to understand the new dynamic keyword and why is it needed for COM. This is my code :-
if (AutomationFactory.IsAvailable)
{
dynamic excel =
AutomationFactory.CreateObject("Excel.Application");
excel.Visible = true;
dynamic workbook = excel.workbooks;
workbook.Add();
dynamic sheet = excel.ActiveSheet;
int i = 1;
double[] data = new double[] { 1.0, 5.0, 9.5, 2.7, 3.2, 0.6 };
foreach (double d in data)
{
dynamic cell = sheet.Cells[i, 1];
cell.Value = "Row " + i;
cell.ColumnWidth = 10;
cell = sheet.Cells[i, 2];
cell.Value = d;
i++;
}
dynamic shapes = sheet.Shapes;
shapes.AddChart(-4100, 120, 2, 300, 200);
}
I am wondering how did the author come to know that shapes support AddChart method and how did he know which parameters he needed to pass to this function? Can somebody throw some light on this?
Thanks in advance 🙂
It is not needed for COM. However, the code that doesn’t use
dynamicis a real mess.As to how the author knew what methods are available, he read the Excel VBA documentation. Looking at the Excel type library would also tell you the method signatures, but without the explanation.