Why do COM libraries used from C# 4.0 require such heavy use of dynamic types?

强颜欢笑 提交于 2019-12-04 12:19:13

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.

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.

In the C# 4.0 demos, I'm seeing lots of code that uses the dynamic type.

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.

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.

  • 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)

Personally, I don't expect dynamic to revolutionise the way I program in C#, but it is big for some people:

Actually this is because Office com library was created with Visual Basic in mind.

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).

And what we have now it's a burden of backward compatibility.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!