[ACCEPTED]-How to hide (remove) a base class's methods in C#?-inheritance-prevention

Accepted answer
Score: 23

Obsolete It

In class B, override MethodToHide and add 7 the Obsolete attribute

[Obsolete("Reason", true)] // true will cause a compile-time error

Set EditorBrowsable

(As mentioned previously)

In 6 class B, override MethodToHide and add the 5 EditorBrowsable attribute

[System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]

Throw exception

(As mentioned previously)

In 4 class B, override MethodToHide and throw 3 exception.

Create Wrapper

I think Michael Meadows is right. Use 2 the Adapter pattern. This pattern also allows easier mocking 1 of code when unit testing.

class B: IInterface
{    
    protected void MethodToExpose()
    {
        A a = new A();
        a.MethodToExpose();
    }

    protected void NewMethodInB()
    {
    }
}
Score: 21

You can't do it and preserve the hierarchy. If 8 possible, you should create interfaces that 7 define your ideal, then subclass the bases 6 classes and implement the interfaces. reference 5 the interfaces only (not the base class 4 types) in your code.

The Adapter pattern was created specifically 3 to solve the problem of how to use a framework 2 when its API doesn't line up exactly with 1 your needs.

Score: 14

If you want to custom tailor a set of features, I'd 11 say you want to make a wrapper rather than 10 inherit the functionality. I don't know 9 of a way to do what you want in C# either.

Think 8 about it, what if some code outside your 7 control wants this NHRepository instance for something... but 6 you've removed the functionality from the 5 function it needs in your child class (which 4 you would send in, since that's your only 3 NHRepository instance.) Everything goes boom. That's 2 why I don't think it's even possible to 1 do without some ugly hack.

Score: 6

If you are using Visual Studio, you can 5 hide the methods/properties from intelliprompt 4 with this attribute:

class A
{
    protected void MethodToExpose()
    {}

    [System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
    protected void MethodToHide(object param)
    {}
}

It won't actually get 3 rid of the function, but if they are just 2 your internal people. It might be close 1 enough.

Score: 4

In my opinion, the best way to do it, if 8 you just want to hide and not really override 7 and externalize a new functionallity, is 6 just to add an EditorBrowsableState attribute. It 5 hides the the method in the visual studio 4 editor. Who ever will try to use it will 3 end up with a compile time error.

Just add 2 this code on top of your method:

[System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]

For example

public class User
{        
    public void DoSomething()
    {
       Something...
    }
}

public class Manager
{
    [System.ComponentModel.EditorBrowsable(EditorBrowsableState.Never)]
    public override void DoSomething()
    { }
}

void main()
{
   User user = new User();
   user.DoSomething();

   Manager manager = new Manager();
   manager.DoSomething(); // --------- This row will throw a design time error
}

Good 1 Luck :)

Score: 1

C# does not have a concept similar to protected 10 or private inheritance in C++.

Your best 9 option is to aggregate an instance of the 8 class and expose the set of methods you 7 are interested in you consumers having access 6 to.

Although in your case I don't think it's 5 possible, you could look into creating an 4 interface that exposes just the common functionality 3 that you want consumers be work with so 2 that your wrapper can be substitutable in 1 some instances for it's aggregate.

Score: 1

I never knew you could do that in C++ though 5 I don't know much about C++. I'm afraid 4 that I agree with Blixt that a wrapper class is probably 3 how I'd implement this.

It might work (but 2 I'm not sure) to simply override the function and throw an exception 1 upon a call....

Score: 1

As far as I know you can't hide it in the 5 way you want. I think you could prevent 4 it from being used in any practical manner 3 though:

class B : A
{
    public new void MethodToHide(object param)
    { 
        throw new DontUseThisMethodException();
    }

    protected void NewMethodInB()
    {}
}

Not the nicest thing to do though, so 2 you would probably want to solve it in some 1 other way...

Score: 1

Actually you CAN hide A's method from C 7 if you would have defined B's method as 6 accessible from C.

The only problem with 5 your code is that you use "private" on your 4 hiding declaration... if you use protected 3 or public you would not have any issues 2 and it would operate as you expect. I do 1 this all the time with fields.

Score: 0

Your code in derived class B won't hide 5 the base method from all derived types, only 4 itself. You would have to set the method 3 to private in the base. The only way around 2 this issue is to create another base class 1 that does not expose this method.

More Related questions