2014-02-18 1 views
6

dartdocsInstanceMirror.typereflectClass (o.runtimeType)이 reflect (o) .type과 다른 결과를 반환하는 상황은 무엇입니까? 발

"는 reflectee 실제 클래스에 거울을 반환 reflectee의 클래스는 reflectee 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들 비교가 동일한 경우 찾는가? 위의 예 에서처럼 int의 두 가지 유형이 일반 == 연산자를 사용하여 동일하지 않음을 보여줍니다.

답변

4

Alexandre가 말한 것은 String 유형입니다. dart : core에서 String 클래스는 runtimeType을 대체하여 항상 기본 클래스 유형 String을 반환합니다. 그러나 VM에는 String에 대해 여러 가지 런타임 부속 유형이 있습니다. 단일 문자 스트링, 아스키 스트링, utf8 스트링. 미러 API는 실제 기본 하위 유형을 반환합니다.

Gilad (API의 디자이너) answer (이) 질문을 참조하십시오.

"어쨌든 2 가지 유형을 비교하고 그 둘이 같은지 확인하고 있습니까?"

거울을 사용할 때 유형을 비교할 수 있습니다 세 가지 새로운 방법 coming soon 있습니다 TypeMirror.isSubtypeOf, TypeMirror.isAssignableTo, ClassMirror.isSubclassOf가.