本文介绍了使用dart:mirrors反射检索getter值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码(简化),它使用反射来迭代类的字段和getter并输出值。 $ b>

$ b>

$
$ b

以下Dart脚本(使用build 17463)提供以下输出:



应用脚本 p>

  import'dart:mirrors' 

class ContainsGetter {//引发一个错误
String get aGetter => 我是吸气者;
}

class ContainsField {//工作正常
String aField =我是字段;
}

void main(){
printFieldValues(reflect(new ContainsField()));
printGetterValues(reflect(new ContainsGetter()));
}

void printFieldValues(instanceMirror){
var classMirror = instanceMirror.type;
classMirror.variables.keys.forEach((key){
var futureField = instanceMirror.getField(key); //< - works ok
futureField.then((imField)= > print(Field:$ key = $ {imField.reflectee}));
});
}

void printGetterValues(instanceMirror){
var classMirror = instanceMirror.type;
classMirror.getters.keys.forEach((key){
var futureValue = instanceMirror.invoke(key,[]); //< - failed
futureValue.then((imValue )=> print(Field:$ key = $ {imValue.reflectee}));
});
}

输出



字段:aField =我是字段
未捕获错误:在镜像执行期间的编译时错误:< Dart_Invoke:没有找到实例方法'ContainsGetter .aGetter'。
堆栈跟踪:
#0 _LocalObjectMirrorImpl._invoke(dart:mirrors-patch:163:3)
#1 _LocalObjectMirrorImpl.invoke(dart:mirrors-patch:125:33)

(可接受的可能是此位尚未写入!)

解决方案

Aah,我刚刚解决了。虽然 aGetter 在实现中类似于一个方法,但您可以使用 getField()而不是调用以检索其值。

  void printGetterValues(instanceMirror){ 
var classMirror = instanceMirror.type;
classMirror.getters.keys.forEach((key){
var futureValue = instanceMirror.getField(key); //< - now works ok
futureValue.then((imValue) => print(Field:$ key = $ {imValue.reflectee}));
});
}


I have the following code (simplified), that uses reflection to iterate a class's fields and getters and output the values. The ContainsGetter class contains a getter, and the ContainsField class contains a simple field.

Using dart:mirrors library, I can get the value of the field by using instanceMirror.getField(fieldName)), but not the getter by using instanceMirror.invoke(fieldName,[]).

The following Dart script (using the build 17463) gives the output below:

app script

import 'dart:mirrors';

class ContainsGetter { // raises an error
  String get aGetter => "I am a getter";
}

class ContainsField { // works fine
  String aField = "I am a field";
}

void main() {
  printFieldValues(reflect(new ContainsField()));
  printGetterValues(reflect(new ContainsGetter()));
}

void printFieldValues(instanceMirror) {
  var classMirror = instanceMirror.type;
  classMirror.variables.keys.forEach((key) {
    var futureField = instanceMirror.getField(key); // <-- works ok
    futureField.then((imField) => print("Field: $key=${imField.reflectee}"));
  });
}

void printGetterValues(instanceMirror) {
  var classMirror = instanceMirror.type;
  classMirror.getters.keys.forEach((key) {
    var futureValue = instanceMirror.invoke(key,[]); // <-- fails
    futureValue.then((imValue) => print("Field: $key=${imValue.reflectee}"));
  });
}

output

Field: aField=I am a field
Uncaught Error: Compile-time error during mirrored execution: <Dart_Invoke: did not find instance method 'ContainsGetter.aGetter'.>
Stack Trace:
#0      _LocalObjectMirrorImpl._invoke (dart:mirrors-patch:163:3)
#1      _LocalObjectMirrorImpl.invoke (dart:mirrors-patch:125:33)

(An acceptable could be that "this bit just hasn't been written yet!")

解决方案

Aah, I've just worked it out. Although aGetter is like a method in its implementation, you use the getField() rather than invoke to retrieve its value.

void printGetterValues(instanceMirror) {
  var classMirror = instanceMirror.type;
  classMirror.getters.keys.forEach((key) {
    var futureValue = instanceMirror.getField(key); // <-- now works ok
    futureValue.then((imValue) => print("Field: $key=${imValue.reflectee}"));
  });
}

这篇关于使用dart:mirrors反射检索getter值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:48