Custom assembly assemblers

I would like to know if I can define custom assembly attributes. Existing attributes are defined as follows:

[assembly: AssemblyTitle("MyApplication")] [assembly: AssemblyDescription("This application is a sample application.")] [assembly: AssemblyCopyright("Copyright © MyCompany 2009")] 

Is there any way to do the following:

 [assembly: MyCustomAssemblyAttribute("Hello World! This is a custom attribute.")] 
+51
c # attributes assemblies
Dec 20 '09 at 20:38
source share
2 answers

Yes, you can. We do such things.

 [AttributeUsage(AttributeTargets.Assembly)] public class MyCustomAttribute : Attribute { string someText; public MyCustomAttribute() : this(string.Empty) {} public MyCustomAttribute(string txt) { someText = txt; } ... } 

For reading use this linq type stmt.

 var attributes = assembly .GetCustomAttributes(typeof(MyCustomAttribute), false) .Cast<MyCustomAttribute>(); 
+76
Dec 20 '09 at 20:44
source share

Yes, use AttributeTargets.Assembly:

 [AttributeUsage(AttributeTargets.Assembly)] public class AssemblyAttribute : Attribute { ... } 
+8
Dec 20 '09 at 20:46
source share



All Articles