I have a class that extends to Control:
public foo : Control
{
//..
}
And then I get a control:
var baa = (((foo)((Control)Controls.Find(controlName, true).First()));
baa.etc = ..;
But when I do:
((PictureBox)((Control)controlImg)).MyExtensionMethod(..)
I get an exception:
Unable to cast object of type ‘System.Windows.Forms.PictureBox’ to type ‘ControlExtensions.foo’.
How to fix this exception and let me know.
Thank you.
There is no way to fix this. You class
foois correct. The error message explains it all.foodoes not inheritPictureBox. Iffoois a picture box of some sort, implement thePictureBoxclass and notControl.To give you a real life example:
The signature of
Catis different thanDogasCatdefinesCat.Meow(), whereasDogdoes not.DogdefinesDog.Bark(), whereasCatdoes not. For example, the following code with comments will help you wrap your head around this:What I am trying to show is the same thing as
a square is a rectangle, but a rectangle isn't always a square: