dart - Dart 2: How to access a class's type?
问题描述
In Swift 4, I used to do something like this to locally store objects of variable classes:
class Repo {
var mediaType : MyBaseClass.Type
func doSomething() {
mediaType.someStaticMethod();
}
}
class SpecificClass : MyBaseClass {
static func someStaticMethod() -> void {
// Stuff
}
}
repo = Repo(SpecificClass.self)
repo.doSomething(); // Executes `Stuff`
Moving to Dart 2, this is the closest I've gotten, yet the error specified at the bottom is blocking me.
class Repo {
Type mediaType;
void doSomething() {
mediaType.someStaticMethod();
}
}
class SpecificClass extends MyBaseClass {
static void someStaticMethod() {
// Whatever
}
}
repo = Repo(SpecificClass)
repo.doSomething() // Should execute `Whatever`, but for the error
Which generates this error:
The method 'someStaticMethod' isn't defined for the class 'Type'
Is this sort of trick feasible with Dart 2?
解决方案
推荐阅读
- sapui5 - sap.m.MessageBox.confirm 在页面刷新时显示未定义 [F5]
- r - R中二进制运算符的非数字参数,用于从CSV读取的数据
- ssl - SSL23_GET_SERVER_HELLO:未知协议
- odata - 将增强的 SAPUI5 代码重新上传到 ABAP 存储库
- mysql - 连接数据库时出错:错误:握手不活动超时
- php - 具有多个表连接和多个连接字段的 Laravel 查询
- mediawiki - MinervaNeue 不加载文本格式
- java - 不同Android版本下Java中的AES CTR加密
- maxima - Maxima:如何获得关于向量或矩阵的导数?
- android - 标签下载属性不适用于移动设备