有一种方法可以实现您想要的——隐藏你不想看到的成员——但让它自动应用,而不需要其他人使用自定义界面进行合作。您可以通过重新引入您不想看到的所有成员并用属性标记它们来做到这一点。
例如,当基类属性对特定后代没有任何意义时,这就是 Windows 窗体所做的。例如,Control 具有 Text 属性,但 Text 属性在 TabControl 上毫无意义。所以 TabControl 覆盖 Text 属性,并为其覆盖添加属性,说“顺便说一下,不要在属性网格或 Intellisense 中显示我的 Text 属性。” 该属性仍然存在,但是由于您从未见过它,因此它不会妨碍您。
如果将[EditorBrowsable(EditorBrowsableState.Never)]属性添加到成员(属性或方法),则 Intellisense 将不再在其代码完成列表中显示该成员。如果我正确理解了您的问题,那么这就是您要实现的重要目标:使应用程序代码很难意外使用该成员。
对于属性,您可能还想添加[Browsable(false)]以从属性网格中隐藏属性,并添加[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]以防止设计器将属性的值写入 .designer.cs 文件。
这些将使意外使用该方法/属性变得非常困难。但是,它们仍然不能保证。如果您确实需要保证,那么也加入一个[Obsolete]属性,并使用“将警告视为错误”进行构建——那么您就可以得到照顾了。
如果基本成员是虚拟的,您可能想要覆盖它,并让您的覆盖简单地调用 base. 不要抛出异常,因为被覆盖的成员可能会在正常的事件过程中被基类调用。另一方面,如果 base 成员不是虚拟的,那么您想使用“new”而不是“override”,并且您可以决定您的实现是否应该调用 base,或者只是抛出异常——没有人应该使用无论如何,您重新引入的成员,所以没关系。
public class Widget : UserControl
{
// The Text property is virtual in the base Control class.
// Override and call base.
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Obsolete("The Text property does not apply to the Widget class.")]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}
// The CanFocus property is non-virtual in the base Control class.
// Reintroduce with new, and throw if anyone dares to call it.
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Obsolete("The CanFocus property does not apply to the Widget class.")]
public new bool CanFocus
{
get { throw new NotSupportedException(); }
}
// The Hide method is non-virtual in the base Control class.
// Note that Browsable and DesignerSerializationVisibility are
// not needed for methods, only properties.
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("The Hide method does not apply to the Widget class.")]
public new void Hide()
{
throw new NotSupportedException();
}
}
是的,这是一项相当多的工作,但你只需要做一次......每个成员,每个班级......嗯,是的。但是如果这些基类成员真的不适用于你的类,并且让他们在那里会引起混乱,那么可能值得努力。