Reading .NET assembly information at runtime with C#

Posted: (EET/GMT+2)

 

If you use C# to write your applications, you will without doubt want to add your application details into the attributes in your AssemblyInfo.cs file. For example, you might want to fill in the AssemblyDescription, AssemblyCopyright or AssemblyCompany attributes, among others. Sometimes, you might also want to display this same information in your user interface, for example in a log file, About box, and so forth. But what is the easiest to way to read this information from C# code? Here's one example:

using System.Reflection;
...
private string ReadDescription()
{
  Assembly assembly = Assembly.GetExecutingAssembly();
  object[] attributes = assembly.GetCustomAttributes(
    typeof(AssemblyDescriptionAttribute), false);
  if (attributes.Length == 0) return "(no description)";
  string desc = ((AssemblyDescriptionAttribute)attributes[0]).Description;
  return desc;
}

As the method name already tells you, this method gives you the value of the AssemblyDescriptionAttribute inside your EXE or DLL (current assembly as returned by GetExecutingAssembly). With this kind of code, it is easy to read any attribute value you've added/embedded into your assembly.