我有一个属性,它是一个引用类型的用户控件。 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; }