首页 > 解决方案 > 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”和变量,我不能这样做。

关于如何获取除类或类成员以外的任何东西的元数据的任何想法?

标签: dartmetadatadart-mirrors

解决方案


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


推荐阅读