In Designer: Property default value is set, but property isn't called while setting default?

前端 未结 2 524
予麋鹿
予麋鹿 2021-01-20 06:41

I have a piece of code that goes something like this:

[DefaultValue(false)]
public bool Property
{
    set
    {
        blah = value;
        someControl.Vi         


        
2条回答
  •  别那么骄傲
    2021-01-20 07:30

    Two options;

    • take away the default (bool defaults to false anyway)
    • use ShouldSerialize* to make it clear

    in the latter, you might want a bool? to track "explicit set" vs "implicit default":

    private bool? property;
    public bool Property
    {
        set
        {
            property = value;
            someControl.Visible = value;
        }
        get
        {
            return property.GetValueOrDefault();
        }
    }
    public void ResetProperty() { property = null; }
    public bool ShouldSerializeProperty() { return property.HasValue; }
    

    Note that Reset* and ShouldSerialize* are patterns recognized by the component-model.

提交回复
热议问题