Just as a mental exercise, today I decided to try to write a single method that would allow me to return information about the currently running assembly as a string. One method needed to return the copyright info, the company info, etc.
The end result is this simple function:
public string GetAssemblyAttribute<T>(T attributeType) where T:Type
{
object attribute = Assembly.GetExecutingAssembly().GetCustomAttributes((T)attributeType, false)[0];
return (string)attribute.GetType().GetProperties()[0].GetValue(attribute, null);
}
And calling it is this easy:
string s = "";
s = GetAttribute(typeof(AssemblyCopyrightAttribute));
s = GetAttribute(typeof(AssemblyCompanyAttribute));
s = GetAttribute(typeof(AssemblyDescriptionAttribute));
s = GetAttribute(typeof(AssemblyProductAttribute));
s = GetAttribute(typeof(AssemblyTitleAttribute));
s = GetAttribute(typeof(AssemblyTrademarkAttribute));
Now why it has to be this ugly I'm not sure, but isn't reflection a hoot!?