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:
- Add a COM reference, Right click it.
- Click properties.
- 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
精彩评论