Newbie question: unless I'm mistaken, most attributes appear to be benign until you use runtime reflection on your assembly(ies) or classes to discover the attributes and act upon them. I've noticed that the ObsoleteAttribute
seems to be unique in the .NET attributes where it can dynamically raise warnings and errors during compilation:
[Obsolete("don't use", false)]
public string Name { get; set; }
The question I have is how does it do this? Is this something built in to the compiler, as the associated warning message number seems specific to the ObsoleteAttribute? I've google'd and can't find any obvious answers. I know with C++ and a combination of nifty macro tricks you can get C++ to emit warnings and errors on demand, but how does C# do this? Thanks...
Yes, the compiler is aware of ObsoleteAttribute
- it's even listed in section 17.4.3 of the C# 4 language specification.
(The compiler also needs to know about AttributeUsageAttribute
and ConditionalAttribute
, and the MS compiler is aware of IndexerNameAttribute
and some other attributes within System.Runtime.InteropServices
.)
You are correct, the recognition and interpretation of this attribute is built into the compiler.
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