开发者

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

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 keywor开发者_高级运维d)?


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");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜