开发者

No intellisense for dynamic keyword?

开发者 https://www.devze.com 2023-01-16 15:44 出处:网络
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 :-

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,开发者_如何学Python 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 dynamic is 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.


Dynamic basically means you don't know at compile time what type you will have (or sometimes the type doesn't even exist yet). Hence the absence of intellisense.

With a dynamic object you just call a method, throw in some parameters, and if that exact method with that signature exists, the DLR will call it.

If it doesn't exist you'll get a runtime exception.


" method and how did he know which parameters he needed to pass to this function? "

He probable coded it or read the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消