开发者

Hide Virtual function In Inherited Class

开发者 https://www.devze.com 2023-02-18 07:52 出处:网络
I have a class that inherits from LinkButton and I want to hide OnClinentClick from my class. 开发者_运维技巧Somthing like this :

I have a class that inherits from LinkButton and I want to hide OnClinentClick from my class.

开发者_运维技巧Somthing like this :

public class MyClass : LinkButton
{
    // some Code
}

And somewhere in code:

MyClass myclass = new MyClass();
MyClass.OnClinentClick = "";//this line must not be accessable


Hiding something from a class definition is not directly supported as it breaks OOP principles.

You could use the new operator, however, I wouldn't advise it. Personally, I would think about my design and/or use a NotSupportedException if there is no other way around it.


You can use the EditorBrowsableAttribute to prevent it from being suggested by IntelliSense, but you can't get rid of it entirely.

[EditorBrowsable(EditorBrowsableState.Never)]
public virtual string OnClientClick { get; set; }

C# only supports public inheritance. You shouldn't be inheriting from a class whose methods don't make sense for all derived classes. Consider composition instead of inheritance to solve this problem.


You can override the function (that is, replace the base implementation with your one, as long as the former is virtual), but you cannot completely prevent the clients from calling the base class function if you hide it with new, as they may always cast to the base class.

Update:
actually, you cannot change the access from public to protected/private when overriding, this won't compile (http://ideone.com/Y65Uh). Besides that, if you use new to hide the base function and make it uncallable, the original function is still visible (http://ideone.com/xiL2F). If you declare the new function public (which is perhaps not what you want), the old function can still be called by casting to the base class (http://ideone.com/A3Bji).


How about making giving it a lower visibility. One of protected and internal might be what you want. Of course that doesn't remove such a member from the derived class, but just removes them from the public interface. It also requires control over the base-class. No idea if LinkButton is one of your classes.

You could also hide the property by reintroducing a new one with the same. But that's a bad idea, and casting back to the base-class allows outsiders to access it.

And you should consider using a has-a relationship instead of an is-a. i.e. Don't derive from a base class if you don't want all its public members. This violates OOP principles such as that it should be possible to substitute the derived class where the base class is expected.

You could also override it, and make the setter throw a NotSupportedException. But that's ugly too because it will only show an error at runtime instead of compiletime. You can generate compile-time warnings with attributes such as the ObsoleteAttribute.

0

精彩评论

暂无评论...
验证码 换一张
取 消