2010-06-15 18 views

Répondre

2

Une solution consisterait à incorporer ces informations dans un attribut d'assembly lors de la génération. Vous pouvez le faire en utilisant les tâches communautaires MSBuild Le temps et les tâches 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> 

et

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

Vous pouvez ensuite inclure le fichier source dans votre projet (GlobalInfo.cs dans cet exemple). Pour accéder à cette valeur dans le code, vous devez utiliser quelque chose comme ceci:

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);