Reputation: 846
In my application i need to load the com object dynamically. I implemented this by loading com by program id. It succeeded, also i could able to access methods and properties of dynamically loaded com. But in the case of event handling some problems occurred. Please find the following code
dynamic ocx = m_axCtrl.GetOcx(); // ocx dynamic loading
ocx.method1();//success
ocx.Event1+=new EventHandler<object>(EventHandler1);
ocx.Event2+=new EventHandler<object>(EventHandler2);
public void EventHandler1(object sender , object e) // e is type of class1
{
}
public void EventHandler2(object sender , object e) // e is type of class2
{
}
public class class1
{
public string arg1;
public string arg2;
}
public class class2
{
public string arg1;
public string arg2;
public string arg3;
public string arg4;
public string arg5;
public string arg6;
}
Here my first event will fire and the last won't fire.I think it is due to the mis match of event arguments. What are the things needs to handle when creating event handlers of above types. Please help me.
Upvotes: 4
Views: 1050
Reputation: 116097
Read this article on MSDN: How to handle events raised by a COM source.
The following snippets are relevant to your question:
COM interop generates the necessary delegates in metadata that you include in your managed client. An imported delegate signature comprises the sink event interface, an underscore, the event name, and the word EventHandler: SinkEventInterface_EventNameEventHandler.
and:
You can use a metadata browser, such as the MSIL Disassembler (Ildasm.exe), to identify events delegates.
Note that you need to use Ildasm on the interop assembly, not the COM library!
This article may come in helpful too: Troubleshooting .NET interoperability.
Upvotes: 1