Share via


Erreur du compilateur CS1614

'name' est ambigu entre 'name' et 'nameAttribute'. Utilisez '@name' ou 'nameAttribute'.

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 s’il existe une classe d’attributs nommée Example avec ExampleAttribute, car le compilateur ne peut pas déterminer si [Example] fait référence à l’attribut Example ou 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() {  
   }  
}