2012-12-21 36 views

Trả lời

6

Bạn có thể làm điều đó với mirrors API:

import 'dart:mirrors'; 

class Test { 
    method1() => "hello"; 
} 

main() { 
    print(existsFunction("main")); // true 
    print(existsFunction("main1")); // false 
    print(existsMethodOnObject(new Test(), "method1")); // true 
    print(existsMethodOnObject(new Test(), "method2")); // false 
} 

bool existsFunction(String functionName) => currentMirrorSystem().isolate 
    .rootLibrary.functions.containsKey(functionName); 

bool existsMethodOnObject(Object o, String method) => reflect(o).type.methods 
    .containsKey(method); 

existsFunction chỉ kiểm tra nếu một hàm với functionName tồn tại trong thư viện hiện nay. Do đó, với các chức năng có sẵn theo tuyên bố importexistsFunction sẽ trả lại false.

Các vấn đề liên quan