从文本或xml文件自动生成代码然后编译?
我想知道如何将特定格式的文本文件生成代码到 VB.net 或 C# 源文件中。 例如:我希望我的代码生成程序读取具有以下格式的文本文件:
<category1>
<subcategory>
entry1
entry2
</subcategory>
</Category1>
然后在 vb.net(或 C#)中生成代码:
Dim TreeNode1 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("entry1")
Dim TreeNode2 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("entry2")
Dim TreeNode3 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("subcategory", New System.Windows.Forms.TreeNode() {TreeNode1, TreeNode2})
这个想法是在用户修改 txt 文件并使用后编译主代码代码生成程序。我更喜欢用 C、Python 或 C# 编写代码生成程序。 我该怎么办?
I would like to know how I could generate code from a text file in a particular format into a VB.net or C# source file.
For instance: I would like my code generating program to read text file having the following format:
<category1>
<subcategory>
entry1
entry2
</subcategory>
</Category1>
And then generate code in vb.net(or C#):
Dim TreeNode1 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("entry1")
Dim TreeNode2 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("entry2")
Dim TreeNode3 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("subcategory", New System.Windows.Forms.TreeNode() {TreeNode1, TreeNode2})
The idea is to compile the main code after the user have modified the txt file and used the code generating program. I would prefer to write the code generating program in C, python or C#.
How can I go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要编写解析器来解析您的文本文件。一旦解析器生成类似于 VB.net 或 C# 代码的字符串,您可以使用
Emit
编译为临时程序集You need to write parser to parse your text file. Once parser generate string which is similar to VB.net or C# code, you can use
Emit
to compile to a temporary assembly使用 CSharpCodeProvider,设置一个字符串骨架(您的其余部分),解析您的文件,然后将生成的代码注入到正确的位置。
有关 CSharpCodeProvider 的信息
请注意,您需要在内存中构建并将构建可执行文件设置为 false。 此处 是有关如何执行此操作的示例,它还演示了如何将编译器的程序集设置为当前执行文件的程序集。
我知道这有点模糊,但如何设置实际的 CSharpCodeProvider 取决于您想要做什么。另请注意,如果您收到 FileNotFound 异常,则会出现编译错误,顶部链接提供了一个很好的解决方案,说明如何以可读的方式抛出它们。
Use the CSharpCodeProvider, set up a string skeleton that is the rest of you class, parse your file, and then inject your generated code in the correct location.
Info about the CSharpCodeProvider
Note that you will want to build in memory and set build executable to false. Here is an example on how to do that, it also shows how set the assemblies for the compiler to the assemblies of the currently executing file.
I know this is a bit vague but how you set up the actual CSharpCodeProvider depends on what you are trying to do. Also beware that if you get FileNotFound exceptions you are getting compile errors, the top link gives a nice solution on how to throw them in a readable fashion.
这取决于您最熟悉其中提到的哪种语言。我建议使用 python,因为您可以立即从命令行解释器开始使用它。
有用于 xml 解析的标准 library :
然后您可以迭代元素按名称,如下所示:
或直接针对所有元素:
下面是如何检查对象树的命令行解释器记录(
>>>
代表解释器提示符):This depends on which of mentioned languages is most familiar to you. I'd recommend to go with python, because you can start playing with it right away from command line interpreter.
There is standard library for xml parsing:
Then you iterate through elements, either by name, like this:
or straight-forward for all elements:
Below is the command line interpreter transcript of how to inspect the object tree (
>>>
stand for interpreter prompt):我不太相信这是一个 python 问题,尽管问题中有标签和倒数第二句话,但这是一个 python 答案。
I'm not really convinced this is a python question, despite the tags and penultimate sentence in the question, but here's a python answer.