Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the ObsoleteAttribute work?

Tags:

c#

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...

like image 818
Barry Avatar asked Dec 28 '22 09:12

Barry


2 Answers

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.)

like image 110
Jon Skeet Avatar answered Jan 10 '23 21:01

Jon Skeet


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

like image 40
Sergey Kalinichenko Avatar answered Jan 10 '23 20:01

Sergey Kalinichenko