Dart meta programming features

房东的猫 提交于 2019-12-10 21:08:32

问题


Will there be an equivelent of the c# Reflection.Emit namespace in dart?

Reflection.Emit has a number of classes that are used to build types at run time and adding properties, configering their getters and setter and building methods and event handlers all at run time, which is really powerfull when it comes to metaprogramming.

my idea is about generating my data models at run time and caching them in a map so i can create instances at run time and add new methods and properties to them when i need to and not having to use mirrors often after generating the class, this could be really useful when writing ORMs and more dynamic applications where you use reflection once rather than using it every time you need to modify an instance

My questions are:

  • Will there be such thing in the future versions of dart? they mention something about a Mirror Builder but i am not sure if does the same thing, can some one please confirm if thats what a Mirror Builder is about?
  • another question is, if i am able to generate my data types on the server as strings, is there a way to to compile them before sending them to the client and map them in a Map and use this Map to create instances?

回答1:


I have seen discussions that this should be supported at some time but as far as I know will not be started to work on in the near future.

Similar requirements are usually solved by code generation at build time (Polymer, Angular, others) by transformers which analyze the code and generated code for reflective property access or code snippets in HTML.

Smoke is a package that aims to simplify this.

Code generation has the advantage that the amount of code needed to be downloaded by the client is much smaller. When you do code generation at runtime you need a compiler and that is a lot of code that needs to be downloaded into the browser.

try.dartlang.org takes a such an approach. The source is available here https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/site/try/ . It includes dart2js (built to JavaScript) and runs a background isolate that compiles the Dart code to JS.



来源:https://stackoverflow.com/questions/23759041/dart-meta-programming-features

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!