如何获得Dart中的一个类的所有字段? [英] How do I get all fields for a class in Dart?
本文介绍了如何获得Dart中的一个类的所有字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我查看了dart:mirrors库,发现 ClassMirror 。虽然我看到 getField
我没有看到所有字段。我确实看到了 getters
。
I looked at the dart:mirrors library, and I found ClassMirror. While I saw getField
I didn't see access to all fields. I did see getters
, though.
如果我想得到类的所有字段,通过 getters
?
If I want to get all fields for a class, do I have to go through getters
?
推荐答案
Zdeslav Vojkovic的回答有点老。
Zdeslav Vojkovic's answer is a bit old.
这适用于我,对于Dart 1.1.3,截至2014年3月2日。
This works for me, for Dart 1.1.3, as of March 2 2014.
import 'dart:mirrors';
class Test {
int a = 5;
static int s = 5;
final int _b = 6;
int get b => _b;
int get c => 0;
}
void main() {
Test t = new Test();
InstanceMirror instance_mirror = reflect(t);
var class_mirror = instance_mirror.type;
for (var v in class_mirror.declarations.values) {
var name = MirrorSystem.getName(v.simpleName);
if (v is VariableMirror) {
print("Variable: $name => S: ${v.isStatic}, P: ${v.isPrivate}, F: ${v.isFinal}, C: ${v.isConst}");
} else if (v is MethodMirror) {
print("Method: $name => S: ${v.isStatic}, P: ${v.isPrivate}, A: ${v.isAbstract}");
}
}
}
:
Variable: a => S: false, P: false, F: false, C: false
Variable: s => S: true, P: false, F: false, C: false
Variable: _b => S: false, P: true, F: true, C: false
Method: b => S: false, P: false, A: false
Method: c => S: false, P: false, A: false
Method: Test => S: false, P: false, A: false
这篇关于如何获得Dart中的一个类的所有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文