W
walt.stoneburner
Why does aliasing an interface fail at runtime?
SAMPLE WORKING CODE
...
IHTMLDocument2 doc = (IHTMLDocument2) browser.Document;
...
BROKEN CODE
public interface IFooBar : IHTMLDocument2 { }
...
IFooBar doc = (IFooBar) browser.Document;
...
The broken code example compiles with no errors or warnings, but causes
an System.InvalidCastException exception at runtime, with a more
detailed message of "Specified cast is not valid."
What I don't get is that if my IFooBar interface inheirts from an
interface that works and contains no other additional method
signatures, then in theory the interfaces should be identical. Yet,
clearly it's failing performing the cast.
What's _really_ going on behind the scenes that explains why C# is
doing this?
Thanks,
-Walt Stoneburner
SAMPLE WORKING CODE
...
IHTMLDocument2 doc = (IHTMLDocument2) browser.Document;
...
BROKEN CODE
public interface IFooBar : IHTMLDocument2 { }
...
IFooBar doc = (IFooBar) browser.Document;
...
The broken code example compiles with no errors or warnings, but causes
an System.InvalidCastException exception at runtime, with a more
detailed message of "Specified cast is not valid."
What I don't get is that if my IFooBar interface inheirts from an
interface that works and contains no other additional method
signatures, then in theory the interfaces should be identical. Yet,
clearly it's failing performing the cast.
What's _really_ going on behind the scenes that explains why C# is
doing this?
Thanks,
-Walt Stoneburner