我有一个界面:
public interface Profile
{
string Name { get; }
string Alias { get; set; }
}
所有实现的对象Profile
都有 aName
和 an Alias
,但有些限制Alias
使其始终与Name
. 应用此限制的可以Alias
这样实现:
string Profile.Alias
{
get
{
return ((Profile)this).Name;
}
set { }
}
由于this
在显式接口实现的上下文中只能是类型Profile
,并且我们知道它是通过Profile
接口而不是包含类或它实现的任何其他接口访问的,为什么需要强制转换?
使用return this.Name;
getter 实现会导致此错误:
Type `ConcreteProfile' does not contain a definition for `Name' and no extension method `Name' of type `ConcreteProfile' could be found (are you missing a using directive or an assembly reference?)