如何将代码生成合并到 C# 解决方案中?
(这个问题非常相似,但唯一的答案似乎并没有满足我的需求)。
我正在考虑在 C# 中使用 Thrift ,并且正在考虑构建过程到底如何工作。 Visual Studio 2008 中的 C# 项目是否支持生成 C# 类的自定义生成操作?
我找到了“自定义工具”选项,但我不确定这就是我正在寻找的...它只允许设计时使用(不是构建过程的组成部分,而是右键单击“运行自定义工具” )。
更新
Fionn 建议的预构建事件确实不是最理想的,因为它们没有考虑依赖关系并延长了构建过程。 此外,它们是从中央位置而不是每个文件进行管理的。
(This question is rather similar, but the only answer does not seem to be answering my needs).
I am thinking of using Thrift in C#, and am considering how exactly the build process would work. Do C# projects in Visual Studio 2008 support custom build actions that generate C# classes?
I found the "Custom Tool" option, but I'm not sure it's what I'm looking for ... it only allows design-time usage (not integral to the build process, but rather right-click "Run Custom Tool").
Update
Prebuild events that Fionn suggested are indeed suboptimal, as they don't take dependencies into account and prolong the build process. Also, they are managed from a central location instead of per-file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只是将代码生成放入预构建事件中会怎样?
它们可以在 Visual Studio 项目属性的“构建事件”部分中找到。
它们还支持一些宏,如果您使用“编辑预构建...”按钮,您将看到这些宏。
What if you simply put your code generation to the pre-build events.
They are found in the "Build Events" section of the Project Properties in Visual Studio.
Also they support some macros which you will see if you use "Edit Pre-build ..." button.
正如 Fionn 提到的,最简单、最安全的选择是将生成包含在“预构建事件”中。 优点:代码总是正确的,缺点:速度慢,因为这意味着每次编译时您都需要重新生成源代码,然后重建所有依赖项,因为生成的代码已更改。
另一种选择是手动重新生成代码文件,如果您有构建机器/持续集成,则在每次构建时重建代码文件。 优点:如果更改很少,则构建速度更快。缺点:生成的代码过时,并且浪费大量时间进行调试来解决问题。
阅读 C# 项目帮助后,您似乎需要一个能够提供您想要的自定义工具操作的插件。 但在 C++ 项目中,您可以定义自己的自定义步骤 MS-帮助。 因此,您可以开发一个 VisualStudio 插件(或找到一个)或将 C++ 项目添加到您的解决方案中,并向该项目添加自定义构建步骤,然后将输出包含在正常的 C# 项目中。
第一个 google for 'Visual Studio 自定义工具代码生成< /a>' 有一个如何编写自己的自定义工具,这将是一个很好的选择初始点。
The simplest and safest option is to include the generation in the "Pre Build Events" as Fionn mentions. Pros: code is always correct, Cons: Slow as this will mean at each compile that you re-generate the source code, and then rebuild all dependencies, as the generated code as altered.
Another option is to manually regenerate the code files, and if you have a build machine/continuous integration rebuild the code files each build. Pros: Faster builds, if there are few changes Cons: out of date generated code, and hours of wasted debugging trying to solve what's wrong.
Reading the C# project help, it seems that you need a plugin that provides the Custom Tool action you want. But in C++ projects you can define your own custom step MS-Help. So you could develop a VisualStudio plugin (or find one) or add a C++ project to your solution, and add a custom build step to that project, and then include the output in your normal C# project.
The first google for 'visual studio custom tool code gen' has a how-to write your own Custom Tool which would be a good starting point.
MS Build 具有高度可配置性。 Visual Studio 本身也非常可扩展。 您可以使用 CodeDOM 发出类。 根据您想要在自定义 VS 和构建过程中投入多少时间,几乎没有什么是您做不到的。
MS Build is highly configurable. Visual Studio itself is also very extensible. You can use the CodeDOM to emit classes. Depending on how much time you want to put into customizing VS and the build process, there is very little you cannot do.