How do you instantiate an IDispatchEx in C#?
this is my first SO question! I would like to instantiate a COM object and cast it to IDispatchEx so that I can enumerate its members. Here is an example:
Type _COMType = System.Type.GetTypeFromProgID("Scripting.FileSystemObject"); var _COMObject = (IDispatchEx)Activator.CreateIn开发者_StackOverflowstance(_COMType);
My IDispatchEx is identical to the one on this website (not my website), except that GetNextDispID and GetMemberName return an int (which I wish to use for HRESULT as described on MSDN).
The example above does not work. Is there any way to instantiate COM objects as you would from Active Scripting cast to the IDispatchEx interface?
Thanks for any and all help/suggestions!
This operation failed because the QueryInterface call on the COM component for the interface with IID '{A6EF9860-C720-11D0-9337-00A0C90DCAA9}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
The exception message you get is as clear as a bell, the Scripting.FileSystemObject simply doesn't implement the IDispatchEx interface. Only IDispatch. This works fine:
Type t = System.Type.GetTypeFromProgID("Scripting.FileSystemObject");
var obj = Activator.CreateInstance(t);
var iobj = (stdole.IDispatch)obj;
You're done, you cannot force a COM coclass to implement an interface. I would not expect very many COM classes to implement it, IDispatchEx is pretty obscure. It fits the JScript mold.
It seems that you will need to define this interface yourself if you wish to use it in C# (source)
This thread may be relevant - it seems that someone has found an existing implementation of IDispatchEx to use.
精彩评论