动态代码生成
我目前正在开发一个应用程序,您可以用它创建“程序”而无需编写源代码,如果您愿意,只需单击并播放即可。
现在的问题是如何从我的数据模型生成可执行程序。有很多种可能性,但我不确定哪一种最适合我。我需要生成包含类和命名空间以及可以成为应用程序一部分的所有内容的程序集。
CodeDOM 类:我听说这个类有很多限制和错误。我需要在方法参数和返回值上创建属性。支持吗?
以编程方式创建 C# 源代码,然后对其调用 CompileAssemblyFromFile:这会起作用,因为我可以生成我想要的任何代码,并且 C# 支持大多数 CLR 功能。但这会不会很慢?
使用反射 ILGenerator 类:我认为通过它我可以生成所有可能的 .NET 代码。但我认为这比其他方法更复杂且更容易出错?
还有其他可能的解决方案吗?
编辑: 该工具是用于开发应用程序的通用工具,不限于特定领域。我不知道它是否可以被视为可视化编程语言。用户可以创建类、方法、方法调用、各种表达式。它不会有非常的限制,因为您应该能够做真实编程语言允许的大多数事情。 目前,许多内容仍必须由用户以文本形式编写,但最终的目标是,几乎所有内容都可以一起单击。
I am currently developing an application where you can create "programs" with it without writing source code, just click&play if you like.
Now the question is how do I generate an executable program from my data model. There are many possibilities but I am not sure which one is the best for me. I need to generate assemblies with classes and namespace and everything which can be part of the application.
CodeDOM class: I heard of lots of limitations and bugs of this class. I need to create attributes on method parameters and return values. Is this supported?
Create C# source code programmatically and then call CompileAssemblyFromFile on it: This would work since I can generate any code I want and C# supports most CLR features. But wouldn't this be slow?
Use the reflection ILGenerator class: I think with this I can generate every possible .NET code. But I think this is much more complicated and error prone than the other approaches?
Are there other possible solutions?
EDIT:
The tool is general for developing applications, it is not restricted to a specific domain. I don't know if it can be considered a visual programming language. The user can create classes, methods, method calls, all kinds of expressions. It won't be very limitating because you should be able to do most things which are allowed in real programming languages.
At the moment lots of things must still be written by the user as text, but the goal at the end is, that nearly everything can be clicked together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您我发现查看 动态语言运行时 是值得的,它或多或少是为创建高基于.NET 的级别语言。
也许还值得看看之前的一些域特定语言上的堆栈溢出线程< /a> 其中包含一些用于使用 DSL 的有用工具的链接,这听起来有点像您正在计划的内容,尽管我仍然不太清楚您的目标到底是什么。
You my find it is rewarding to look at the Dynamic Language Runtime which is more or less designed for creating high-level languages based on .NET.
It's perhaps also worth looking at some of the previous Stack Overflow threads on Domain Specific Languages which contain some useful links to tools for working with DSLs, which sounds a little like what you are planning although I'm still not absolutely clear from the question what exactly your aim is.
大多数“点击即玩”的事情应该足够简单,只需将一些预定义的构建块对象粘在一起(可能使用边界上的接口)。含义:您可能不需要进行动态代码生成 - 只需“伪造”即可。例如,使用属性包对象(例如
DataTable
等,尽管这不是我的第一选择)作为值等。动态评估的另一个选项是
表达式
类;特别是在 .NET 4.0 中,它的用途非常广泛,并且允许编译为委托。Most things "click and play" should be simple enough just to stick some pre-defined building-block objects together (probably using interfaces on the boundaries). Meaning: you might not need to do dynamic code generation - just "fake it". For example, using property-bag objects (like
DataTable
etc, although that isn't my first choice) for values, etc.Another option for dynamic evaluation is the
Expression
class; especially in .NET 4.0, this is hugely versatile, and allows compilation to a delegate.进行 C# 源代码生成,在速度重要之前不要关心速度。 C# 编译器速度相当快。
Do the C# source generation and don't care about speed until it matters. The C# compiler is quite quick.
当我编写动态代码生成器时,我严重依赖 System.Reflection.Emit。
基本上,您以编程方式创建动态程序集并向其中添加新类型。这些类型是使用 Emit 构造(属性、事件、字段等)构建的。在实现方法时,您必须使用 ILGenerator 将 MSIL 操作码提取到您的方法中。这听起来非常可怕,但您可以使用一些工具来提供帮助:
When I wrote a dynamic code generator, I relied heavily on System.Reflection.Emit.
Basically, you programatically create dynamic assemblies and add new types to them. These types are constructed using the Emit constructs (properties, events, fields, etc..). When it comes to implementing methods, you'll have to use an ILGenerator to pump out MSIL op-codes into your method. That sounds super scary, but you can use a couple of tools to help:
这取决于您的要求,CodeDOM 肯定最适合将其存储在“数据模型”中的“程序”。
然而,与任何其他方法相比,使用选项 2 不太可能明显变慢。
It depends on your requirements, CodeDOM would certainly be the best fit for a "program" stored it in a "data model".
However its unlikely that using option 2 will be in any way measurably slower in comparision with any other approach.
我会回应其他人的观点,1)编译器很快,2)“点击并播放”的东西应该足够简单,这样添加到一堆小部件中的任何一个小部件都不会使其成为非法的一堆。
祝你好运。我怀疑除了真正的玩具级程序之外,您是否可以实现第 (2) 点。
I would echo others in that 1) the compiler is quick, and 2) "Click and Play" things should be simple enough so that no single widget added to a pile of widgets can make it an illegal pile.
Good luck. I'm skeptical that you can achieve point (2) for anything but really toy-level programs.