如何prevent Windows窗体设计从生成的属性默认值分配? [英] How to prevent Windows Forms designer from generating default value assignments for properties?

查看:104
本文介绍了如何prevent Windows窗体设计从生成的属性默认值分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属性,它是一个引用类型的用户控件。 Windows窗体设计器生成不断code,可分配属性的初始值设置为null。我怎样才能prevent呢?

I have a user control with a property that is of a reference type. The Windows Forms designer keeps generating code that assigns the property's initial value to null. How can I prevent this?

我尝试添加复位和ShouldSerialize方法 - 复位有一个空的身体和ShouldSerialize总是返回假 - 但没有奏效。我还应用了BrowsableAttribute并将其设置为false。

I tried adding Reset and ShouldSerialize methods -- Reset had an empty body and ShouldSerialize always returned false -- but that did not work. I also applied the BrowsableAttribute and set it to false.

编辑:

属性的类型是一类在同一个项目。这不是一个组件或控件,只是一个普通的类都是从Object继承。此外,该属性的setter方法​​调用使用属性的值作为参数和方法不接受null作为一个有效参数的方法。

The property's type is a class in the same project. It's not a component or control, just a plain class inheriting from Object. Also, the property's setter calls a method using the property's value as its argument and the method does not accept null as a valid argument.

例如:

public MyClass Property1 
{
   get { return _property1; }
   set
   {
        _property1 = value;
        SomeMethod(value); // This method throws ArgumentNullException;
   }
}

注:我知道,get和set方法可能更适合这里。

Note: I do realize that get and set methods would probably be more appropriate here.

推荐答案

低迷,我可以用一个片段。告诉大家,它不应该永远序列化属性的值的设计师:

Murky, I could use a snippet. Tell the designer that it shouldn't ever serialize the value of the property:

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public Image Aardvark { get; set; }

这篇关于如何prevent Windows窗体设计从生成的属性默认值分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆