[英]Enforcing that a Dart type implements an interface
I have a map of strings to types in Dart eg animals = <String, Type>{'cat':CatType, 'dog':DogType}
which I modify at runtime. 我在Dart中有一个字符串到类型的映射,例如,我在运行时修改的animals = <String, Type>{'cat':CatType, 'dog':DogType}
。 Is there a way to ensure that types added to this map implement a particular interface, either at compile-time or runtime? 有没有一种方法可以确保添加到此映射的类型在编译时或运行时实现特定的接口? Something like 就像是
animals = <String, Type<? implements AnimalType>>{...}
or during runtime, 或在运行时
void register(name, type):
if type implements AnimalType:
animals[name] = type
No, not really. 不,不是。
The Type
class isn't generic, so you can't have a constrain on that. Type
类不是通用的,因此您不能对此加以限制。
You can use the dart:mirrors
library to check whether a Type
object represents a class that implements another type, but it's most likely not worth the effort. 您可以使用dart:mirrors
库来检查Type
对象是否表示实现了另一种类型的类,但这很不值得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.