2014-09-04 27 views
3

我有以下幾種類型:如何區分達特種

class AddressEditor extends TextEditor {} 
class TypeEditor extends TextEditor {} 

我試圖找出編輯是這樣:如果我用鏡子

import 'dart:mirrors'; 

getTypeName(dynamic obj) 
{ 
    return reflect(obj).type.reflectedType.toString(); 
} 

void validationErrorHandler(ValidationError e) 
{ 
    var editor = e.editor; 
    if(editor is AddressEditor) 
    print(getTypeName(editor)) // prints TextEditor 

    if(editor is TypeEditor) 
    print(getTypeName(editor)) // prints TextEditor 
} 

void validationErrorHandler(ValidationError e) 
{ 
    var editor = e.editor; 
    if(editor is AddressEditor) 
    print(editor.runtimeType.toString()) // prints TextEditor 

    if(editor is TypeEditor) 
    print(editor.runtimeType.toString()) // prints TextEditor 
} 

爲什麼編輯器類型爲TypeEditorAddressEditor沒有被識別?是的,我知道要麼是TextEditor,但有什麼方法可以確定Dart中的TypeEditorAddressEditor

我需要使這些標識與驗證的結果一起工作。

感謝

+0

如果你需要編譯你r代碼轉換爲JavaScript,不要使用dart:鏡像,除非您確定輸出非常大。 – 2014-09-05 02:23:27

+0

謝謝賽斯。但我非常渴望不惜一切代價尋找解決方案。 – 2014-09-05 10:54:05

回答

4

UPDATE

事實證明,TextEditor有一個方法newInstance()這就是所謂的BWU Datagrid獲得新的編輯器實例(基本上TextEditor是一家工廠,在一個實現)。

因爲TypeEditorAddressEditor不重寫此方法,所以創建了內部純的TextEditor實例。

爲了獲得所需的行爲,您需要覆蓋newInstance並實現此方法使用的構造函數。因爲TextEditor中的構造函數是私有的,所以不能重用,需要複製(我將重新考慮這個設計)。複製構造函數的前兩行需要稍微修改一下。然後

的AddressEditor看起來像

class AddressEditor extends TextEditor { 
    AddressEditor() : super(); 

    @override 
    TextEditor newInstance(EditorArgs args) { 
    return new AddressEditor._(args); 
    } 

    AddressEditor._(EditorArgs args) { 
    this.args = args; 
    $input = new TextInputElement() 
     ..classes.add('editor-text'); 
    args.container.append($input); 
    $input 
     ..onKeyDown.listen((KeyboardEvent e) { 
     if (e.keyCode == KeyCode.LEFT || e.keyCode == KeyCode.RIGHT) { 
     e.stopImmediatePropagation(); 
     } 
    }) 
     ..focus() 
     ..select(); 
    } 
} 

TypeEditor是一樣的只是一個不同的類和構造函數名。

ORIGINAL

我敢肯定,隨着is上面的例子中工作正常,問題出在其他地方(這些值不AddressEditorsTypeEditors只是TextEditors

class TextEditor {} 

class AddressEditor extends TextEditor {} 
class TypeEditor extends TextEditor {} 

void main() { 
    check(new AddressEditor()); 
    check(new TypeEditor()); 
    check(new TextEditor()); 
} 

void check(TextEditor editor) { 
    if(editor is AddressEditor) print('AddressEditor: ${editor.runtimeType}'); 
    if(editor is TypeEditor) print('TypeEditor: ${editor.runtimeType}'); 
    if(editor is TextEditor) print('TextEditor: ${editor.runtimeType}'); 
} 

輸出

AddressEditor: AddressEditor 
TextEditor: AddressEditor 

TypeEditor: TypeEditor 
TextEditor: TypeEditor 

TextEditor: TextEditor 
+0

我想在計算機上添加指向文件的鏈接以顯示整個組件,但無法在評論中找到方法。如果你知道如何請建議相同。 – 2014-09-04 21:33:22

+0

在GitHub上創建一個Gist並添加一個鏈接到您的問題。 – 2014-09-05 04:17:02

+0

嘗試新的AddressEditor()總是拋出以下異常|||例外:在'AddressEditor'類中沒有聲明構造函數'AddressEditor'。 NoSuchMethodError:未找到方法:'AddressEditor' 接收方:類型:class'AddressEditor' 參數:[...](http:// localhost:8080/epimss_design.html.12.dart:105)|||並沒有顯示網格。 – 2014-09-05 11:51:19

相關問題