How do I get the qualified name from a Type instance, in Dart?

淺唱寂寞╮ 提交于 2019-12-01 00:12:22

问题


I have a instance of Type, but I want its fully qualified name. How can I do this? I know I have to use Mirrors (Dart's reflection library).


回答1:


Use the new reflectClass top-level function from dart:mirrors.

Here's an example:

import 'dart:html';
import 'dart:mirrors';

class Awesome {
  // ...
}

void main() {
  var awesome = new Awesome();
  Type type = awesome.runtimeType;
  ClassMirror mirror = reflectClass(type);
  Symbol symbol = mirror.qualifiedName;
  String qualifiedName = MirrorSystem.getName(symbol);

  query('#name').text = qualifiedName;
}

The qualifiedName should be something like:

http://127.0.0.1:3030/Users/sethladd/dart/type_name/web/type_name.dart.Awesome

Note, this works in build 21753 or higher. Also, this doesn't currently work in dart2js yet. We plan to support it in dart2js.



来源:https://stackoverflow.com/questions/16109993/how-do-i-get-the-qualified-name-from-a-type-instance-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!