Compilerfehler CS1614

„name“ ist für „name“ und „nameAttribute“ mehrdeutig. Verwenden Sie „@name“ oder „nameAttribute“.

Der Compiler hat eine mehrdeutige Attributspezifikation festgestellt.

Der C#-Compiler bietet die Möglichkeit, ExampleAttribute der Einfachheit halber als [Example] anzugeben. Wenn neben ExampleAttribute jedoch auch eine Attributklasse mit dem Namen Example vorhanden ist, entsteht eine Mehrdeutigkeit, da der Compiler nicht feststellen kann, ob sich [Example] auf das Attribut Example oder das Attribut ExampleAttribute bezieht. Um die Mehrdeutigkeit aufzulösen, verwenden Sie Example für das [@Example]-Attribut und [ExampleAttribute] für ExampleAttribute.

Im folgenden Beispiel wird der Fehler CS1614 generiert:

// 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() {  
   }  
}