C# FAQ: How get assembly attributes at runtime

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How get assembly attributes at runtime?

The majority of assembly-wide attributes—usually specified in AssemblyInfo.cs—can be retrieved using Assembly.GetCustomAttributes. For example, code such as the following can be used to get the assembly title:

Assembly thisAssembly = this.GetType().Assembly;
object[] attributes = thisAssembly.GetCustomAttributes 
   (typeof(AssemblyTitleAttribute), false));
if (attributes.Length == 1)
{
   Console.WriteLine (((AssemblyTitleAttribute) attributes[0]).Title);
}

Notice that AssemblyVersionAttribute becomes a part of the assembly name, which can be accessed via the Assembly.GetName method. The version can be accessed via the name with the Version property. For example:

Assembly thisAssembly = this.GetType().Assembly;
Console.WriteLine (thisAssembly.GetName().Version);

Personal tools