Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Assembly Attributes

I would like to know if I can define custom assembly attributes. Existing attributes are defined in the following way:

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

Is there a way I can do the following:

[assembly: MyCustomAssemblyAttribute("Hello World! This is a custom attribute.")] 
like image 646
Charlie Salts Avatar asked Dec 20 '09 20:12

Charlie Salts


People also ask

What are assembly attributes?

Assembly attributes are values that provide information about an assembly. The attributes are divided into the following sets of information: Assembly identity attributes. Informational attributes.

Where do I put assembly attributes?

Assembly attributes like that must be before any namespace or class (or other type) declarations in the code file in question. Show activity on this post. I'd usually put it outside of any class declarations, and indeed namespace declarations. It could go anywhere (except within a class), and AssemblyInfo.

What are the attributes of AssemblyInfo CS file?

Assembly identity attributes Three attributes (with a strong name, if applicable) determine the identity of an assembly: name, version, and culture. These attributes form the full name of the assembly and are required when you reference it in code. You can set an assembly's version and culture using attributes.


2 Answers

Yes you can. We do this kind of thing.

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

To read use this kind of linq stmt.

var attributes = assembly     .GetCustomAttributes(typeof(MyCustomAttribute), false)     .Cast<MyCustomAttribute>(); 
like image 160
Preet Sangha Avatar answered Sep 19 '22 15:09

Preet Sangha


Yes, use AttributeTargets.Assembly:

[AttributeUsage(AttributeTargets.Assembly)] public class AssemblyAttribute : Attribute { ... } 
like image 40
Lee Avatar answered Sep 19 '22 15:09

Lee