from the dartdocs for InstanceMirror.type
:
从达特医生那里,比如西密罗。类型:
"Returns a mirror on the actual class of the reflectee. The class of the reflectee may differ from the object returned by invoking runtimeType on the reflectee."
返回reflectee的实际类的镜像。通过调用reflectee上的运行时类型,reflectee的类可能与返回的对象不同。
so under what circumstances does reflectClass(o.runtimeType)
return a different result from reflect(o).type
?
那么在什么情况下,reflectClass(o. runtimetype)返回与reflection (o).type不同的结果呢?
I have tried with example code:
我尝试过示例代码:
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
additionally is there anyway of comparing 2 Type
s and finding if they are equal? as the example above shows that the 2 seperate Types of int
aren't equal using the regular ==
operator.
另外,有没有比较两种类型和发现它们是否相等?如上例所示,使用regular == =运算符时,两个分隔的int类型并不相等。
2 个解决方案
#1
4
You will get different results when a class overrides the runtimeType
member. Basically an Object can lie on its type.
当一个类覆盖运行时类型成员时,您将得到不同的结果。基本上一个对象可以位于它的类型上。
You can read more in the thread : example of wanting to override runtimeType?.
您可以在线程中阅读更多内容:要重写运行时类型的示例?
#2
4
An example of what Alexandre said is the String type. In dart:core the String class overrides runtimeType to always return the base class type String. However in the VM there are a number of different runtime subtypes for String. i.e single character strings, ascii strings, utf8 strings. The mirror api returns the real underlying subtype.
Alexandre说的一个例子是字符串类型。在dart中:core String类重写runtimeType以总是返回基类类型字符串。然而,在VM中有许多不同的字符串运行时子类型。我。单个字符串,ascii字符串,utf8字符串。镜像api返回真正的底层子类型。
See Gilad's (the API's designer) answer to this SO question.
参见Gilad (API的设计者)对这个问题的回答。
"is there anyway of comparing 2 Types and finding if they are equal?"
“有没有比较两种类型和发现它们是否相等?”
There are three new methods coming soon that allow you to compare types when using mirrors: TypeMirror.isSubtypeOf, TypeMirror.isAssignableTo, ClassMirror.isSubclassOf.
在使用镜像时,有三种新的方法可以让您比较类型:typemirates ror。isSubtypeOf TypeMirror。isAssignableTo ClassMirror.isSubclassOf。
#1
4
You will get different results when a class overrides the runtimeType
member. Basically an Object can lie on its type.
当一个类覆盖运行时类型成员时,您将得到不同的结果。基本上一个对象可以位于它的类型上。
You can read more in the thread : example of wanting to override runtimeType?.
您可以在线程中阅读更多内容:要重写运行时类型的示例?
#2
4
An example of what Alexandre said is the String type. In dart:core the String class overrides runtimeType to always return the base class type String. However in the VM there are a number of different runtime subtypes for String. i.e single character strings, ascii strings, utf8 strings. The mirror api returns the real underlying subtype.
Alexandre说的一个例子是字符串类型。在dart中:core String类重写runtimeType以总是返回基类类型字符串。然而,在VM中有许多不同的字符串运行时子类型。我。单个字符串,ascii字符串,utf8字符串。镜像api返回真正的底层子类型。
See Gilad's (the API's designer) answer to this SO question.
参见Gilad (API的设计者)对这个问题的回答。
"is there anyway of comparing 2 Types and finding if they are equal?"
“有没有比较两种类型和发现它们是否相等?”
There are three new methods coming soon that allow you to compare types when using mirrors: TypeMirror.isSubtypeOf, TypeMirror.isAssignableTo, ClassMirror.isSubclassOf.
在使用镜像时,有三种新的方法可以让您比较类型:typemirates ror。isSubtypeOf TypeMirror。isAssignableTo ClassMirror.isSubclassOf。