In my MEF-based application, some modules use forms, but some do not.
I want to be able to respond to the closure of these forms, but can’t seem to figure out how to do so. Module types are based on interfaces and those that use a form inherit a base class which inherits from Windows.Forms.Form.
E.g.
public partial class SwatchForm : ModuleForm, IAcquisition
Where
public partial class ModuleForm : Form
Since not all things that inherit IAcquisition (or my other module interfaces) inherit from ModuleForm, I tried this:
if (this.AcquisitionModule.GetType().IsSubclassOf(typeof(ModuleForm)))
{
(ModuleForm)(this.AcquisitionModule).ModuleFormClosed += whatever....
}
But it still complains that IAcquisition has nothing called ModuleFormClosed.
I thought about using exceptions instead, but that feels like a bit of a hack. Is there a way to do this using events?
.has a higher precedence than a cast, so you need to place the parentheses differently. Furthermore, you can check the type in a simpler way. TryAnother way is