如何从 C# 中的预编译器获取当前日期时间?
在 C# 3.0 中,我有一个属性,它应该包含类的版本。 版本号只是编译的日期和时间。 现在,我有以下代码:
public DateTime Version
{
get { return DateTime.UtcNow; }
}
显然,这是错误的,因为此属性返回我当前的日期和时间。 那么,预编译器可以在编译时打印 DateTime 吗? 在这种情况下,我可以执行类似于下面的操作。
public DateTime Version
{
get { return new DateTime("PRECOMPILER DATE"); }
}
In C# 3.0, I have a property which is suppose to contain the version of the class. The version number is simply the date and time of compilation. Right now, I have the following code:
public DateTime Version
{
get { return DateTime.UtcNow; }
}
Obviously, this is wrong since this property returns me the current date and time. So, is the precompiler can print the DateTime at compile time? In this case, I could do something similar to below.
public DateTime Version
{
get { return new DateTime("PRECOMPILER DATE"); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以从 dll 本身检索它(来源:codinghorror)
You can retreive it from the dll itself (Source: codinghorror)
C#没有宏的概念; 但是,您可以在构建脚本中使用其他工具(csproj / NANT / 等)在编译之前操作源代码。 例如,我使用它来将修订号设置为当前 SVN 修订版。
一个便宜的选择是预构建事件(您可以通过 VS 中的项目属性对话框执行此操作):本质上是在构建之前运行的 bat 文件; 然后您可以编写所需的任何更改脚本。 一个更复杂的选项是构建任务。
例如,实用程序库此处包含一个
Time
任务和一个文件更新任务; (理论上)应该可以将两者链接在一起以模拟您的需要。
就我个人而言,我会使用
[AssemblyVersion]
详细信息而不是时间 - 如果您将其链接到源代码控制系统,则可以很容易地找到有问题的版本; 因此,对于我的 SVN 版本,我然后使用(在我的构建项目中):现在我的程序集版本是正确的,包括操作系统报告的文件版本。
C# doesn't have the concept of macros; however, you can use other tools in your build script (csproj / NANT / etc) to manipulate the source before it compiles. I use this, for example, to set the revision number to the current SVN revision.
A cheap option is a pre-build event (you can do this via the project properties dialog in VS): essentially a bat file that runs before build; you can then script whatever changes you need. A more sophisticated option is build tasks.
For example, the utility library here includes a
Time
task and aFileUpdate
task; it should (in theory) be possible to chain the two together to emulate what you need.Personally, I'd use the
[AssemblyVersion]
details rather than the time - if you link this to your source-control system, this makes it very easy to find the offending version; so for my SVN version, I then use (in my build proj):And now my assembly-version is correct, including the file-version that gets reported by the OS.
C# 中没有与
__TIME__
预处理器指令等效的东西,您能做的最好的事情就是获取当前程序集并获取创建日期,它将包含编译时间。 请参阅此处There's no equivalent for the
__TIME__
preprocessor directive in C#, The best you could do is get the current assembly and get the created date, it will contain the compile time. See here我认为在 C# 中没有办法做到这一点 - C++ 风格的宏定义是明确禁止的。
您可以使用程序集的上次修改日期来解决这个问题,或者可能包含一个文本文件作为嵌入资源,并添加一个将当前日期/时间写入其中的自定义构建操作。
I don't think there's a way to do this in C# - C++ style macro definitions are explictly disallowed.
You could work around it using either the last modified date of the assembly, or potentially include a text file as an embedded resource and add a custom build action that writes the current date/time into it.
没有预处理器
__TIME__
指令,但您可以使用其他内容,例如程序集的创建时间。 您还可以考虑使用源代码控制系统中的关键字替换(subversion 中的 $Date$)。There's no preprocessor
__TIME__
directive, but there's other things you could use, such as the creation time of the assembly. You might also consider using keyword substitution from your source control system ($Date$ in subversion).可以从元数据中检索 内部版本号程序集,并且可以设置为与构建日期匹配。
通常,您会在 AssemblyInfo.cs 文件中找到或创建一个条目。 当您的项目是使用 Visual Studio 创建时。 示例:
AssemblyVersionAttribute 文档的引用。 (我把一些重要的部分加粗了)
正如您所看到的,如果您可以使用此设置默认内部版本号。 默认内部版本号是每天递增的值。
然后,您可以使用 AssemblyName 检索内部版本号。版本 属性。 MSDN 提供的示例应该可以帮助您入门。
默认内部版本号是自 2000 年 1 月 1 日以来的天数。
这个问题也有关于如何使用它的清晰解释。
The Build number can be retrieved from the metadata of an assembly, and can be set to match with the date of the build.
Normally you will find, or create an entry in the AssemblyInfo.cs file. When your project is created using Visual Studio. An example:
A quote for the documentation on AssemblyVersionAttribute. (I've bolded some important parts)
As you can see, if you can set a default build number with this. The default build number is a value that is incremented every day.
You can then retrieve the Build number by using the AssemblyName.Version property. The examples provided by MSDN should get you going.
The default build number is the number of days since 1/1/2000.
This question also has a clear explanation on how to use this.
您是否正在寻找这样的东西:
(通过版本信息获取构建程序集的日期;构建号是自 2000 年 1 月 1 日以来的天数)
Do you looking for something like this:
(Get's the Date of Building the Assembly via Version-Info; Build-Number is days since 1.1.2000)