【问题标题】:Dart: how to get metadata for anything other than classes and class members?Dart:如何获取除类和类成员之外的任何内容的元数据?
【发布时间】:2018-11-21 15:37:36
【问题描述】:

我正在试验元数据。

在本文档中:

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-mirrors 标签,你会发现很多例子。
  • 好吧,我已经阅读了此链接中提供的大量材料。我花了 8 个小时阅读更多内容并进行更多实验。我没有找到如何从typedef 获取元数据。我的同事也试图找出...也没有结果。文档说我们可以做到。但是,通过查看所有响应,我们了解到这是不可能的。这对我们来说太麻烦了。我们放弃了 Dart。

标签: dart metadata dart-mirrors


【解决方案1】:

这似乎是一个 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 应该包含 typedefs,但它没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2017-10-12
    • 2014-09-12
    • 2018-05-07
    相关资源
    最近更新 更多