Mono Class Library: System.AttributeUsageAttribute Overview | Members

System.AttributeUsageAttribute.AllowMultiple Property

Gets or sets a value indicating whether more than one instance of a specified attribute is permitted to be applied to any given program element.

public bool AllowMultiple { set; get; }

Value

A bool where true indicates more than one instance of the attribute is permitted to be applied; otherwise, false. The default is false.

Remarks

Note: It is expected that compilers will validate this property; this property is not validated during execution.

Example

Example #1:

The following example demonstrates the use of AttributeUsageAttribute.AllowMultiple. If AllowMultiple for an attribute is set to true, more than one of those attributes can be assigned to any given program element.

C# Example

using System;

[AttributeUsageAttribute( AttributeTargets.Class |
                          AttributeTargets.Struct,
                          AllowMultiple = true )]
public class Author : Attribute {

  public Author(string name) { this.name = name; }
  public string name;
}

[Author( "John Doe" )]
[Author( "John Q Public" )]
class JohnsClass {

  public static void Main() {}
}

Example #2:

The following example demonstrates an error that is expected to be caught by compilers: the sample attempts to assign multiple instances of an attribute for which AllowMultiple was set to false.

C# Example

using System;

[AttributeUsageAttribute( AttributeTargets.Class |
                          AttributeTargets.Struct,
                          AllowMultiple = false )]
public class Author : Attribute {

  public Author(string name) { this.name = name; }
  public string name;
}

[Author( "John Doe" )]
[Author( "John Q Public" )]
class JohnsClass {

  public static void Main() {}
}

This should throw an error similar to:

error CS0579: Duplicate 'Author' attribute

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0