0

I am trying to access COM object from on the fly compiled C# code.

It's impossible to add assembly reference and "use" it by regular way, can anybody point me to dynamical COM query example?

guiding5
  • 129
  • 1
  • 10

2 Answers2

4

You can use System.Activator to load the COM object on the fly, for example:

Type t = Type.GetTypeFromProgID("CDO.Message");
object obj = Activator.CreateInstance(t);

And use Type.InvokeMember() to call the methods of the object.

Peter
  • 2,654
  • 2
  • 33
  • 44
0

I know this will work for Office COM Interop, not sure about any other libraries.

I Hope you do mean you want to use the new dynamic type with C#?

You need to use .NET4 no Pia feature:

  1. Add a COM reference, Right click it.
  2. Click properties.
  3. And there should be the option there -> Embed Interop Assembly. Make sure its TRUE.

Now, if you notice, most of the constructors and methods will have optional arguments, and certain objects (like worksheets I think) will be of type dynamic.

If you do have a "Interop Type cannot be embedded error" See this answer

Community
  • 1
  • 1
gideon
  • 19,329
  • 11
  • 72
  • 113