使用Dart中的反射从ClassMirror获取getters和/或attribuites? [英] Obtain getters and/or attribuites from ClassMirror using reflection in Dart?
问题描述
上一个版本的dart可以使用
获取getters。$ b
cm.getters.values
正如此答案中发布的:http://stackoverflow.com/a/14505025/2117440
但实际版本已移除并替换为
cm.declarations.values
最后一个代码获取所有属性,getter,setter,方法和构造函数。我想知道是否有一种方法只有getters和attributes没有其他方法。
我现在使用的代码是一个:
importdart:mirrors
class MyNestedClass {
String name;
}
class MyClass {
int i,j;
MyNestedClass myNestedClass;
int sum()=> i + j;
MyClass(this.i,this.j);
}
void main(){
MyClass myClass = new MyClass(3,5)
..myNestedClass =(new MyNestedClass luis);
print(myClass.toString());
InstanceMirror im = reflect(myClass);
ClassMirror cm = im.type;
映射< Symbol,MethodMirror> instanceMembers = cm.instanceMembers;
cm.declarations.forEach((name,declaration){
if(declaration.simpleName!= cm.simpleName)//如果不是te构造函数
print('$ {MirrorSystem.getName(name)}:$ {im.getField(name).reflectee}');
});
}
正如你在前面的代码中可以看到的,检查是否不是我需要的构造函数以将declaration.simpleName与cm.simpleName进行比较。直到我明白是无效的,因为我们比较字符串。
总之,我想知道是否有或将是一个更好的方法来解决这个问题。 / p>
也许有更好的方法,但这应该提供你需要的
< pre class =lang-dart prettyprint-override>
cm.declarations.forEach((name,declaration){
VariableMirror field;
if(declaration是VariableMirror)field = declaration ;
MethodMirror方法;
if(声明为MethodMirror)method = declaration;
if(field!= null){
print :$ {field.simpleName}');
} else if(method!= null&&!method.isConstructor){
print('method:$ {method.simpleName}');
}
});
投射到 VariableMirror
或 MethodMirror
您可以获得更多属性:
字段:
- isConst
- isFinal
- isStatic
方法:
- constructorName
- isConstructor
- isConstConstructor
- isFactoryConstructor
- isGenerativeConstructor
- isGetter
- isOperator
- isRedirecting构造函数
- isRegularMethod
- isSetter
- isStatic
- isSynthetic
Previous version of dart were able to get getters using
cm.getters.values
As is posted in this answer: http://stackoverflow.com/a/14505025/2117440
However actual version was removed that featuread and replaced by
cm.declarations.values
last code gets all attributes, getters, setters, methods and constructor. I would like to know if there is a way to get only "getters and attributes" without others method.
The code that I'm using right now is that one:
import "dart:mirrors";
class MyNestedClass {
String name;
}
class MyClass {
int i, j;
MyNestedClass myNestedClass;
int sum() => i + j;
MyClass(this.i, this.j);
}
void main() {
MyClass myClass = new MyClass(3, 5)
..myNestedClass = (new MyNestedClass()..name = "luis");
print(myClass.toString());
InstanceMirror im = reflect(myClass);
ClassMirror cm = im.type;
Map<Symbol, MethodMirror> instanceMembers = cm.instanceMembers;
cm.declarations.forEach((name, declaration) {
if(declaration.simpleName != cm.simpleName) // If is not te constructor
print('${MirrorSystem.getName(name)}:${im.getField(name).reflectee}');
});
}
As you can see in previous code to check if is not the constructor I need to compare the declaration.simpleName with cm.simpleName. Which until I understand is inefficient since we are comparing strings.
In conclusion, I would like to know if there is or will be a better way to solve this problem.
Maybe there is a better way but this should provide what you need
cm.declarations.forEach((name, declaration) {
VariableMirror field;
if(declaration is VariableMirror) field = declaration;
MethodMirror method;
if(declaration is MethodMirror) method = declaration;
if(field != null) {
print('field: ${field.simpleName}');
} else if(method != null && !method.isConstructor){
print('method: ${method.simpleName}');
}
});
After casting to VariableMirror
or MethodMirror
you can get a lot more properties:
field:
- isConst
- isFinal
- isStatic
method:
- constructorName
- isConstructor
- isConstConstructor
- isFactoryConstructor
- isGenerativeConstructor
- isGetter
- isOperator
- isRedirectingConstructor
- isRegularMethod
- isSetter
- isStatic
- isSynthetic
这篇关于使用Dart中的反射从ClassMirror获取getters和/或attribuites?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!