Properties will not be serialized to .designer.cs

In VS2010, control properties will not be serialized, despite the ShouldSerializeFoo method, also with designer syntax. Visible / Content.

Here is the code:

class Class1 : UserControl {
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public string Foo {
        get; set;
    }
    public bool ShouldSerializeFoo() {
        return true;
    }
    public Class1() {
        Foo = "dupa";
    }
}

However, the constructor does not generate anything for this property:

        // 
        // class11
        // 
        this.class11.Location = new System.Drawing.Point(224, 262);
        this.class11.Name = "class11";
        this.class11.Size = new System.Drawing.Size(150, 150);
        this.class11.TabIndex = 2;
        this.class11.Load += new System.EventHandler(this.class11_Load);
+3
source share
1 answer

You are mixing serialization schemes. Serialization of the constructor (for which it is intended DesignerSerializationVisibility) has nothing to do with the instance serialization mechanism (which works ShouldSerializeXXX, among many other things).

DesignerSerializationVisibility.Content a string ( ). :

  • - -
  • -
  • - .

Visible. , Content . :

public class MyControl : Control
{
    public class SomeOptions
    {
        public string Option1 { get; set; }
        public string Option2 { get; set; }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public SomeOptions Options { get; private set; }

    public string Foo { get; set; }
}

, , :

// myControl1
this.myControl1.Foo = "value";
this.myControl1.Options.Option1 = "option1";
this.myControl1.Options.Option2 = "option2";

, , ; Content , ( SomeOptions), .

, , Content string. string , . Visible, ( ).

, , ( ) . - DefaultValue . , , , ( , , ), :/p >

[DefaultValue("foo")]
public string Foo { get; set; }

, Foo "foo", .

+9

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


All Articles