Dart:如何获取除类和类成员之外的任何内容的元数据



我正在试验元数据。

本文件中:

https://www.dartlang.org/guides/language/language-tour#metadata

据说:

元数据可以出现在库、类、typedef、类型参数之前,构造函数、工厂、函数、字段、参数或变量声明和在进口或出口指令之前。您可以检索运行时使用反射的元数据。

因此。。。我尝试获取元数据。。。

// Resources:
// https://stackoverflow.com/questions/26826521/executing-bundle-of-functions-by-their-metadata-tag-in-dart-lang
import 'dart:mirrors';
/// This class represents the metadata "OnFailure".
class OnFailure {
final int criticalityLevel;
final String handlerName;
/// Constructor.
/// [criticalityLevel] represent the level of criticality.
/// [handlerName] represents the name of the function to execute.
const OnFailure(int this.criticalityLevel, String this.handlerName);
}
/// This class represents the metadata "Log".
class Log {
final String destination;
const Log(String this.destination);
}
/// This class represents the metadata "Doc".
class Doc {
final String path;
const Doc(String this.path);
}
@OnFailure(0, 'onFatalHandler')
class ClassProcessor {
@Doc('/var/doc/ClassProcessor')
bool status;
@Log('/var/log/ClassProcessor')
bool call(int value) {
return value > 0;
}
}
@OnFailure(0, 'onFatalHandler')
typedef bool TypeProcessor(int value);
main() {
ClassProcessor processorClass = ClassProcessor();
// Get the metadata for a class.
InstanceMirror instanceMirror = reflect(processorClass);
ClassMirror classMirror = instanceMirror.type;
print(instanceMirror.type.metadata); // => [InstanceMirror on Instance of 'OnFailure']
OnFailure metadata = classMirror.metadata[0].reflectee;
print("Critical level: ${metadata.criticalityLevel}"); // => Critical level: 0
print("Handler name: ${metadata.handlerName}"); // => Handler name: onFatalHandler
// Get the metadata for a method.
MethodMirror methodMirror = classMirror.declarations[Symbol('call')];
Log log = methodMirror.metadata[0].reflectee;
print("Log file is ${log.destination}");
// Get the metadata for a property.
VariableMirror variableMirror = classMirror.declarations[Symbol('status')];
Doc doc = variableMirror.metadata[0].reflectee;
print("Doc file is ${doc.path}");
// Get the metadata for a typedef.
// ???
TypeProcessor processorInstance = (int value) {
if (value > 0) {
print("That's OK.");
} else {
print("A fatal error occurred !");
}
};
instanceMirror = reflect(processorInstance);
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
// Get the metadata for a variable.
// ???
@Doc('/var/doc/data')
int data = 10;
instanceMirror = reflect(data);
print(instanceMirror.reflectee); // => 10
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
}

我可以获取类、类属性和类方法的元数据,但不能使用"typedef"和变量来获取元数据。

有没有关于如何获取除类或类成员之外的任何内容的元数据的想法?

这似乎是一个Dart错误。

var mirrorSystem = currentMirrorSystem();
var libraryMirror = mirrorSystem.libraries.keys
.where((k) => k.path.endsWith('/bin/main.dart'))
.map((k) => mirrorSystem.libraries[k])
.first;

libraryMirror.declarations应该包括typedef,但它没有。

相关内容

最新更新