【发布时间】:2012-03-10 22:52:46
【问题描述】:
有没有办法“严格”限制或强制使用可以使用的类型。
S <: T
类似
Method( value As T ) ' Any Type of T including subtypes of T
Method( value Is T ) ' Only take a T not a subtype of T
我可以在运行时这样做
If TypeOf value Is T Then
但这是运行时检查,编译时检查会更可取
Option Strict On
这仅将其限制为(隐式)类型扩展 Coerecions。
我希望所有类型强制都必须是显式的。
有可能吗?如果有怎么办?
【问题讨论】:
-
在子类型不能有效替代基本类型(通常违反Liskov Substitution Principle)的情况下,您在做什么?您的场景可能适合Visitor 等模式。
标签: .net types type-coercion