interface IThing
{
int Property {get;}
void Method();
}
class Thing : IThing
{
int IThing.Property {get; } = 999;
void IThing.Method() {Console.WriteLine($"Property = {Property}");}
}
Run Code Online (Sandbox Code Playgroud)
这会产生编译器错误“名称属性在当前上下文中不存在”。不管我指的是Property或IThing.Property还是this.Property
为什么显式接口实现似乎会相互“屏蔽”接口方法?这是我的语言功能还是语法错误(我之前没有使用过显式接口实现,并且正在对其进行测试以查看)。
您必须将this其作为接口的实例进行引用。您可以通过铸造来做到这一点。
Console.WriteLine($"Property = {((IThing)this).Property}");
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,另请参阅显式接口实现。