X hits on this document





320 / 396

Chapter ‎18   Unsafe code

[AttributeUsage(AttributeTargets.All)] public class XAttribute: Attribute {}

[X]// Error: ambiguity class Class1 {}

[XAttribute]// Refers to XAttribute class Class2 {}

[@X]// Refers to X class Class3 {}

[@XAttribute]// Refers to XAttribute class Class4 {}

shows two attribute classes named X and XAttribute. The attribute [X] is ambiguous, since it could refer to either X or XAttribute. Using a verbatim identifier allows the exact intent to be specified in such rare cases. The attribute [XAttribute] is not ambiguous (although it would be if there was an attribute class named XAttributeAttribute!). If the declaration for class X is removed, then both attributes refer to the attribute class named XAttribute, as follows:

using System;

[AttributeUsage(AttributeTargets.All)] public class XAttribute: Attribute {}

[X]// Refers to XAttribute class Class1 {}

[XAttribute]// Refers to XAttribute class Class2 {}

[@X]// Error: no attribute named "X" class Class3 {}

It is a compile-time error to use a single-use attribute class more than once on the same entity. The example

using System;

[AttributeUsage(AttributeTargets.Class)] public class HelpStringAttribute: Attribute { string value;

public HelpStringAttribute(string value) { this.value = value; }

public string Value { get {...} } }

[HelpString("Description of Class1")] [HelpString("Another description of Class1")] public class Class1 {}

results in a compile-time error because it attempts to use HelpString, which is a single-use attribute class, more than once on the declaration of Class1.

An expression E is an attribute-argument-expression if all of the following statements are true:

Copyright Microsoft Corporation 1999-2003. All Rights Reserved.307

Document info
Document views1167
Page views1167
Page last viewedWed Jan 18 06:57:29 UTC 2017