Click here to Skip to main content
11,637,415 members (64,868 online)
Click here to Skip to main content

Tagged as

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

, 7 Jan 2011 CPOL 17.9K 9
Rate this:
Please Sign up or sign in to vote.
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! Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Philippe Bouteleux
Software Developer (Senior)
France France
A happy .NET developper, fond of all the new bits of technologies M$ has to offer.

I code in C# using WPF, Linq, WCF and SQL Server all day long and I'm paied for that Smile | :)

You may also be interested in...

Comments and Discussions

 
QuestionInconsistent crashes in C# application which uses COM interop Pin
AmodVKulkarni14-Jun-12 2:51
memberAmodVKulkarni14-Jun-12 2:51 
AnswerRe: Inconsistent crashes in C# application which uses COM interop Pin
Philippe Bouteleux14-Jun-12 4:25
memberPhilippe Bouteleux14-Jun-12 4:25 
QuestionObject Required Pin
ttflim21-Apr-12 14:15
memberttflim21-Apr-12 14:15 
AnswerRe: Object Required Pin
Philippe Bouteleux14-Jun-12 4:31
memberPhilippe Bouteleux14-Jun-12 4:31 
Hi !

Sorry I've missed the notification of your request somewhere in the flow of my mailbox.

Just got another question and realized I missed yours. Whatever.

Not sure if it still relevant but I'd guess that it is the GetTypeByID() that returns null ?

Your COM identifier may not be correct or your COM server not correctly setup. Have you any other way to check that first?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 7 Jan 2011
Article Copyright 2011 by Philippe Bouteleux
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid