I am coding a builder of Dart code, in Dart, to create a client lib to be updated dynamically - cause the server is providing an API which is updated very frequently. The Dart libs (both builder and the built client API) shall be published, so the code quality must be satisfactory.
Anyway, the API can be accessed through sockets, encoded in a protobuf message, fine. The builder can then easily create classes, methods, etc. using dart:mirrors Then, on the client side, once the the API is built, messages are received containing actual data. But when an enumeration is sent, the client has to build a Dart enum instance out of the enum name as String, and an integer as index.
Here is how I did that (typeName is the enum name as String, indexNumber is the index as int):
var classSymbol = MirrorSystem.getSymbol(typeName);
var classMirror = library.declarations[classSymbol];
var symbolName = classMirror.declarations.keys.elementAt(indexNumber + 3); // Very dirty.
return classMirror.getField(symbolName).reflectee;
The indexNumber + 3 is because in the ClassMirror declarations, there are 3 Symbols before the enumeration values when the reflection is on an enum.
I couldn't find a cleaner way to make it working. If anyone has an idea...
Aucun commentaire:
Enregistrer un commentaire