如何在更多文件中的类上使用dart Reflectable?

时间:2019-02-28 16:25:34

标签: reflection dart

documentation中的示例位于一个文件中,并通过main()函数进行了初始化。如何在现实世界中使用此软件包?

我需要的是在这个简单项目中如何使用它的示例:

git@github.com:fidlip/reftest.git

lib / reflector.dart:

import 'package:reflectable/reflectable.dart';

class Reflector extends Reflectable {
  const Reflector() : super(invokingCapability);
}

const reflector = const Reflector();

lib / model_a.dart:

import 'package:untitled/reflector.dart';

@reflector
class ModelA {
  String prop;
}
void main() {}

lib / model_b.dart:

import 'package:untitled/reflector.dart';

@reflector
class ModelB {
  String prop;
}


void main() {}

bin / main.dart

import 'package:untitled/reflector.dart';

import '../lib/class_a.reflectable.dart' as a;
import '../lib/class_b.reflectable.dart' as b;


main(List<String> arguments) {
  a.initializeReflectable();
  b.initializeReflectable();

  reflector.annotatedClasses.forEach((cls) {
    print("Annotated class ${cls.qualifiedName}");
  });

}

pubspec.yaml

name: reftest
description: A sample command-line application.

environment:
  sdk: '>=2.1.0 <3.0.0'

dependencies:
  reflectable: ^2.0.10

dev_dependencies:
  pedantic: ^1.0.0
  test: ^1.0.0
  build_runner: any

build.yaml

targets:
  $default:
    builders:
      reflectable:
        generate_for:
          include:
            - lib/**.dart

构建此项目:pub run build_runner build lib

当我运行此项目时:pub run main.dart 我得到了Annotated class .B,但希望同时打印两个类(A和B)。

如果您有想法,欢迎提出请求

0 个答案:

没有答案