C
Chris
I created a control that derives from the System.Windows.Forms.Control
namespace.
I then created an interface to which this new control must adhere, so that I
could require future controls to provide certain functionality.
In my form code, I'm not going to be sure of what types of controls the form
will be using until run-time. So, I created an array of controls, all cast
as Control. This all works fine, so far.
Now, in the form code, I figured out how to determine whether or not the
control I'm currently accessing from the control array implements my
interface. I did this using a call to
currentControl.GetType().GetInterface("InterfaceName"). However, because
currentControl is reference as a System.Windows.Forms.Control, rather than
as a MyControl, I can't cast to the interface.
Does anyone know a way of casting to the interface and accessing the
properties that the interface defines? Obviously, it's going to be very
counter-productive to have to create a whole set of custom controls that
merely inherit from the various Windows forms controls, just so that I can
make sure that all of the implement my interface.
The closest MSDN help article I could find is this:
<ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconwhenshouldiimplementinterfacesi
nmycomponent.htm>, and indicates code like the following:
BusinessAccount business = new BusinessAccount();
IAccount account = business;
account.PostInterest();
Here's some sample code:
using System.Windows.Forms
Control[] controlArray;
..... get controls into the array, from various sources....
if (controlArray.GetType().GetInterface( "MyInterface", true ) != null )
{
MyInterface interfaceInst = controlArray;
int propertyValue = interfaceInst.Property1;
}
Unfortunately, this code throws an InvalidCastException. Using a casting
statement (i.e. "MyInterface interfaceInst = (MyInterface)
controlArray;") doesn't work, either.
Anyone have any ideas?
Thanks,
Chris
namespace.
I then created an interface to which this new control must adhere, so that I
could require future controls to provide certain functionality.
In my form code, I'm not going to be sure of what types of controls the form
will be using until run-time. So, I created an array of controls, all cast
as Control. This all works fine, so far.
Now, in the form code, I figured out how to determine whether or not the
control I'm currently accessing from the control array implements my
interface. I did this using a call to
currentControl.GetType().GetInterface("InterfaceName"). However, because
currentControl is reference as a System.Windows.Forms.Control, rather than
as a MyControl, I can't cast to the interface.
Does anyone know a way of casting to the interface and accessing the
properties that the interface defines? Obviously, it's going to be very
counter-productive to have to create a whole set of custom controls that
merely inherit from the various Windows forms controls, just so that I can
make sure that all of the implement my interface.
The closest MSDN help article I could find is this:
<ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconwhenshouldiimplementinterfacesi
nmycomponent.htm>, and indicates code like the following:
BusinessAccount business = new BusinessAccount();
IAccount account = business;
account.PostInterest();
Here's some sample code:
using System.Windows.Forms
Control[] controlArray;
..... get controls into the array, from various sources....
if (controlArray.GetType().GetInterface( "MyInterface", true ) != null )
{
MyInterface interfaceInst = controlArray;
int propertyValue = interfaceInst.Property1;
}
Unfortunately, this code throws an InvalidCastException. Using a casting
statement (i.e. "MyInterface interfaceInst = (MyInterface)
controlArray;") doesn't work, either.
Anyone have any ideas?
Thanks,
Chris