嵌入应用程序编译时间戳

发布于 2024-09-05 15:47:24 字数 79 浏览 3 评论 0原文

有没有一种简单的方法可以在构建过程中进行配置,以写入要在应用程序中显示的构建时间戳,以具有类似“此页面上次更新时间:06/26/2010”的内容

Is there an easy way to configure in a build process to write in the time stamp of the build to be displayed in the application to have something like "This page was last updated: 06/26/2010"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-09-12 15:47:24

一种解决方案是在构建过程中将此信息嵌入到程序集属性中。您可以使用 MSBuild 社区任务 Time 和 AssemblyInfo 任务来执行此操作:

<Time>
    <Output TaskParameter="Month" PropertyName="Month" />
    <Output TaskParameter="Day" PropertyName="Day" />
    <Output TaskParameter="Year" PropertyName="Year" />
    <Output TaskParameter="Hour" PropertyName="Hour" />
    <Output TaskParameter="Minute" PropertyName="Minute" />
    <Output TaskParameter="Second" PropertyName="Second" />
</Time>

然后

<AssemblyInfo CodeLanguage="CS"  
    OutputFile="$(MSBuildProjectDirectory)\GlobalInfo.cs" 
    AssemblyDescription="This page was last updated: $(Month)/$(Day)/$(Year)"
/>

您将在项目中包含源文件(本例中为 GlobalInfo.cs)。要在代码中访问该值,您可以使用如下内容:

public static string GetAssemblyDescription(Type t)
{
    string result = String.Empty;
    var items = t.Assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
    if (items != null && items.Length > 0)
    {
        AssemblyDescriptionAttribute attrib = (AssemblyDescriptionAttribute)items[0];
        result = attrib.Description;
    }
    return result;
}

Type t = typeof(MyClass);
string description = GetAssemblyDescription(t);
Console.WriteLine(description);

One solution would be to embed this information in an assembly attribute during your build. You can do this using the MSBuild community tasks Time and AssemblyInfo tasks:

<Time>
    <Output TaskParameter="Month" PropertyName="Month" />
    <Output TaskParameter="Day" PropertyName="Day" />
    <Output TaskParameter="Year" PropertyName="Year" />
    <Output TaskParameter="Hour" PropertyName="Hour" />
    <Output TaskParameter="Minute" PropertyName="Minute" />
    <Output TaskParameter="Second" PropertyName="Second" />
</Time>

and

<AssemblyInfo CodeLanguage="CS"  
    OutputFile="$(MSBuildProjectDirectory)\GlobalInfo.cs" 
    AssemblyDescription="This page was last updated: $(Month)/$(Day)/$(Year)"
/>

You would then include the source file in your project (GlobalInfo.cs in this example). To access this value in code you would use something like this:

public static string GetAssemblyDescription(Type t)
{
    string result = String.Empty;
    var items = t.Assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
    if (items != null && items.Length > 0)
    {
        AssemblyDescriptionAttribute attrib = (AssemblyDescriptionAttribute)items[0];
        result = attrib.Description;
    }
    return result;
}

Type t = typeof(MyClass);
string description = GetAssemblyDescription(t);
Console.WriteLine(description);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文