Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class X
- {
- public virtual void Blah() {
- Console.WriteLine("X.Blah()");
- }
- }
- class Y : X
- {
- public string message;
- public override void Blah() {
- Console.WriteLine(message);
- }
- }
- interface HasBlah
- {
- void Blah(object child);
- }
- class Demo<T> : HasBlah
- {
- public void Blah(T child) {
- child.Blah();
- }
- public override void Blah(object child) {
- Blah(child as T); // This throws an exception if child can't be cast to T
- }
- }
- void ExampleFailure() {
- X x = new X();
- Y y = new Y();
- y.message = "y.message";
- HasBlah demoX = new Demo<X>();
- HasBlah demoY = new Demo<Y>();
- demoX.Blah(x); // outputs "X.Blah()"
- demoY.Blah(y); // outputs "y.message"
- demoY.Blah(x); // throws an exception
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement