Polymorphism Through Extension Methods?

后端 未结 5 1055
有刺的猬
有刺的猬 2021-02-20 05:21

I have a class library which contain some base classes and others that are derived from them. In this class library, I\'m taking advantage of polymorphism to do what I want it t

5条回答
  •  [愿得一人]
    2021-02-20 05:40

    No, that won't work.

    Extension methods are statically dispatched, using the same mechanism as overload resolution.

    If you have a variable of compile-time type Base, the compiler will always call the base extension method, regardless of the runtime type.

    Instead, you can make the base extension method check the runtime type and call the appropriate other extension method.

提交回复
热议问题