I have a problem when using ISynchronizeInvoke.BeginInvoke with polymorphic
parameters. It seems that if I try to call BeginInvoke and in the array of
parameters place an object that's derived from a base class my method
expects as a parameter, the method gets only an object of the type of the
base class, rather than my full object.
I know this sounds convoluted, so here's an example. My program consists of
an engine and a WindowsForms UI. The engine operates on a separate thread
and reports back to the UI using the main form's ISynchronizeInvoke
interface. When engine performs some operation during which it throws an
exception it calls OnProcessingStageException which in turn fires and event
that will be received by the UI. Here's how it looks.
public event EventHandler ProcessingStageException;
void FireEvent(EventHandler handler, Object sender, EventArgs e) {
if (this.target != null && handler != null) {
this.target.BeginInvoke(handler, new object[] {sender, e});
}
}
private void OnProcessingStageException(object sender, EventArgs e) {
FireEvent(ProcessingStageException, this, (ExceptionEventArgs)e);
}
The main form's method for handling this event is defined as follows:
private void OnProcessingStageException(object sender, EventArgs e) {
MessageBox.Show(
this, string.Format("Error performing operation.\nDetails: {0}",
((ExceptionEventArgs)e).ex), "VideoInspector");
}
The problem is that when I try to cast ((ExceptionEventArgs)e) I get
InvalidCast exception, even though ExceptionEventArgs inherits from
EventArgs.
Is this by desing or did I hit some kind of a bug?
Thanks for your help
Andrew