tom*_*ash 6 c# c++ versioning header c-preprocessor
我有C++项目(VS2005),其中包含#define指令中版本号的头文件.现在我需要在双C#项目中包含完全相同的数字.最好的方法是什么?
我正在考虑将此文件作为资源包含在内,然后在运行时使用正则表达式解析它以恢复版本号,但也许有更好的方法,您怎么看?
我不能在.h文件之外移动版本,也建立系统取决于它,C#项目是应该适应的.
我会考虑使用.tt文件来处理.h并将其转换为.cs文件.它非常简单,源文件将成为您的C#解决方案的一部分(意味着它们将在.h文件更改时刷新),可以单击以在编辑器中打开等.
如果你只有1 #define它可能有点矫枉过正,但是如果你有一个满了它们的文件(例如mfc resource.h文件),那么这个解决方案将成为一个巨大的胜利.
例如:创建一个文件,DefineConverter.tt并将其添加到您的项目中,更改标记的行以引用您的.h文件,您将在项目中获得一个充满静态const条目的新类.(注意输入文件是相对于项目文件的,如果你想要绝对路径,则设置hostspecific = false).
<#@ template language="C#v3.5" hostspecific="True" debug="True" #>
<#@ output extension="cs" #>
<#@ assembly name="System.Core.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#
string input_file = this.Host.ResolvePath("resource.h"); <---- change this
StreamReader defines = new StreamReader(input_file);
#>
//------------------------------------------------------------------------------
// This code was generated by template for T4
// Generated at <#=DateTime.Now#>
//------------------------------------------------------------------------------
namespace Constants
{
public class <#=System.IO.Path.GetFileNameWithoutExtension(input_file)#>
{
<#
// constants definitions
while (defines.Peek() >= 0)
{
string def = defines.ReadLine();
string[] parts;
if (def.Length > 3 && def.StartsWith("#define"))
{
parts = def.Split(null as char[], StringSplitOptions.RemoveEmptyEntries);
try {
Int32 numval = Convert.ToInt32(parts[2]);
#>
public static const int <#=parts[1]#> = <#=parts[2]#>;
<#
}
catch (FormatException e) {
#>
public static const string <#=parts[1]#> = "<#=parts[2]#>";
<#
}
}
} #>
}
}
Run Code Online (Sandbox Code Playgroud)
只需几个步骤即可实现您想要的目标:
该任务接收一个参数,其中包含您引用的标头 .h 文件的位置。然后,它提取该版本并将该版本放入您之前创建的 C# 占位符文件中。或者,如果您可以的话,您可以考虑使用通常保存版本的 AssemblyInfo.cs。
如果您需要更多信息,请随时发表评论。