C# - Hiding all methods of the UserControl class from a derived one
I have a custom user control. Normally it inherites the UserControl
class. But by this way it inherites all the public methods and properties of UserControl
. But I want to hide all these and implement my own few methods and properties.
Say that I have a custom control named CustomControl
.
public class CustomControl : UserControl
When I create an instance of CustomControl
:
CustomControl cControl = new CustomControl();
When I type cControl.
intellisense offers me all the methods and properties derived from UserControl
. But I wa开发者_运维技巧nt to list only mine which are implemented in CustomControl
class.
You could make an interface, and then only the methods and properties of the interface will be exposed.
public interface ICustomControl {
string MyProperty { get; set;}
}
public class CustomControl : UserControl, ICustomControl {
public string MyProperty { get; set; }
}
...
ICustomControl cControl = new CustomControl();
Then intellisense only shows MyProperty and Object's members (and extension methods, if any apply).
EDIT:
protected ICustomControl CustomControl { get; set; }
public Form1()
{
InitializeComponent();
CustomControl = this.customControl1;
CustomControl.MyProperty = "Hello World!"; // Access everything through here.
}
Then you can make CustomControl's scope internal or protected internal if you want.
That's not how inheritance works. By creating a sub class you are explicitly saying you want all of the base class's methods and properties accessible.
Why not use composition and make the UserControl a member of your custom control instead of inheriting from it?
You can hide them from IntelliSense by shadowing them in your class (redeclaring each inherited method with keyword new
), and applying EditorBrowsableAttribute
to them. But the methods will still be there, and will still be callable. In general, there is no way to disallow clients to call inherited methods on instances of your class.
You have some options:
- Using
EditorBrowsableAttribute
will hide the properties from intellisense - Using
BrowsableAttribute
will hide the properties from property grids - Hiding the properties themselves using "private new" will hide them from everything
Things to consider:
- Using attributes will hide the properties depending on the implementation of the "consumer" but in a language level, you aren't hiding anything. For instance, you could implement a property grid that checks for EditorBrowsableAttribute before showing the property. [I'm not sure about Microsoft's Windows Forms implementation]
- Using "private new" will also prevent you to access the properties, although, inside your control you can yet call
base.PropertyName
to have access to the original ones.
I understand your intention. It's usual to restrict behavior of enherited controls even though it "breaks" the inheritance concept.
Don't inherit UserControl
, inherit Control
instead.
You could do this through aggregation e.g.
public CustomControl
{
private Control control_;
public property control {get{ return _control;}}
.
.
.
public void FunctionIWantExposed() {}
}
But this isn't particularly helpful. You won't be able to add it to any controls collection. You could use the property in the custom class control and add that to a controlscollection but then all those methods you are trying to hide get exposed again.
精彩评论