Поделиться через


Ошибка компилятора CS1614

Обновлен: Ноябрь 2007

Сообщение об ошибке

"имя" не является однозначным между "атрибут1" и "атрибут2"; используйте "@атрибут" или "атрибутAttribute"
'name' is ambiguous; between 'attribute1' and 'attribute2'. use either '@attribute' or 'attributeAttribute'

Компилятором обнаружена неоднозначная спецификация атрибута.

Для удобства компилятор 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() {
   }
}