如何在后续代中禁用派生类中的设计器

为了在类中禁用组件设计器,只需向其添加[System.ComponentModel.DesignerCategory(“”)]属性即可,但它不适用于任何代中从此类派生的任何类。 例如:

[System.ComponentModel.DesignerCategory("")] public class A:ServiceBase { } //Designer is disabled here public class B:A {} //Designer is enabled here [System.ComponentModel.DesignerCategory("")] public class B:A {} //Designer is enabled here too [System.ComponentModel.DesignerCategory("Code")] public class B:A {} //Designer is enabled even here 

当然,这发生在任何其他世代和排列中。 例如

 //Whatever attribute here public class C:B {} //Designer is enabled here 

有没有人试图摆脱它? 为什么组件模型尝试添加设计器支持,即使它在第一代中明确禁用?

谢谢

这种行为的原因是缓存引用的程序集。 要解决此问题,请删除对包含基本服务器的程序集的引用,并再次添加。 在这种情况下,Visual Studio重建项目并且不会将默认编辑器定义为derrived类。

“属性inheritance”起初让我觉得奇怪,因为我一直认为属性不是inheritance的; 检查docs.microsoft.com之后我发现不一定是这种情况 – 属性可能有Inherited = true ,所以感谢你帮助我拓宽知识:-)

此外,我还必须从.csproj文件中删除一堆Component条目