C# - 从派生类中隐藏 UserControl 类的所有方法
我有一个自定义用户控件。通常它继承UserControl
类。但通过这种方式它继承了UserControl
的所有公共方法和属性。但我想隐藏所有这些并实现我自己的一些方法和属性。
假设我有一个名为 CustomControl
的自定义控件。
public class CustomControl : UserControl
当我创建 CustomControl
的实例时:
CustomControl cControl = new CustomControl();
当我输入 cControl.
时,intellisense 为我提供从 UserControl
派生的所有方法和属性。但我只想列出在 CustomControl
类中实现的我的。
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 want to list only mine which are implemented in CustomControl
class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以创建一个接口,然后只公开该接口的方法和属性。
...
然后智能感知仅显示 MyProperty 和 Object 的成员(以及扩展方法,如果有的话)。
编辑:
然后,如果需要,您可以将 CustomControl 的范围设为内部或受保护的内部。
You could make an interface, and then only the methods and properties of the interface will be exposed.
...
Then intellisense only shows MyProperty and Object's members (and extension methods, if any apply).
EDIT:
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.
为什么不使用组合并使 UserControl 成为自定义控件的成员而不是从它继承?
Why not use composition and make the UserControl a member of your custom control instead of inheriting from it?
您可以通过在类中隐藏它们(使用关键字
new
重新声明每个继承的方法)并向它们应用EditorBrowsableAttribute
来对 IntelliSense 隐藏它们。但这些方法仍然存在,并且仍然可以调用。一般来说,没有办法禁止客户端调用类实例上的继承方法。You can hide them from IntelliSense by shadowing them in your class (redeclaring each inherited method with keyword
new
), and applyingEditorBrowsableAttribute
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.您有一些选择:
EditorBrowsableAttribute
将从智能感知中隐藏属性BrowsableAttribute
将从属性网格中隐藏属性需要考虑的事情:
base.PropertyName
来访问属性原来的。我明白你的意图。通常会限制继承控件的行为,即使它“破坏”了继承概念。
You have some options:
EditorBrowsableAttribute
will hide the properties from intellisenseBrowsableAttribute
will hide the properties from property gridsThings to consider:
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.
不要继承
UserControl
,而是继承Control
。Don't inherit
UserControl
, inheritControl
instead.您可以通过聚合来做到这一点,例如
但这并不是特别有用。您将无法将其添加到任何控件集合中。您可以使用自定义类控件中的属性并将其添加到控件集合中,但随后您尝试隐藏的所有这些方法都会再次暴露。
You could do this through aggregation e.g.
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.