更新时间:2023-02-15 09:33:27
这违反了OOP原则.基础对象存储区的所有属性等都存在于派生对象中.
但是,Microsoft始终会自己这样做!PictureBox
是一个很好的例子.它在属性网格中或通过intellisense在代码编辑器中没有可浏览的Text
属性,但是由于它是从具有Text
属性的System.Windows.Forms.Control
派生而来的,因此仍然存在.若要实现此目的,请对不希望看到的属性,事件和方法执行类似的操作.
This is against OOP principals. All properties etc of the base object shold be present in the deriving object.
However, Microsoft do this themselves all the time! ThePictureBox
is a good example. It has no browsableText
property in the property grid or in the code editor via intellisense, but it''s still there as it derives fromSystem.Windows.Forms.Control
which has theText
property.
The way to achieve this is to do something like this with properties, events and methods that you don''t want to be visible.
using System;<br />using System.ComponentModel;<br />using System.Drawing;<br />using System.Windows.Forms;<br /><br />public class MyPictureBox : PictureBox<br />{<br /> [Browsable(false)]<br /> [EditorBrowsable(EditorBrowsableState.Never)]<br /> [Obsolete("This property is obselete", true)]<br /> public new Color BackColor<br /> {<br /> get { return base.BackColor; }<br /> set { base.BackColor = value; }<br /> }<br />}
这将使BackColor属性消失" IF MyPictureBox为在单独的装配体中.如果在同一程序集中,或者作为对另一个项目的项目引用添加,则它将不起作用-它必须是对单独dll的引用.
This will make the BackColor property ''disappear'' IF MyPictureBox is in a seperate assembly. It will not work if in the same assembly, or added as a project reference to another project - it must be a reference to a seperate dll.
解决此问题的***方法是使用只显示您要显示的属性的包装器类
Probably the best way to approach this is to use a wrapper class that only displays the properties you want to show