編譯器錯誤 CS1614
更新:2007 年 11 月
錯誤訊息
'name' 在 'attribute1' 和 'attribute2' 之間模稜兩可。請使用 '@attribute' 或 'attributeAttribute'
編譯器發現模稜兩可的屬性 (Attribute) 規格。
為了方便起見,C# 編譯器允許您將 ExampleAttribute 指定為單純 [Example]。然而,如果名為 Example 的屬性類別與 ExampleAttribute 同時存在,就會發生模稜兩可的情況,因為編譯器無法分辨出 [Example] 是指 Example 屬性或 ExampleAttribute 屬性。若要加以區別,請針對 Example 使用 [@Example] 屬性,ExampleAttribute 則使用 [ExampleAttribute]。
下列範例會產生 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() {
}
}