Setting DefaultValue for mutable type properties?

I have a Windows.Forms component that has a mySize property that returns a size structure. My intention was for this property to calculate the returned mySize automatically depending on the size of the component, if mySize was not set explicitly, in which case return the value set mySize. Unfortunately, now when I embed the component in the form, the Windows Forms developer decided to start the explicit generation and set the value of the mySize property, which correctly clicks me.

So I need to set DefaultValue so that the Designer leaves and leaves me alone.

I read the answers regarding System.ComponentModel.DefaultValue, so I know that I have to manually set the property value in the constructor, but the answers and documentation that I found relate to setting DefaultValue of False, constant.

The dimensional structure is not constant, so the VB compiler gets confused, telling me that I cannot set the size as the DefaultValue for the font size property, because sizes are not constants.

It makes my brain hurt.

I can probably create code around the problem by creating the getMySize and setMySize methods instead of using the property, but I would like to know if there is any way to set the default property for size.

NB: I do not use mySize as some kind of abnormal attempt to override the Size property (which has a DefaultValue value of 150x150, so SOMETHING seems to be able to set DefaultValues ​​for sizes); mySize is just the size value required by the class.

+3
source share
2 answers

Instead of using the DefaultValue attribute, write the following two methods:

bool ShouldSerializemySize() { ... }
void ResetmySize() { ... }

In ShouldSerializemySize, return true if the value should be serialized for code. In ResetmySize, the reset property has a default value.

The component designer will automatically select these methods using reflection.

More details here: http://msdn.microsoft.com/en-us/library/53b8022e(VS.71).aspx

+5

, , "" " " DefaultValue MSDN, DefaultValue, .

, , , , MSDN, , .

( - ) .

0

Source: https://habr.com/ru/post/1707469/


All Articles