How does ObsoleteAttribute work?

Newbie question: if I am mistaken, most attributes seem to be benign until you use reflection during assembly (s) or classes to detect and act on the attributes. I noticed that ObsoleteAttribute seems unique in .NET attributes, where it can dynamically raise warnings and errors at compile time:

  [Obsolete("don't use", false)] public string Name { get; set; } 

I have a question how to do this? Is this something embedded in the compiler since the corresponding warning message number seems specific to ObsoleteAttribute? I google'd and cannot find any obvious answers. I know with C ++ and a combination of great macros, you can force C ++ to generate warnings and errors on demand, but how does C # do this? Thanks...

+4
source share
3 answers

Yes, the compiler knows ObsoleteAttribute - it is even listed in section 17.4.3 of the C # 4 language specification.

(The compiler should also be aware of AttributeUsageAttribute and ConditionalAttribute , and the MS compiler should be aware of IndexerNameAttribute and some other attributes in System.Runtime.InteropServices .)

+7
source

You are right, recognition and interpretation of this attribute are built into the compiler.

+2
source

As Skeet notes, the compiler knows about ObsoleteAttribute. This is not the only one; check also the conditional attribute.

+1
source

Source: https://habr.com/ru/post/1386346/


All Articles