【问题标题】:Restrict Access Modifier of Form Class Properties in C#在 C# 中限制表单类属性的访问修饰符
【发布时间】:2019-04-27 11:14:22
【问题描述】:
我创建了一个继承 Windows.FORM 的 DLL 类,我想限制它的 properties Access Modifier比如 Size(width-height) & FormBorderStyle to Private
在另一个程序集中无法访问。我应该怎么做,什么是相关的?
也许使用抽象类?谢谢你的帮助
【问题讨论】:
标签:
c#
winforms
inheritance
properties
access-modifiers
【解决方案1】:
不要这样做。
access modifiers 旨在指导开发人员。它们绝不提供任何防止使用的保护。
想要访问它们的开发人员能够访问它们,即使它们是私有的,并且弄乱框架默认值可能会导致严重的问题。
或者...
如果您的表单是一个完全独立的功能或特性,请在表单周围制作一个包装器。
例如:
//the wrapper
public class PropertyPages : IPropertyPages
{
//your wrapped form...
private YourForm _propertyForm = new YourForm();
//a public show, but the form itself remain inaccessible.
public void Show()
{
_propertyForm.Show();
}
}