Is there a better alternative than this to 'switch on type'?

后端 未结 30 2861
梦毁少年i
梦毁少年i 2020-11-22 03:28

Seeing as C# can\'t switch on a Type (which I gather wasn\'t added as a special case because is relationships mean that more than one distinct

30条回答
  •  一个人的身影
    2020-11-22 04:14

    Create a superclass (S) and make A and B inherit from it. Then declare an abstract method on S that every subclass needs to implement.

    Doing this the "foo" method can also change its signature to Foo(S o), making it type safe, and you don't need to throw that ugly exception.

提交回复
热议问题