簡體   English   中英

嵌入應用程序編譯時間戳

[英]Embed application compilation time stamp

有沒有一種簡單的方法可以在構建過程中進行配置,以寫入要在應用程序中顯示的構建時間戳記,使其具有類似“此頁面的最新更新時間:06/26/2010”的信息

一種解決方案是在構建過程中將此信息嵌入到程序集屬性中。 您可以使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM