編譯器錯誤 CS0686
更新:2007 年 11 月
錯誤訊息
存取子 'accessor' 無法實作型別 'type' 的介面成員 'member'。請用明確介面實作。
建議:當實作的介面中包含的方法名稱,與自動產生且和屬性或事件相關的方法發生衝突時,便可能發生這個錯誤。屬性的 get/set 方法會產生為 get_property 和 set_property,而事件的 add/remove 方法則會產生為 add_event 和 remove_event。如果介面包含上述任何一種方法,便會發生衝突。若要避免這個錯誤,請使用明確介面實作來實作方法。若要執行這項操作,請將函式指定如下:
Interface.get_property() { /* */ }
Interface.set_property() { /* */ }
範例
下列範例會產生 CS0686:
// CS0686.cs
interface I
{
int get_P();
}
class C : I
{
public int P
{
get { return 1; } // CS0686
}
}
// But the following is valid:
class D : I
{
int I.get_P() { return 1; }
public static void Main() {}
}
宣告事件時也可能會發生這個錯誤。事件建構會自動產生 add_event 和 remove_event 方法,而可能與介面中同名的方法發生衝突,如下列範例所示:
// CS0686b.cs
using System;
interface I
{
void add_OnMyEvent(EventHandler e);
}
class C : I
{
public event EventHandler OnMyEvent
{
add { } // CS0686
remove { }
}
}
// Correct (using explicit interface implementation):
class D : I
{
void I.add_OnMyEvent(EventHandler e) {}
public static void Main() {}
}