For my current project, I've implemented a Shared AssemblyInfo.cs file, mostly because every project is from the same company and share some common version numbers. In one of my console applications, I needed to construct a header using the Assembly Title and Copyright information from my SharedAssemblyInfo.cs file. To achieve this, I added the following to the end of SharedAssemblyInfo.cs:
namespace MyNamespace {
public static class AssemblyInfo {
public static string Title {
get {
var attr = GetAssemblyAttribute<AssemblyTitleAttribute>();
if (attr != null)
return attr.Title;
return string.Empty;
}
}
public static string Company {
get {
var attr = GetAssemblyAttribute<AssemblyCompanyAttribute>();
if (attr != null)
return attr.Company;
return string.Empty;
}
}
public static string Copyright {
get {
var attr = GetAssemblyAttribute<AssemblyCopyrightAttribute>();
if (attr != null)
return attr.Copyright;
return string.Empty;
}
}
private static T GetAssemblyAttribute<T>() where T : Attribute {
object[] attributes = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(T), true);
if (attributes == null || attributes.Length == 0) return null;
return (T)attributes[0];
}
}
}
Now, I can use it like this:
Console.WriteLine(AssemblyInfo.Title);
Console.WriteLine(AssemblyInfo.Copyright);
The best part is that since SharedAssemblyInfo.cs is linked to each of my projects, I can use the AssemblyInfo class wherever I want!
Also, the class is reading attributes from the assembly that's currently executing, which means that if you call AssemblyInfo.Title in Project 1, it will return the value of Project 1's AssemblyTitleAttribute, while Project 2 will return its own value. If you have defined the assembly title in a linked file, however, both Project 1 and Project 2 will return that value.