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
AttributeUsageAttributeandConditionalAttribute, and the MS compiler is aware ofIndexerNameAttributeand some other attributes withinSystem.Runtime.InteropServices.)