G
Guest
I think it would be highly intuitive to use interfaces in the following manne
inteface IFo
void Invalidate()
class FooBa
System.Windows.Forms.Form winFrm
IFoo theFoo
...
theFoo = winFrm
Obviously, Forms do not implement the IFoo interface. However, they do implement all methods required by that interface, so why does the above code throw an invalid cast exception at runtime? At the very least I would think we could do explicit casts and let it go from there. To me, an IFoo object should mean any object with an .Invalidate method.
This would really save a lot of casting in my code if I could define my own interfaces then cast .NET types to these interfaces. For example, I have a component that is passed either a Sys.Win.Form or Sys.Win.Control. In the component itself, I only need to access the .Handle property and the .Invalidate method (both Form and Control types have both .Handle and .Invalidate()). Since I only need to be passed an object with a .Handle property and a .Invalidate method, my component could just store an IFoo interface type that requires a .Handle and a .Invalidate(). But alas, .NET won't let me do this, so I am forced to store 2 variables, a Form and a Control, then use only the one passed to my component
Can someone shed some light on why it is designed this way?
inteface IFo
void Invalidate()
class FooBa
System.Windows.Forms.Form winFrm
IFoo theFoo
...
theFoo = winFrm
Obviously, Forms do not implement the IFoo interface. However, they do implement all methods required by that interface, so why does the above code throw an invalid cast exception at runtime? At the very least I would think we could do explicit casts and let it go from there. To me, an IFoo object should mean any object with an .Invalidate method.
This would really save a lot of casting in my code if I could define my own interfaces then cast .NET types to these interfaces. For example, I have a component that is passed either a Sys.Win.Form or Sys.Win.Control. In the component itself, I only need to access the .Handle property and the .Invalidate method (both Form and Control types have both .Handle and .Invalidate()). Since I only need to be passed an object with a .Handle property and a .Invalidate method, my component could just store an IFoo interface type that requires a .Handle and a .Invalidate(). But alas, .NET won't let me do this, so I am forced to store 2 variables, a Form and a Control, then use only the one passed to my component
Can someone shed some light on why it is designed this way?