dart - 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
s,但它没有。
推荐阅读
- javascript - 提交操作后如何拒绝我的formik表单重新渲染
- reactjs - Flyto 错误:只能在函数组件的主体内部调用 Hooks
- json - 使用文件格式解析 Snowflake 中的特定 JSON 文件时出错
- python - 沿着不同的python脚本在主要函数之间传递变量
- excel - 将单元格的内容复制/粘贴到下一个空行中的目标工作表
- php - 是否可以使用 WordPress 主题预览作为可以使用所有功能的主要网站源?
- c - 此代码是否正确存储了稍后使用的值?
- c++ - 在 C++ 登录程序中的 VS 中出现 Xstring 错误
- r - 具有多列的 Pivot_longer
- function - 在 Google Apps 脚本中循环重置函数