来自 dartdocs为 InstanceMirror.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/