开发者

Access COM dynamicaly from C#

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

It's impossible to add assembly refe开发者_运维知识库rence and "use" it by regular way, can anybody point me to dynamical COM query example?


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.


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜