Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Get rid of COM Interop DLL by using the new C# 4 dynamic keyword

0.00/5 (No votes)
7 Jan 2011 1  
How to instantiate a COM object wrapper and call a method at runtime

Up to now, to call a COM object from managed code, you had to either:



  • make a reference to the COM instance from your Visual Studio project (which means you had to have the COM object setup on your development machine)
  • or make a reference to a precompiled assembly containing your COM interop wrapper

With the new framework .NET and C# 4 comes the dynamic keyword that allows binding to an object's method at runtime using the underlying DLR.


So to make thing short, the only thing you've got to do to interop with a COM object is to instantiate it using an activator as a dynamic variable and call a runtime bound method on it, in two lines:


dynamic comInterop= Activator.CreateInstance(Type.GetTypeFromProgID("MyCOM.Object.Name"));
var result = comInterop.MethodCall(parameter);

Hope this helps! :-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here