Dart:如何获取除类和类成员以外的任何元数据?

时间:2018-11-21 15:37:37

标签: dart metadata dart-mirrors

我正在尝试元数据。

在本文档中:

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

据说:

  

元数据可以出现在库,类,typedef,type参数,   构造函数,工厂,函数,字段,参数或变量   声明,并在导入或导出指令之前。你可以找   使用反射在运行时元数据。

因此...我试图获取元数据...

// 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”和变量来获取元数据。

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

1 个答案:

答案 0 :(得分:0)

这似乎是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,但不包括。