Mega Code Archive

 
Categories / C# / Reflection
 

Get an array of the custom attributes applied to an assembly A parameter specifies the assembly

using System;     using System.Reflection;          [assembly: AssemblyTitle("CustAttrs1CS")]     [assembly: AssemblyDescription("GetCustomAttributes() Demo")]     [assembly: AssemblyCompany("Microsoft")]     class DemoClass {         static void Main(string[] args) {             Type clsType = typeof(DemoClass);             Assembly assy = clsType.Assembly;             foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {                 if (attr.GetType() == typeof(AssemblyTitleAttribute))                     Console.WriteLine("Assembly title is \"{0}\".",((AssemblyTitleAttribute)attr).Title);                 else if (attr.GetType() == typeof(AssemblyDescriptionAttribute))                     Console.WriteLine("Assembly description is \"{0}\".",((AssemblyDescriptionAttribute)attr).Description);                 else if (attr.GetType() == typeof(AssemblyCompanyAttribute))                     Console.WriteLine("Assembly company is {0}.",((AssemblyCompanyAttribute)attr).Company);             }         }     }