Search code examples
c#compiler-constructionattributeslanguage-features

ConditionalAttribute and other special classes


The ConditionalAttribute can be used to remove calls to a marked method depending on the compiler symbols defined. I'm assuming we could not create this class ourselves since the compiler must be looking for it specifically.

I was wondering what other classes there are that the compiler, or language uses that we could not code ourselves.


Solution

    • The compiler looks for [ExtensionAttribute] to indicate extension methods (and classes containing extension methods).

    • [DynamicAttribute] is used to indicate that a member should be treated as type dynamic (even though the member type itself will just be object)

    • [InternalsVisibleTo] allows one assembly to access the internal members of another.

    Basically look through the System.Runtime.CompilerServices namespace, and examine the attributes in there... many of them will be handled specially by a compiler, even if it's not the C# compiler (e.g. DateTimeConstantAttribute isn't used by the C# compiler as far as I'm aware, but DecimalConstantAttribute is. It's possible that the C# compiler will consume constant DateTime values even though it won't produce them...)