dart - 在什么情况下,reflectClass(o.runtimeType) 会返回与reflect(o).type 不同的结果?

标签 dart dart-mirrors

来自 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 Type s 并找到它们是否相等?如上例所示,int 的 2 个独立类型不等于使用常规 ==运算符(operator)。

最佳答案

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

您可以在线程中阅读更多内容:example of wanting to override runtimeType? .

关于dart - 在什么情况下,reflectClass(o.runtimeType) 会返回与reflect(o).type 不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852736/

相关文章:

flutter - 如何在Sembast记录中的映射内监听单个值?

flutter - 在 Flutter 中将数据传递给有状态的小部件

firebase - 刷新小部件时Stream.asFuture()复制数据

function - 如何在Dart中将函数传递给监听器?

android - 如何在 Flutter 中设置间隔?

Dart:如何仅将特定类型变量的变量标识符名称转换为字符串

oop - 如何在Dart中获取类的所有子类?

reflection - 调用镜像的全局功能

class - 如何判断一个对象是否是类的实例