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.")]
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.
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.
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.
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>();
Yes, use AttributeTargets.Assembly:
[AttributeUsage(AttributeTargets.Assembly)] public class AssemblyAttribute : Attribute { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With