为什么我需要在 C# 显式实现中将 'this' 强制转换为接口类型?
Why do I need to cast 'this' to interface type in a C# explicit implementation?
我有一个界面:
public interface Profile
{
string Name { get; }
string Alias { get; set; }
}
所有实现 Profile
的对象都有一个 Name
和一个 Alias
,但有些对象限制 Alias
,使其始终与 Name
相同。应用此限制的可以像这样实现 Alias
:
string Profile.Alias
{
get
{
return ((Profile)this).Name;
}
set { }
}
因为 this
在显式接口实现的上下文中只能是类型 Profile
并且我们知道它是通过 Profile
接口而不是包含它的接口访问的class 或它实现的任何其他接口,为什么需要转换?
将 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?)
Since this within the context of an explicit interface implementation can only possibly be of type Profile and we know it was accessed through the Profile interface rather than that of the containing class or any other interface it implements, why is the cast required?
因为它使用显式接口实现。这只是显式接口实现所做的一部分 - 以及它如何实现消除调用歧义的目的的一部分,否则这些调用将是模棱两可的。来自 C# 5 规范,第 13.4.1 节:
It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.
...
Explicit interface member implementations serve two primary purposes:
- Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct. This is particularly useful when a class or struct implements an internal interface that is of no interest to a consumer of that class or struct.
- Explicit interface member implementations allow disambiguation of interface members with the same signature. Without explicit interface member implementations it would be impossible for a class or struct to have different implementations of interface members with the same signature and return type, as would it be impossible for a class or struct to have any implementation at all of interface members with the same signature but with different return types.
Since this
within the context of an explicit interface implementation can only possibly be of type Profile
这不是真的。您正在 ConcreteProfile
class 中实现 Profile.Alias
。在此上下文中,this
指的是 ConcreteProfile
实例,可用于访问 ConcreteProfile
实例的任何成员。
例如,ConcreteProfile
class 可以包含另一个 Name
属性 而不是 Profile.Name
。在这种情况下 this.Name
将指代 属性.
因为要访问Profile.Name
,所以必须将this
转换为Profile
。
我有一个界面:
public interface Profile
{
string Name { get; }
string Alias { get; set; }
}
所有实现 Profile
的对象都有一个 Name
和一个 Alias
,但有些对象限制 Alias
,使其始终与 Name
相同。应用此限制的可以像这样实现 Alias
:
string Profile.Alias
{
get
{
return ((Profile)this).Name;
}
set { }
}
因为 this
在显式接口实现的上下文中只能是类型 Profile
并且我们知道它是通过 Profile
接口而不是包含它的接口访问的class 或它实现的任何其他接口,为什么需要转换?
将 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?)
Since this within the context of an explicit interface implementation can only possibly be of type Profile and we know it was accessed through the Profile interface rather than that of the containing class or any other interface it implements, why is the cast required?
因为它使用显式接口实现。这只是显式接口实现所做的一部分 - 以及它如何实现消除调用歧义的目的的一部分,否则这些调用将是模棱两可的。来自 C# 5 规范,第 13.4.1 节:
It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.
...
Explicit interface member implementations serve two primary purposes:
- Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct. This is particularly useful when a class or struct implements an internal interface that is of no interest to a consumer of that class or struct.
- Explicit interface member implementations allow disambiguation of interface members with the same signature. Without explicit interface member implementations it would be impossible for a class or struct to have different implementations of interface members with the same signature and return type, as would it be impossible for a class or struct to have any implementation at all of interface members with the same signature but with different return types.
Since
this
within the context of an explicit interface implementation can only possibly be of typeProfile
这不是真的。您正在 ConcreteProfile
class 中实现 Profile.Alias
。在此上下文中,this
指的是 ConcreteProfile
实例,可用于访问 ConcreteProfile
实例的任何成员。
例如,ConcreteProfile
class 可以包含另一个 Name
属性 而不是 Profile.Name
。在这种情况下 this.Name
将指代 属性.
因为要访问Profile.Name
,所以必须将this
转换为Profile
。