简体   繁体   English

这在c#中可能吗? 只有一个类可以访问基类方法,而第二个类不能访问相同的基类方法

[英]is this possible in c# ? only one class access base class method and second class can not access the same base class method

A is base class A是基类

B is derived from A and also C is derived from A B衍生自A,C也衍生自A

I want only B can access the method of A , C an not access of that same method of A. 我只希望B可以访问A,C的方法,而不能访问A的相同方法。

class A {

    protected void Foo() {
    }
}

class B : A {

    void Bar() {
        this.Foo(); // OK
    }
}

class C : A {
    void Baz() {
        this.Foo(); // I don't want to permit this
    }
}

HOW IT POSSIBLE IN c# 如何在C#中实现

I think this look like a problem for Interface segregation principle : 我认为这看起来像是Interface segregation principle的问题:

Clients should not be forced to depend upon interfaces that they don't use. 不应强迫客户端依赖于不使用的接口。

But in your case this can be rephrased for the class inheritance. 但是在您的情况下,可以将其改写为类继承。

Create pure base class (without a method you want to hide from class C ) 创建纯基类(没有要从类C隐藏的方法)

public class Base
{
    protected void SomeDummyMethod()
    {

    }
}

Then create your A class which inherit from Base and add a method you what to share for class B 然后创建A继承自Base A类,并为B类添加一个共享的方法

public class A : Base
{
    protected void YourFooMethod()
    {

    }
}

Create B class which inherit from A and will have access to all functionality including YourFooMethod 创建从A继承的B类,并且可以访问所有功能,包括YourFooMethod

public class B : A
{
    public void Bar() 
    {
        this.YourFooMethod();
    }
}

And finally your C class which have all base functionality except YourFooMethod method 最后是您的C类,它具有除YourFooMethod方法之外的所有基本功能

public class C : Base
{
    public void Bar() 
    {
        this.YourFooMethod(); //Compile error: YourFooMethod is not a member of...
    }
}

I suppose you could write code in class A that checks the calling class name against a white list or a black list and throws an exception in the cases you want to disallow, but I would not recommend doing this. 我想您可以在A类中编写代码,该代码对照白名单或黑名单检查调用类的名称,并在您要禁止的情况下引发异常,但我不建议这样做。 That would be very difficult to maintain, and class A should not need to know about every class that extends it. 这将很难维护,并且A类不需要了解扩展它的每个类。

What you are trying to do is really honestly a bad idea. 老实说,您要尝试执行的操作是个坏主意。

C# (and .NET in general) has the access modifiers: C#(通常是.NET)具有访问修饰符:

  • public - Anyone can access public -任何人都可以访问
  • private - Only the containing scope/type can access private -仅包含作用域/类型可以访问
  • protected - Only the containing type and its derived types can access protected仅包含类型及其派生类型可以访问
  • internal - Only types defined in the same Assembly (or InternalsVisibleTo Assemblies) can access internal -只有在同一程序集(或InternalsVisibleTo )中定义的类型可以访问
  • protected internal - The set-union of protected and internal can access. protected internal - protectedinternal可以访问的集合。

You're asking for something in-between private and protected , where you can manually restrict access to named types. 您需要在privateprotected之间,您可以手动限制对命名类型的访问。

This is not currently possible to enforce, at least at compile-time, in .NET - though if types A and B exist in the same assembly and C exists in a different assembly then internal would work. 当前至少在编译时无法在.NET中强制执行-尽管如果类型AB存在于同一程序集中,而类型C存在于不同的程序集中,则internal将起作用。

At runtime you could enforce this with code-access-security, or more simply: using reflection to get the calling-class's name ( this.GetType() ), or use a password: 在运行时,您可以使用代码访问安全性或更简单地实施此操作:使用反射获取调用类的名称( this.GetType() ),或使用密码:

or more simpler: a password requirement: 或更简单:密码要求:

class A {
    private Boolean isAllowedAccess;

    protected A(String password) {
        this.isAllowedAccess = password == "12345abc";
    }

    protected void Foo() {
        if( !this.isAllowedAccess ) throw new InvalidOperationException();
    }
}

class B : A {

    public B() : base("12345abc") {
    }

    void Bar() {
        this.Foo(); // OK
    }
}

class C : A {

    public C() : base(null) {
    }

    void Baz() {
        this.Foo(); // I don't want to permit this
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM