In the C# 4.0 demos, I'm seeing lots of code that uses the dynamic type. For example, the following code sets the value of an Excel cell:
在C#4.0演示中,我看到很多使用动态类型的代码。例如,以下代码设置Excel单元格的值:
excel.Cells[1, 1].Value = ...
excel.Cells [1,1] .Value = ...
However, you can also access the cell in an early bound manner with a cast:
但是,您也可以使用强制转换以早期绑定方式访问单元格:
((Range)excel.Cells[1, 1]).Value = ...;
((范围)excel.Cells [1,1])。值= ......;
Why don't the Excel COM library just describe the Cell type as a Range type in the first place? Similarly, all the arguments to the following method are dynamic:
为什么Excel COM库首先只将Cell类型描述为Range类型?同样,以下方法的所有参数都是动态的:
excel.ActiveWorkbook.Charts.Add(...)
Why couldn't the arguments have been static? Looking at the Excel object model, there are dynamic types everywhere. Is this due to limitations in the expressiveness in COM? Is there a pattern to when dynamic types rather than static types are used in COM libraries?
为什么参数不是静态的?查看Excel对象模型,到处都有动态类型。这是由于COM中表达能力的限制吗?在COM库中使用动态类型而不是静态类型时是否存在模式?
3 个解决方案
#1
The COM library exposes this as a variant, which could mean any number of things. It's really the Office library's "fault" for doing so many things with variants.
COM库将此作为变体公开,这可能意味着任何数量的事情。对于使用变体做这么多事情,这真的是Office库的“错误”。
The dynamic
type is the nearest .NET equivalent to variant (now it exists) but the team didn't want to change tlbimp
to generate dynamic types in PIAs for backward compatibility reasons. You'll only get the "variant to dynamic" conversion in C# 4 when linking a PIA (building the bits you use into your own assembly) instead of referencing it.
动态类型是最接近变体的.NET(现在它存在),但团队不希望改变tlbimp以在PIA中生成动态类型,以实现向后兼容性。在链接PIA(将您使用的位构建到您自己的程序集中)而不是引用它时,您只能在C#4中获得“变量到动态”转换。
#2
In the C# 4.0 demos, I'm seeing lots of code that uses the dynamic type.
在C#4.0演示中,我看到很多使用动态类型的代码。
Since dynamic
is one of the biggest changes in C# 4.0, I'd be hugely surprised if this wasn't the case. That doesn't mean you should immediately start writing all your code with dynamic
- simply that you should recognise when it might be useful.
由于动态是C#4.0中最大的变化之一,如果情况并非如此,我会非常惊讶。这并不意味着您应该立即开始用动态编写所有代码 - 只需要知道它何时可能有用。
The variance and optional/named argument changes are appreciated too, of course - but make a much shorter demo.
当然,方差和可选/命名参数更改也很受欢迎 - 但是要做一个更短的演示。
Historically, Office (in particular) has been such a pig to program against that this is a "big thing* for people who automate Office.
从历史上看,办公室(特别是)一直是一个反对计划的猪,这对于自动化Office的人来说是一件“重要的事情”。
- people who consume COM (and Office especially)
- people who consume DLR types (IronPython etc)
- people who talk to dynamic systems such as javascript (Silverlight etc)
消费COM的人(特别是Office)
消费DLR类型的人(IronPython等)
与动态系统交谈的人,如javascript(Silverlight等)
Personally, I don't expect dynamic
to revolutionise the way I program in C#, but it is big for some people:
就个人而言,我不希望动态改变我在C#中编程的方式,但对于某些人来说这很重要:
#3
Actually this is because Office com library was created with Visual Basic in mind.
实际上这是因为Office com库是用Visual Basic创建的。
Even more you can think that entire object hierarchy was created for VB (plain VB without .net). And VB historically created such way that it is simple to use IDispatch'able com interfaces from it (using late binding).
您甚至可以认为整个对象层次结构是为VB创建的(没有.net的普通VB)。 VB历史上创造了这样的方式,即使用IDispatch'able com接口很简单(使用后期绑定)。
And what we have now it's a burden of backward compatibility.
而我们现在拥有的是向后兼容的负担。
#1
The COM library exposes this as a variant, which could mean any number of things. It's really the Office library's "fault" for doing so many things with variants.
COM库将此作为变体公开,这可能意味着任何数量的事情。对于使用变体做这么多事情,这真的是Office库的“错误”。
The dynamic
type is the nearest .NET equivalent to variant (now it exists) but the team didn't want to change tlbimp
to generate dynamic types in PIAs for backward compatibility reasons. You'll only get the "variant to dynamic" conversion in C# 4 when linking a PIA (building the bits you use into your own assembly) instead of referencing it.
动态类型是最接近变体的.NET(现在它存在),但团队不希望改变tlbimp以在PIA中生成动态类型,以实现向后兼容性。在链接PIA(将您使用的位构建到您自己的程序集中)而不是引用它时,您只能在C#4中获得“变量到动态”转换。
#2
In the C# 4.0 demos, I'm seeing lots of code that uses the dynamic type.
在C#4.0演示中,我看到很多使用动态类型的代码。
Since dynamic
is one of the biggest changes in C# 4.0, I'd be hugely surprised if this wasn't the case. That doesn't mean you should immediately start writing all your code with dynamic
- simply that you should recognise when it might be useful.
由于动态是C#4.0中最大的变化之一,如果情况并非如此,我会非常惊讶。这并不意味着您应该立即开始用动态编写所有代码 - 只需要知道它何时可能有用。
The variance and optional/named argument changes are appreciated too, of course - but make a much shorter demo.
当然,方差和可选/命名参数更改也很受欢迎 - 但是要做一个更短的演示。
Historically, Office (in particular) has been such a pig to program against that this is a "big thing* for people who automate Office.
从历史上看,办公室(特别是)一直是一个反对计划的猪,这对于自动化Office的人来说是一件“重要的事情”。
- people who consume COM (and Office especially)
- people who consume DLR types (IronPython etc)
- people who talk to dynamic systems such as javascript (Silverlight etc)
消费COM的人(特别是Office)
消费DLR类型的人(IronPython等)
与动态系统交谈的人,如javascript(Silverlight等)
Personally, I don't expect dynamic
to revolutionise the way I program in C#, but it is big for some people:
就个人而言,我不希望动态改变我在C#中编程的方式,但对于某些人来说这很重要:
#3
Actually this is because Office com library was created with Visual Basic in mind.
实际上这是因为Office com库是用Visual Basic创建的。
Even more you can think that entire object hierarchy was created for VB (plain VB without .net). And VB historically created such way that it is simple to use IDispatch'able com interfaces from it (using late binding).
您甚至可以认为整个对象层次结构是为VB创建的(没有.net的普通VB)。 VB历史上创造了这样的方式,即使用IDispatch'able com接口很简单(使用后期绑定)。
And what we have now it's a burden of backward compatibility.
而我们现在拥有的是向后兼容的负担。