防止调用基类实现的接口方法派生类C# [英] Prevent calling base class implemented interface method in the derived class C#

查看:169
本文介绍了防止调用基类实现的接口方法派生类C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能实现在一个基类的接口,并允许调用/覆盖在第一个派生类级别实现的方法,但会阻止任何进一步的派生类调用它?

Is it possible to implement an interface in a base class and allow calling/overriding the implemented method in the first derived class level but prevent calling it from any further derived classes?

    public interface IInterfaceSample
    {
        bool Test();
    }

    public class Base: IInterfaceSample
    {
        public virtual bool Test()
        {
             return True;
        }
    }

    public class Sub1: Base
    {
        //I need to be able to override the Test method here
        public override bool Test()
        {
             return True;
        }
    }

    //Under a separate project:
    public class Sub2: Sub1
    {
       //I need to prevent overriding the interface implementation in this class
    }

现在我需要的是这样的:

Now what i need is this:

    var b = new Base();
    b.Test();//This should work

    var s1 = new Sub1();
    s1.Test();//I need this to work too

    var s2 = new Sub2();
    s2.Test();//I need to prevent doing this



到目前为止,从研究,我想这也许是不可能的,因为接口必须是公开的,否则有使用它们没有真正的价值。

So far from research i think this might not be possible because interfaces has to be public, otherwise there is no real value of using them.

在我的情况,我需要一流的分公司2有访问在Sub1的,但属性只这一点,并在该类以及专门的接口实现方法的方法没有访问。

In my case, i need class Sub2 to have access to the properties in Sub1 but only that and no access to the methods on that class and specially the interface implementation methods.

只有这样,我能做到这一点是在所有未使用的接口,像这样做:

The only way i was able to do this was to not use the interfaces at all and do it like this:

    public class Base
    {
        internal virtual bool Test()
        {
             return True;
        }
    }

    public class Sub1: Base
    {
        //I am able to override the Test method here
        internal override bool Test()
        {
             return True;
        }
    }

    //Under a separate project:
    public class Sub2: Sub1
    {
       //Nothing to override here which is what i need
    }

    var b = new Base();
    b.Test();//This works

    var s1 = new Sub1();
    s1.Test();//This works too

    var s2 = new Sub2();
    s2.Test();//This is prevented



不过,我想我如果这,仍然可以用接口来实现,任何的帮助深表感谢。

However i am wondering if this is still available to achieve with interfaces, any help is much appreciated.

推荐答案

没有,这是不可能的 - 这将打破整点多态性。特别是,假设你没有使用 VAR ,但明确使用的类型:

No, this isn't possible - it would break the whole point of polymorphism. In particular, imagine you didn't use var, but used the types explicitly:

Sub1 s2 = new Sub2();
s2.Test();

这有编译:


  • 第一行有编译,因为分公司2 源自 Sub1的

  • 第二行有编译,因为你想要 s1.Test()编译,在编译时类型的 S1 Sub1的以及

  • The first line has to compile because Sub2 is derived from Sub1.
  • The second line has to compile because you wanted s1.Test() to compile, where the compile-time type of s1 is Sub1 as well.

作为经验法则,如果你有两个类X和Y,并且只的部分的X上的公共行动是有效的Y,则Y不应该从十派生你应该能够处理任何例如派生类中,如果是基类(它实现所有接口)的一个实例。

As a rule of thumb, if you have two classes X and Y, and only some of the public operations on X are valid for Y, then Y shouldn't derive from X. You should be able to treat any instance of a derived class as if it's an instance of the base class (and all interfaces it implements).

这篇关于防止调用基类实现的接口方法派生类C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆