在 C# 代码中重用 .h 文件中的定义语句
我有 C++ 项目(VS2005),其中包含 #define 指令中带有版本号的头文件。 现在我需要在孪生 C# 项目中包含完全相同的数字。 最好的方法是什么?
我正在考虑将此文件作为资源包含在内,然后在运行时使用正则表达式解析它以恢复版本号,但也许有更好的方法,您认为呢?
我无法将版本移到 .h 文件之外,构建系统也依赖于它,并且 C# 项目是应该进行调整的项目。
I have C++ project (VS2005) which includes header file with version number in #define directive. Now I need to include exactly the same number in twin C# project. What is the best way to do it?
I'm thinking about including this file as a resource, then parse it at a runtime with regex to recover version number, but maybe there's a better way, what do you think?
I cannot move version outside .h file, also build system depends on it and the C# project is one which should be adapted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您始终可以使用预构建事件在 .cs 文件上运行 C 预处理器,并使用构建后事件来撤消预构建步骤。 预处理器只是一个文本替换系统,所以这是可能的:
并且预处理器将生成:
You could always use the pre-build event to run the C preprocessor on the .cs file and the post build event to undo the pre-build step. The preprocessor is just a text-substitution system, so this is possible:
and the preprocessor will generate:
您可以编写包含此 .h 文件的简单 C++/C 实用程序,并动态创建可在 C# 中使用的文件。
该实用程序可以作为 C# 项目的一部分作为预构建阶段运行。
这样您始终与原始文件同步。
You can write simple C++/C utility that include this .h file and dynamically create file that can be used in C#.
This utility can be run as a part of C# project as a pre-build stage.
This way you are always sync with the original file.
MSDN 告诉我们:
您可以使用托管 C++ 创建库,其中包含围绕常量的类包装器。 然后您可以从 C# 项目引用此类。 只是不要忘记使用 readonly <; 输入 > 而不是 const < 输入 > 作为常量声明:)
MSDN tells us:
You can create library using managed C++ that includes class - wrapper around your constants. Then you can reference this class from C# project. Just don't forget to use readonly < type > instead of const < type > for your constants declaration :)
只需几个步骤即可实现您想要的目标:
该任务接收带有标头 .h 位置的参数您提到的文件。 然后,它提取该版本并将该版本放入您之前创建的 C# 占位符文件中。 或者,如果您可以的话,您可以考虑使用通常保存版本的 AssemblyInfo.cs。
如果您需要更多信息,请随时发表评论。
You can achieve what you want in just a few steps:
The task receives a parameter with the location of the header .h file you referred. It then extracts the version and put that version in a C# placeholder file you previously have created. Or you can think using AssemblyInfo.cs that normally holds versions if that is ok for you.
If you need extra information please feel free to comment.
我编写了一个 python 脚本,它将 #define FOO“bar” 转换为 C# 中可用的内容,并且我在 C# 项目的预构建步骤中使用它。 有用。
I wrote a python script that converts #define FOO "bar" into something usable in C# and I'm using it in a pre-build step in my C# project. It works.
在 gbjbaanb 的解决方案的基础上,我创建了一个 .tt 文件,该文件查找特定目录中的所有 .h 文件,并将它们滚动到具有多个类的 .cs 文件中。
差异
Building on gbjbaanb's solution, I created a .tt file that finds all .h files in a specific directory and rolls them into a .cs file with multiple classes.
Differences
我会考虑使用 .tt 文件来处理 .h 并将其转换为 .cs 文件。 这非常简单,源文件将成为 C# 解决方案的一部分(这意味着它们将随着 .h 文件的更改而刷新),可以单击在编辑器中打开,等等。
如果您只有 1 个 #define这可能有点矫枉过正,但如果您有一个充满它们的文件(例如,也许是 mfc resources.h 文件),那么这个解决方案将成为一个巨大的胜利。
例如:创建一个文件 DefineConverter.tt 并将其添加到您的项目中,更改标记行以引用您的 .h 文件,然后您将在项目中获得一个充满静态 const 条目的新类。 (请注意,输入文件是相对于您的项目文件的,如果您想要绝对路径,请设置 hostspecic=false)。
I would consider using a .tt file to process the .h and turn it into a .cs file. Its very easy and the source files will then be part of your C# solution (meaning they will be refreshed as the .h file changes), can be clicked on to open in the editor, etc.
If you've only got 1 #define it might be a little overkill, but if you have a file full of them (eg a mfc resource.h file perhaps) then this solution becomes a big win.
eg: create a file, DefineConverter.tt and add it to your project, change the marked line to refer to your .h file, and you'll get a new class in your project full of static const entries. (note the input file is relative to your project file, set hostspecific=false if you want absolute paths).