Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 keyword)?

like image 840
TrueWill Avatar asked Jul 14 '10 23:07

TrueWill


1 Answers

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");
like image 121
Randolpho Avatar answered Oct 19 '22 22:10

Randolpho