6

来自dartdocsInstanceMirror.type

“在反射器的实际类上返回一个镜像。反射器的类可能与在反射器上调用 runtimeType 所返回的对象不同。”

那么在什么情况下会reflectClass(o.runtimeType)返回不同的结果reflect(o).type呢?

我尝试过使用示例代码:

import 'dart:mirrors';

class A{}

void main() {

  var string = "str";
  var boolean = true;
  var integer = 5;
  var map = {};
  var list = [];
  var custom = new A();

  var strRunT = string.runtimeType;
  var strRefT = reflect(string).type.reflectedType;
  var boolRunT = boolean.runtimeType;
  var boolRefT = reflect(boolean).type.reflectedType;
  var intRunT = integer.runtimeType;
  var intRefT = reflect(integer).type.reflectedType;
  var mapRunT = map.runtimeType;
  var mapRefT = reflect(map).type.reflectedType;
  var listRunT = list.runtimeType;
  var listRefT = reflect(list).type.reflectedType;
  var cusRunT = custom.runtimeType;
  var cusRefT = reflect(custom).type.reflectedType;

  print('string');
  print('runtimeType = $strRunT');
  print('reflectedType = $strRefT');
  print('runT == refT = ${strRunT == strRefT}');
  print('runT == String = ${strRunT == String}');
  print('refT == String = ${strRefT == String}');
  print('');
  print('bool');
  print('runtimeType = $boolRunT');
  print('reflectedType = $boolRefT');
  print('runT == refT = ${boolRunT == boolRefT}');
  print('runT == bool = ${boolRunT == bool}');
  print('refT == bool = ${boolRefT == bool}');
  print('');
  print('integer');
  print('runtimeType = $intRunT');
  print('reflectedType = $intRefT');
  print('runT == refT = ${intRunT == intRefT}');
  print('runT == int = ${intRunT == int}');
  print('refT == int = ${intRefT == int}');
  print('');
  print('map');
  print('runtimeType = $mapRunT');
  print('reflectedType = $mapRefT');
  print('runT == refT = ${mapRunT == mapRefT}');
  print('runT == Map = ${mapRunT == Map}');
  print('refT == Map = ${mapRefT == Map}');
  print('');
  print('list');
  print('runtimeType = $listRunT');
  print('reflectedType = $listRefT');
  print('runT == refT = ${listRunT == listRefT}');
  print('runT == List = ${listRunT == List}');
  print('refT == List = ${listRefT == List}');
  print('');
  print('custom');
  print('runtimeType = $cusRunT');
  print('reflectedType = $cusRefT');
  print('runT == refT = ${cusRunT == cusRefT}');
  print('runT == A = ${cusRunT == A}');
  print('refT == A = ${cusRefT == A}');
  print('');
}


//OUTPUT
string
runtimeType = String
reflectedType = String
runT == refT = false
runT == String = true
refT == String = false

bool
runtimeType = bool
reflectedType = bool
runT == refT = true
runT == bool = true
refT == bool = true

integer
runtimeType = int
reflectedType = int
runT == refT = false
runT == int = true
refT == int = false

map
runtimeType = _LinkedHashMap
reflectedType = _LinkedHashMap
runT == refT = true
runT == Map = false
refT == Map = false

list
runtimeType = List
reflectedType = List
runT == refT = true
runT == List = false
refT == List = false

custom
runtimeType = A
reflectedType = A
runT == refT = true
runT == A = true
refT == A = true

另外有没有比较 2 Types 并确定它们是否相等?如上面的示例所示,int使用常规运算符时 2 个单独的类型不相等==

4

2 回答 2

4

亚历山大所说的一个例子是字符串类型。在 dart:core 中,String 类覆盖 runtimeType 以始终返回基类类型 String。然而,在 VM 中,String 有许多不同的运行时子类型。即单字符串、ascii 字符串、utf8 字符串。镜像 api 返回真正的底层子类型。

请参阅 Gilad(API 的设计者)对这个 SO 问题的回答。

“有没有比较两种类型并找出它们是否相等?”

即将推出三种新方法,可让您在使用镜像时比较类型:TypeMirror.isSubtypeOf、TypeMirror.isAssignableTo、ClassMirror.isSubclassOf。

于 2014-02-18T23:11:19.690 回答
4

当类覆盖runtimeType成员时,您将得到不同的结果。基本上一个对象可以位于它的类型上。

您可以在线程中阅读更多内容:想要覆盖 runtimeType 的示例?.

于 2014-02-18T13:29:09.210 回答