C# 代码生成器/格式化程序
有谁知道有助于生成 C# 代码的库吗?例如,如果我需要生成一个包含类定义的 *.cs 文件,我希望能够使用对象树(类似于表达式树)指定类和方法体,然后告诉库给我格式化的 C# 代码作为字符串。
谢谢。
Does anyone know of a library which helps with generating C# code? For example, if I need to generate a *.cs file containing the definition of a class, I'd like to be able to specify the class and method bodies using an object tree (similar to expression trees) and then tell the library to give me well the formatted C# code as a string.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否查看过
Microsoft.CSharp.CSharpCodeProvider
?Have you looked at
Microsoft.CSharp.CSharpCodeProvider
?用于代码生成的工具有很多,如下所列:
Code Smith
codesmithtools dot com
codegeneratorpro dot com
等
代码生成器可能听起来可以节省大量时间,实际上这取决于您所从事的项目类型。
查看 System.CodeDom 和 CSharpCodeProvider。
There are many tools for code generation and they are listed below:
Code Smith
codesmithtools dot com
codegeneratorpro dot com
etc
Code generators may sound like saving a lot of time, in reality it depends on the kind of project your working on.
Look into System.CodeDom and CSharpCodeProvider.
我相当喜欢 Terance Parr 的 StringTemplate。它是编译器构建工具 ANTLR 的核心 - StringTemplate 负责代码生成,允许 ANTLR 只针对关于其编译器的任何语言。
您可以从 http://www.stringtemplate.org/download.html< 下载最新的 C#/.Net 端口/a>
您可以在 Parr 博士的这些论文中阅读有关字符串模板的信息:
CodeProject 也有一篇关于使用 StringTemplate 生成代码的文章:http://www .codeproject.com/KB/codegen/DotNetCodeGeneration.aspx
I'm rather fond of Terance Parr's StringTemplate. It's at the core of the compiler building tool ANTLR — StringTemplate is responsible for for code generation, allowing ANTLR to target just about any language for its compilers.
You can download the latest C#/.Net port from http://www.stringtemplate.org/download.html
You can read about string template in these papers by Dr. Parr:
CodeProject has an article on code generation with StringTemplate as well: http://www.codeproject.com/KB/codegen/DotNetCodeGeneration.aspx