简体   繁体   English

如何使用显式类实现接口类型

[英]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.

相关问题 我如何使用:Interface实现类? - How can i implement a class using :Interface? 如何实现具有IEnumerable类型的属性的接口 <IA> 并使用实现该类型的接口的类型覆盖? - How can I implement an interface with a property of type IEnumerable<IA> and override with a type that implements the interface of the type? 如何约束通用类型以实现通用接口? - How can I constrain a generic type to implement a generic interface? 值类型如何实现接口类型? - How can a value type implement an interface type? 如何实现接口并从基类继承? - How can I implement an interface and inherit from a base class? 如何在返回的类上实现接口并保留其数据? - How can I Implement an interface on a returned class and keep its data? 我可以通过Type变量而不是显式类型来转换类的实例吗? - Can I cast an instance of a class by a Type variable rather then an explicit type? 如何使用泛型类实现具有接口类型签名的接口 - How to implement interface having a signature of interface type with generic class 在.NET中如何在没有接口继承的情况下在静态类中实现接口方法? - How can I implement interface methods in the static class without Interface Inheritance in .NET? 我如何利用类型的EntityCollection <Interface> 在实体框架中使用局部类? - How can I utilize EntityCollection of type <Interface> using a partial class with Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM