2012-11-03 12 views

Odpowiedz

3

Oto prosty kopiowaniem pasteable przykładowy kod:

import 'dart:mirrors'; 
import 'dart:io'; 

main() { 
    var im = reflect(new File('test')); // Retrieve the InstanceMirror of some class instance. 
    im.type.methods.values.forEach((MethodMirror method) => print(method.simpleName)); 
} 

wyjściowa wynosi:

existsSync 
_delete 
exists 
directory 
_getDecodedLines 
readAsTextSync 
readAsBytesSync 
readAsLinesSync 
_directory 
throwIfError 
lastModifiedSync 
readAsLines 
open 
_ensureFileService 
deleteSync 
delete 
_exists 
length 
openInputStream 
create 
_create 
readAsText 
_openStdioSync 
openOutputStream 
_fullPath 
_lastModified 
fullPathSync 
readAsBytes 
lastModified 
_openStdio 
_open 
openSync 
lengthSync 
directorySync 
fullPath 
createSync 
_lengthFromName 
+1

ClassMirror nie zawiera już właściwości "metody". Teraz ma "instanceMembers" i "declarationations". Czy "instanceMembers" będzie równoznaczne ze starą właściwością "methods"? – ringstaff

1

Oto prosty przykład:

(Uwaga: Będziemy chcieli mieć (bardzo) do aktualnej wersji SDK dla tego, to było zrobione w wersji Dart Edytor 0.2.1_r14167, wersja Dart SDK 0.2.1.2_r14167 2 listopada 2012)

Moje najszczersze podziękowania dla Gilada z zespołu Google Dart za dostarczenie tego przykładu!

#import('dart:mirrors'); 

class MyClass { 
    String _test; 

    String get test  => _test; 
    set  test(String paramVal) => _test = paramVal; 

    void my_method() { 
    } 

    void print_test(){ 
    print("test string is: ${_test}"); 
    } 

    MyClass(String test) { 
    _test = test; 
    } 

} 


main() { 

    MyClass myClass = new MyClass("Make my day, PUNK."); 

    myClass.print_test(); 

    //ClassMirror myClassMirror = reflect(myClass).type; 

    InstanceMirror myClassInstanceMirror = reflect(myClass); 

    ClassMirror MyClassMirror = myClassInstanceMirror.type; 

    Map<String, MethodMirror> map = MyClassMirror.methods; 

    print("map = ${map}"); 

    map.values.forEach((MethodMirror mm){ 
    myClassInstanceMirror.invoke(mm.simpleName,[]); 
    }); 

} 
0

chodzi Reflection Właśnie napisałem parę „Funkcje pomocnicze” do pobierania listę nazw metod (a nie MAP) i wywołanie metody ... wszystko co mogę powiedzieć na pewno, że działa teraz. Istnieją prawdopodobnie techniczne powody, aby nie robić tego w ten sposób - ale w moim przypadku nie działa to w złożonym środowisku. Sądzę jednak, że ładnie maskują wiele szczegółów, z którymi nie wszyscy będą chcieli sobie poradzić.

Oto funkcjonującej demonstrację zi bez funkcji pomocniczych:

#import('dart:mirrors'); 

class MyClass { 
    String _test; 
    String get test    => _test; 
    set  test(String paramVal) => _test = paramVal; 
    void my_method1(){print("in method1");} 
    void my_method2(){print("in method2");} 
    void print_test(){ 
    print("test string is: ${_test}"); 
    } 
    MyClass(String test) { 
    _test = test; 
    } 
} 

//Helper Methods 
InstanceMirror hMr; 
List REFLECT_methods(Object myClass) {hMr=reflect(myClass);return(hMr.type.methods.values);} 
REFLECT_invoke(MethodMirror mm){hMr.invoke(mm.simpleName, []);} 

main() { 

    MyClass myClass = new MyClass("Make my day, PUNK."); 

    print("\n=======Using Raw Reflection================"); 
    InstanceMirror myClassInstanceMirror = reflect(myClass); 
    ClassMirror MyClassMirror = myClassInstanceMirror.type; 
    Map<String, MethodMirror> map1 = MyClassMirror.methods; 
    map1.values.forEach((MethodMirror mm){ 
    myClassInstanceMirror.invoke(mm.simpleName,[]); 
    }); 

    print("\n==========Using Helper functions============="); 
    List list2 = REFLECT_methods(myClass); 
    list2.forEach((method){ 
    REFLECT_invoke(method); 
    }); 
}