Using Attribute Targets

Attribute targets specify to what program element an attribute is being applied. They're not always required but can sometimes help make the intent of the attribute more understandable. For example, if there were ambiguity between whether an attribute applied to a method's return value or the method itself, then an attribute target specification would resolve the ambiguity. Listing 27.5 contains an example of how to specify attribute targets.

Listing 27.5. Attribute Targets
using System;

/// <summary>
///     Attribute Targets.
/// </summary>
[assembly:CLSCompliant(false)]
class AttributeTarget
{
    static void Main(string[] args)
    {
    }
}

The attribute in Listing 27.5 specifies that the CLSCompliant attribute applies to assembly ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.