Partager via


Erreur du compilateur CS1614

Mise à jour : novembre 2007

Message d'erreur

'nom' est ambigu ; entre 'attribut1' et 'attribut2', utilisez '@attribut' ou 'attributAttribute'
'name' is ambiguous; between 'attribute1' and 'attribute2'. use either '@attribute' or 'attributeAttribute'

Le compilateur a rencontré une spécification d'attribut ambiguë.

Pour des raisons de commodité, le compilateur C# vous permet de spécifier ExampleAttribute simplement sous la forme [Example]. Cependant, une ambiguïté apparaît si une classe d'attributs nommée Example existe avec ExampleAttribute, puisque le compilateur ne peut pas déterminer si [Example] se réfère à l'attribut Example ou à l'attribut ExampleAttribute. Pour lever cette ambiguïté, utilisez [@Example] pour l'attribut Example et [ExampleAttribute] pour ExampleAttribute.

L'exemple suivant génère l'erreur CS1614 :

// CS1614.cs
using System;

// Both of the following classes are valid attributes with valid
// names (MySpecial and MySpecialAttribute). However, because the lookup
// rules for attributes involves auto-appending the 'Attribute' suffix
// to the identifier, these two attributes become ambiguous; that is,
// if you specify MySpecial, the compiler can't tell if you want
// MySpecial or MySpecialAttribute.

public class MySpecial : Attribute {
   public MySpecial() {}
}

public class MySpecialAttribute : Attribute {
   public MySpecialAttribute() {}
}

class MakeAWarning {
   [MySpecial()] // CS1614
                 // Ambiguous: MySpecial or MySpecialAttribute?
   public static void Main() {
   }

   [@MySpecial()] // This isn't ambiguous, it binds to the first attribute above.
   public static void NoWarning() {
   }

   [MySpecialAttribute()] // This isn't ambiguous, it binds to the second attribute above.
   public static void NoWarning2() {
   }

   [@MySpecialAttribute()] // This is also legal.
   public static void NoWarning3() {
   }
}