Compilerfehler CS1614
Aktualisiert: November 2007
Fehlermeldung
"Name" kann "attribute1" oder "attribute2" sein. Verwenden Sie entweder "@Attribut" oder "attributeAttribute".
'name' is ambiguous; between 'attribute1' and 'attribute2'. use either '@attribute' or 'attributeAttribute'
Der Compiler hat eine mehrdeutige Attributspezifikation ermittelt.
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 Example-Attribut oder das ExampleAttribute-Attribut bezieht. Um die Mehrdeutigkeit aufzulösen, verwenden Sie für das Example-Attribut [@Example] und für ExampleAttribute[ExampleAttribute].
Im folgenden Beispiel wird 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() {
}
}