[英]How can I implement an interface type using a explicit class
I have the following interfaces: 我有以下接口:
public interface IReplaceable
{
int ParentID { get; set; }
IReplaceable Parent { get; set; }
}
public interface IApproveable
{
bool IsAwaitingApproval { get; set; }
IApproveable Parent { get; set; }
}
I would like to implement it in the class like so: 我想在类中实现它,如下所示:
public class Agency : IReplaceable, IApproveable
{
public int AgencyID { get; set; }
// other properties
private int ParentID { get; set; }
// implmentation of the IReplaceable and IApproveable property
public Agency Parent { get; set; }
}
Is there any way that I can do this? 有什么方法可以做到这一点吗?
You can't implement an interface with a method (or property) that doesn't satisfy the interface's signature. 您无法使用不满足接口签名的方法(或属性)实现接口。 Consider this: 考虑一下:
IReplaceable repl = new Agency();
repl.Parent = new OtherReplaceable(); // OtherReplaceable does not inherit Agency
How should the type checker evaluate this? 类型检查器应该如何评估这个? It would be valid by the signature of IReplaceable
, but not by the signature of Agency
. 它可以通过IReplaceable
的签名有效,但不能通过Agency
的签名。
Instead, consider using explicit interface implementation like this: 相反,请考虑使用这样的显式接口实现 :
public class Agency : IReplaceable
{
public int AgencyID { get; set; }
// other properties
private int ParentID { get; set; }
public Agency Parent { get; set; }
IReplaceable IReplaceable.Parent
{
get
{
return this.Parent; // calls Agency Parent
}
set
{
this.Parent = (Agency)value; // calls Agency Parent
}
}
IApproveable IApproveable.Parent
{
get
{
return this.Parent; // calls Agency Parent
}
set
{
this.Parent = (Agency)value; // calls Agency Parent
}
}
}
Now, when you do something like this: 现在,当你做这样的事情时:
IReplaceable repl = new Agency();
repl.Parent = new OtherReplaceable();
The type checker considers this valid by the signature of the IReplaceable
, so it compiles just fine, but it would throw an InvalidCastException
at run time (of course, you can implement your own logic if you don't want an exception). 类型检查器认为这对IReplaceable
的签名有效,因此它编译得很好,但它会在运行时抛出一个InvalidCastException
(当然,如果你不想要异常,你可以实现自己的逻辑)。 However, if you do something like this: 但是,如果您执行以下操作:
Agency repl = new Agency();
repl.Parent = new OtherReplaceable();
It will not compile, because the type checker will know that repl.Parent
must be an Agency
. 它不会编译,因为类型检查器将知道repl.Parent
必须是Agency
。
You can implement it explicitly . 您可以明确地实现它 。 That's why they exist. 这就是他们存在的原因。
public class Agency : IReplaceable, IApproveable
{
public int AgencyID { get; set; }
int IReplaceable.ParentID
{
get;
set;
}
bool IApproveable.IsAwaitingApproval
{
get;
set;
}
IApproveable IApproveable.Parent
{
get;
set;
}
IReplaceable IReplaceable.Parent
{
get;
set;
}
}
Sadly you can't access it via class instance, you need to cast it to interface in order to use them. 遗憾的是,您无法通过类实例访问它,您需要将其强制转换为接口才能使用它们。
Agency agency = ...;
agency.Parent = someIApproveable;//Error
agency.Parent = someIReplaceable;//Error
((IApproveable)agency).Parent = someIApproveable;//Works fine
((IReplaceable)agency).Parent = someIReplaceable;//Works fine
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.