[英]Enforcing that a Dart type implements an interface
我在Dart中有一个字符串到类型的映射,例如,我在运行时修改的animals = <String, Type>{'cat':CatType, 'dog':DogType}
。 有没有一种方法可以确保添加到此映射的类型在编译时或运行时实现特定的接口? 就像是
animals = <String, Type<? implements AnimalType>>{...}
或在运行时
void register(name, type):
if type implements AnimalType:
animals[name] = type
不,不是。
Type
类不是通用的,因此您不能对此加以限制。
您可以使用dart:mirrors
库来检查Type
对象是否表示实现了另一种类型的类,但这很不值得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.