简体   繁体   English

继承只读属性

[英]inheriting a read-only property

In Microsoft documentation, the declaration for the read-only property Controls of the Control class is as so: 在Microsoft文档中,Control类的只读属性Controls的声明如下:

[BrowsableAttribute(false)]
public Control.ControlCollection Controls { get; }

I am inheriting this property. 我正在继承此属性。 And hiding it: 并将其隐藏:

[EditorBrowsable(EditorBrowsableState.Never)]
[BrowsableAttribute(false)]
[ComVisible(false)]
public new Control.ControlCollection Controls { get; }

but this doesn't compile? 但这不能编译吗? Error: 'UserControl1.Controls.get' must declare a body because it is not marked abstract or extern. 错误:“ UserControl1.Controls.get”必须声明一个主体,因为它没有被标记为抽象或外部。 Automatically implemented properties must define both get and set accessors. 自动实现的属性必须定义get和set访问器。

So I change it: 所以我改变了它:

[EditorBrowsable(EditorBrowsableState.Never)]
[BrowsableAttribute(false)]
[ComVisible(false)]
public new Control.ControlCollection Controls { 
    get { return null; } }

Which does compile. 哪个编译。 But I need a bit of the properties functionality, and not to completely nullify it. 但是我需要一些属性功能,而不是完全使其无效。 (I must apologize to the early respondents to this post for my considerable edit here.) (Within 30 seconds!!) (对于此处的大量修改,我必须向这篇文章的早期回复者表示歉意。)(在30秒之内!!!)
I must change my script... so I try: 我必须更改脚本...所以我尝试:

[EditorBrowsable(EditorBrowsableState.Never)]
[BrowsableAttribute(false)]
[ComVisible(false)]
public new Control.ControlCollection Controls { get; private set; }

This too won't compile: Error: The accessibility modifier of the 'UserControl1.Controls.set' accessor must be more restrictive than the property or indexer 'UserControl1.Controls' 这也将无法编译: 错误:“ UserControl1.Controls.set”访问器的可访问性修饰符必须比属性或索引器“ UserControl1.Controls”更具限制性。

What's to be done about this? 对此该怎么办?

This is what I suggest. 这就是我的建议。

[EditorBrowsable(EditorBrowsableState.Never)]
[BrowsableAttribute(false)]
[ComVisible(false)]
public new Control.ControlCollection Controls { 
    get { return base.Controls; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM