【发布时间】:2011-06-16 16:09:46
【问题描述】:
我从中派生了一个基类(黑盒;不能修改它)。在我的派生类中,我不再希望允许用户使用属性,而是现在有一个公共函数可供使用。我不会详细说明原因(无关紧要),但这是我试图隐藏的属性:
public ComboBoxItemCollection Items { get; }
我试过了,但是没用:
private new ComboBoxItemCollection Items { get; set; }
我也试过这个,但是编译器说我不允许将两个访问器都设为私有:
public new ComboBoxItemCollection Items { private get; private set; }
我该如何正确地做到这一点?请注意,我并不依赖于这是一个完整的安全解决方案,显然通过转换为基类他们仍然可以调用此属性。这只是为了给用户一个编译时错误,这有助于引导他们意识到他们不能使用这个属性,而应该使用派生类中的新函数。
编辑
感谢这个帖子中的答案,我想出了以下解决方案:
[Obsolete( "This property is obsolete. Please use MRUIdComboBox.AddItem() instead.", true )]
public new ComboBoxItemCollection Items
{
get
{
throw new NotSupportedException( "This property is not supported. Please use MRUIdComboBox.AddItem() instead." );
}
}
【问题讨论】:
标签: c# method-hiding