How to use dart to implement a delegate/proxy?
dart, dart-mirrors, delegates, nosuchmethod, proxy
Solution
You have to use dart:mirrors. Here's how to do :
import 'dart:mirrors';
class Parser {
noSuchMethod(Invocation invocation) {
ClassMirror cm = reflectClass(Proxy);
return cm.invoke(invocation.memberName
, invocation.positionalArguments
/*, invocation.namedArguments*/ // not implemented yet
).reflectee;
}
}
class Proxy {
static String hello() { return "hello"; }
static String world() { return "world"; }
}
main(){
var parser = new Parser();
print(parser.hello());
print(parser.world());
}
Problem
I have two classes `Parser` and `Proxy`, and when I invoke a method from `Parser`, which does not exist, it will delegate it to `Proxy` class. My code: ``` class Parser { noSuchMethod(Invocation invocation) { // how to pass the `invocation` to `Proxy`??? } } class Proxy { static String hello() { return "hello"; } static String world() { return "world"; } } ``` That when I write: ``` var parser = new Parser(); print(parser.hello()); ``` It will print: ``` hello ```