I read about attributes and understand that they can be applied to various targets with code - (see Attributes of Attributes ).
So, looking at the AssemblyInfo.cs file in my project, I see the following:
[assembly: AssemblyTitle("AttributesDemo")] [assembly: AssemblyDescription("")]
That makes sense to me. The attribute whose purpose is the assembly.
In my code, I can add an attribute to the class as follows:
[MyAttribute] class MySerialzableClass {
With MyAttribute will be:
[AttributeUsage (AttributeTargets.All)] public class MyAttribute : System.Attribute { }
So, I thought of the assembly: instruction in the first block of code. And tried this, just for experimentation:
[class: MyAttribute] class MySerialzableClass {
This gives a compiler warning:
'class' is not a recognized attribute of a location. All attributes in this block will be ignored.
So my question is this: why do I need to specify a Target Attribute Target for some attributes and are not required for others? Moreover, why should you do this?
source share