CreateObject equivalent for C# 4, dynamic keyword, and late binding?

与世无争的帅哥 提交于 2019-12-12 07:48:15

问题


How do I create a dynamic COM/OLE/ActiveX object in C# 4.0 from a program identifier or ProgID (such as "Word.Application") without referencing a library?

In C# 3.5 I'd have to write something like

Type comObjectType = Type.GetTypeFromProgID(progId, true);
Activator.CreateInstance(comObjectType);

Is there an easier way to do it in C# 4.0 so I can assign it to a variable of type dynamic (using the dynamic keyword)?


回答1:


What's wrong with

dynamic myTypeInstance = Activator.CreateInstance(Type.GetTypeFromProgID(typeName, true));

?

If it's a known type name, there's also

dynamic myTypeInstance = Activator.CreateInstance("typeName", "assemblyName");


来源:https://stackoverflow.com/questions/3251308/createobject-equivalent-for-c-sharp-4-dynamic-keyword-and-late-binding

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