简体   繁体   中英

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

B is derived from A and also C is derived from A

I want only B can access the method of A , C an not access of that same method of 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#

I think this look like a problem for 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 )

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

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

    }
}

Create B class which inherit from A and will have access to all functionality including YourFooMethod

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

And finally your C class which have all base functionality except YourFooMethod method

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. That would be very difficult to maintain, and class A should not need to know about every class that extends it.

What you are trying to do is really honestly a bad idea.

C# (and .NET in general) has the access modifiers:

  • public - Anyone can access
  • private - Only the containing scope/type can access
  • protected - Only the containing type and its derived types can access
  • internal - Only types defined in the same Assembly (or InternalsVisibleTo Assemblies) can access
  • protected internal - The set-union of protected and internal can access.

You're asking for something in-between private and protected , where you can manually restrict access to named types.

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.

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:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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