Derived collection property not serialized in designer












1














I have class which derives from a collection which has a property MyString, i.e.



  public class CollectionItem
{
public bool MyBoolean { get; set; }

public int MyInteger { get; set; }
}

public class MyCollection : List<CollectionItem>
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string MyString { get; set; }

public MyCollection()
{
MyString = "Hello";
}
}


This collection is part of a simple DummyControl, i.e.



  public class DummyControl : System.Windows.Forms.Control
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(System.Drawing.Brushes.Yellow, e.ClipRectangle);
}

public DummyControl()
{
MyCollection = new MyCollection();

for (int i = 0; i < 3; i++)
{
CollectionItem item = new CollectionItem();
item.MyInteger = i;
item.MyBoolean = i > 1;
MyCollection.Add(item);
}
}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public MyCollection MyCollection { get; set; }
}


When I put this DummyControl on a Windows Form, everything is serialized as expected to the Form Designer except the MyString property - this is what I get:



  // 
// dummyControl1
//
this.dummyControl1.Location = new System.Drawing.Point(66, 62);
collectionItem1.MyBoolean = false;
collectionItem1.MyInteger = 0;
collectionItem2.MyBoolean = false;
collectionItem2.MyInteger = 1;
collectionItem3.MyBoolean = true;
collectionItem3.MyInteger = 2;
this.dummyControl1.MyCollection.Add(collectionItem1);
this.dummyControl1.MyCollection.Add(collectionItem2);
this.dummyControl1.MyCollection.Add(collectionItem3);
this.dummyControl1.Name = "dummyControl1";
this.dummyControl1.Size = new System.Drawing.Size(338, 266);
this.dummyControl1.TabIndex = 0;
this.dummyControl1.Text = "dummyControl1";


Can anybody please tell me what more I have to do in order to have the MyString property serialized to the Form Designer?










share|improve this question



























    1














    I have class which derives from a collection which has a property MyString, i.e.



      public class CollectionItem
    {
    public bool MyBoolean { get; set; }

    public int MyInteger { get; set; }
    }

    public class MyCollection : List<CollectionItem>
    {
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string MyString { get; set; }

    public MyCollection()
    {
    MyString = "Hello";
    }
    }


    This collection is part of a simple DummyControl, i.e.



      public class DummyControl : System.Windows.Forms.Control
    {
    protected override void OnPaint(PaintEventArgs e)
    {
    base.OnPaint(e);
    e.Graphics.FillRectangle(System.Drawing.Brushes.Yellow, e.ClipRectangle);
    }

    public DummyControl()
    {
    MyCollection = new MyCollection();

    for (int i = 0; i < 3; i++)
    {
    CollectionItem item = new CollectionItem();
    item.MyInteger = i;
    item.MyBoolean = i > 1;
    MyCollection.Add(item);
    }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public MyCollection MyCollection { get; set; }
    }


    When I put this DummyControl on a Windows Form, everything is serialized as expected to the Form Designer except the MyString property - this is what I get:



      // 
    // dummyControl1
    //
    this.dummyControl1.Location = new System.Drawing.Point(66, 62);
    collectionItem1.MyBoolean = false;
    collectionItem1.MyInteger = 0;
    collectionItem2.MyBoolean = false;
    collectionItem2.MyInteger = 1;
    collectionItem3.MyBoolean = true;
    collectionItem3.MyInteger = 2;
    this.dummyControl1.MyCollection.Add(collectionItem1);
    this.dummyControl1.MyCollection.Add(collectionItem2);
    this.dummyControl1.MyCollection.Add(collectionItem3);
    this.dummyControl1.Name = "dummyControl1";
    this.dummyControl1.Size = new System.Drawing.Size(338, 266);
    this.dummyControl1.TabIndex = 0;
    this.dummyControl1.Text = "dummyControl1";


    Can anybody please tell me what more I have to do in order to have the MyString property serialized to the Form Designer?










    share|improve this question

























      1












      1








      1







      I have class which derives from a collection which has a property MyString, i.e.



        public class CollectionItem
      {
      public bool MyBoolean { get; set; }

      public int MyInteger { get; set; }
      }

      public class MyCollection : List<CollectionItem>
      {
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
      public string MyString { get; set; }

      public MyCollection()
      {
      MyString = "Hello";
      }
      }


      This collection is part of a simple DummyControl, i.e.



        public class DummyControl : System.Windows.Forms.Control
      {
      protected override void OnPaint(PaintEventArgs e)
      {
      base.OnPaint(e);
      e.Graphics.FillRectangle(System.Drawing.Brushes.Yellow, e.ClipRectangle);
      }

      public DummyControl()
      {
      MyCollection = new MyCollection();

      for (int i = 0; i < 3; i++)
      {
      CollectionItem item = new CollectionItem();
      item.MyInteger = i;
      item.MyBoolean = i > 1;
      MyCollection.Add(item);
      }
      }

      [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
      public MyCollection MyCollection { get; set; }
      }


      When I put this DummyControl on a Windows Form, everything is serialized as expected to the Form Designer except the MyString property - this is what I get:



        // 
      // dummyControl1
      //
      this.dummyControl1.Location = new System.Drawing.Point(66, 62);
      collectionItem1.MyBoolean = false;
      collectionItem1.MyInteger = 0;
      collectionItem2.MyBoolean = false;
      collectionItem2.MyInteger = 1;
      collectionItem3.MyBoolean = true;
      collectionItem3.MyInteger = 2;
      this.dummyControl1.MyCollection.Add(collectionItem1);
      this.dummyControl1.MyCollection.Add(collectionItem2);
      this.dummyControl1.MyCollection.Add(collectionItem3);
      this.dummyControl1.Name = "dummyControl1";
      this.dummyControl1.Size = new System.Drawing.Size(338, 266);
      this.dummyControl1.TabIndex = 0;
      this.dummyControl1.Text = "dummyControl1";


      Can anybody please tell me what more I have to do in order to have the MyString property serialized to the Form Designer?










      share|improve this question













      I have class which derives from a collection which has a property MyString, i.e.



        public class CollectionItem
      {
      public bool MyBoolean { get; set; }

      public int MyInteger { get; set; }
      }

      public class MyCollection : List<CollectionItem>
      {
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
      public string MyString { get; set; }

      public MyCollection()
      {
      MyString = "Hello";
      }
      }


      This collection is part of a simple DummyControl, i.e.



        public class DummyControl : System.Windows.Forms.Control
      {
      protected override void OnPaint(PaintEventArgs e)
      {
      base.OnPaint(e);
      e.Graphics.FillRectangle(System.Drawing.Brushes.Yellow, e.ClipRectangle);
      }

      public DummyControl()
      {
      MyCollection = new MyCollection();

      for (int i = 0; i < 3; i++)
      {
      CollectionItem item = new CollectionItem();
      item.MyInteger = i;
      item.MyBoolean = i > 1;
      MyCollection.Add(item);
      }
      }

      [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
      public MyCollection MyCollection { get; set; }
      }


      When I put this DummyControl on a Windows Form, everything is serialized as expected to the Form Designer except the MyString property - this is what I get:



        // 
      // dummyControl1
      //
      this.dummyControl1.Location = new System.Drawing.Point(66, 62);
      collectionItem1.MyBoolean = false;
      collectionItem1.MyInteger = 0;
      collectionItem2.MyBoolean = false;
      collectionItem2.MyInteger = 1;
      collectionItem3.MyBoolean = true;
      collectionItem3.MyInteger = 2;
      this.dummyControl1.MyCollection.Add(collectionItem1);
      this.dummyControl1.MyCollection.Add(collectionItem2);
      this.dummyControl1.MyCollection.Add(collectionItem3);
      this.dummyControl1.Name = "dummyControl1";
      this.dummyControl1.Size = new System.Drawing.Size(338, 266);
      this.dummyControl1.TabIndex = 0;
      this.dummyControl1.Text = "dummyControl1";


      Can anybody please tell me what more I have to do in order to have the MyString property serialized to the Form Designer?







      c# .net winforms windows-forms-designer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 9:05









      Shunyata Kharg

      47221125




      47221125
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Default CodeDom collection serializer doesn't serialize properties of the collection. You can create a custom serializer by deriving from CodeDomSerializer. Then register the custom serializer for your class:



          [DesignerSerializer(typeof(MyCollectionSerializer), typeof(CodeDomSerializer))]
          public class MyCollection : List<CollectionItem>


          MyCollectionSerializer



          This serializer serializes MyString property of MyCollection:



          using System.CodeDom;
          using System.ComponentModel;
          using System.ComponentModel.Design.Serialization;
          public class MyCollectionSerializer : CodeDomSerializer
          {
          public override object Serialize(IDesignerSerializationManager manager, object value)
          {
          var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
          typeof(MyCollection).BaseType, typeof(CodeDomSerializer));
          var statements = (CodeStatementCollection)baseSerializer.Serialize(manager, value);
          var property = TypeDescriptor.GetProperties(value)[nameof(MyCollection.MyString)];
          if (property.ShouldSerializeValue(value))
          {
          var targetObject = base.GetExpression(manager, value);
          var cpre = new CodePropertyReferenceExpression(targetObject, property.Name);
          var cpe = new CodePrimitiveExpression(property.GetValue(value));
          var cas = new CodeAssignStatement(cpre, cpe);
          statements.Add(cas);
          }
          return statements;
          }
          }


          Then MyString property will be serialized as well:



          // 
          // dummyControl1
          //
          ...
          this.dummyControl1.MyCollection.Add(collectionItem1);
          this.dummyControl1.MyCollection.Add(collectionItem2);
          this.dummyControl1.MyCollection.Add(collectionItem3);
          this.dummyControl1.MyCollection.MyString = "Hello";
          ...





          share|improve this answer























          • Thank you Reza!
            – Shunyata Kharg
            Nov 12 at 9:04










          • You're welcome :)
            – Reza Aghaei
            Nov 12 at 9:17











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186302%2fderived-collection-property-not-serialized-in-designer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Default CodeDom collection serializer doesn't serialize properties of the collection. You can create a custom serializer by deriving from CodeDomSerializer. Then register the custom serializer for your class:



          [DesignerSerializer(typeof(MyCollectionSerializer), typeof(CodeDomSerializer))]
          public class MyCollection : List<CollectionItem>


          MyCollectionSerializer



          This serializer serializes MyString property of MyCollection:



          using System.CodeDom;
          using System.ComponentModel;
          using System.ComponentModel.Design.Serialization;
          public class MyCollectionSerializer : CodeDomSerializer
          {
          public override object Serialize(IDesignerSerializationManager manager, object value)
          {
          var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
          typeof(MyCollection).BaseType, typeof(CodeDomSerializer));
          var statements = (CodeStatementCollection)baseSerializer.Serialize(manager, value);
          var property = TypeDescriptor.GetProperties(value)[nameof(MyCollection.MyString)];
          if (property.ShouldSerializeValue(value))
          {
          var targetObject = base.GetExpression(manager, value);
          var cpre = new CodePropertyReferenceExpression(targetObject, property.Name);
          var cpe = new CodePrimitiveExpression(property.GetValue(value));
          var cas = new CodeAssignStatement(cpre, cpe);
          statements.Add(cas);
          }
          return statements;
          }
          }


          Then MyString property will be serialized as well:



          // 
          // dummyControl1
          //
          ...
          this.dummyControl1.MyCollection.Add(collectionItem1);
          this.dummyControl1.MyCollection.Add(collectionItem2);
          this.dummyControl1.MyCollection.Add(collectionItem3);
          this.dummyControl1.MyCollection.MyString = "Hello";
          ...





          share|improve this answer























          • Thank you Reza!
            – Shunyata Kharg
            Nov 12 at 9:04










          • You're welcome :)
            – Reza Aghaei
            Nov 12 at 9:17
















          1














          Default CodeDom collection serializer doesn't serialize properties of the collection. You can create a custom serializer by deriving from CodeDomSerializer. Then register the custom serializer for your class:



          [DesignerSerializer(typeof(MyCollectionSerializer), typeof(CodeDomSerializer))]
          public class MyCollection : List<CollectionItem>


          MyCollectionSerializer



          This serializer serializes MyString property of MyCollection:



          using System.CodeDom;
          using System.ComponentModel;
          using System.ComponentModel.Design.Serialization;
          public class MyCollectionSerializer : CodeDomSerializer
          {
          public override object Serialize(IDesignerSerializationManager manager, object value)
          {
          var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
          typeof(MyCollection).BaseType, typeof(CodeDomSerializer));
          var statements = (CodeStatementCollection)baseSerializer.Serialize(manager, value);
          var property = TypeDescriptor.GetProperties(value)[nameof(MyCollection.MyString)];
          if (property.ShouldSerializeValue(value))
          {
          var targetObject = base.GetExpression(manager, value);
          var cpre = new CodePropertyReferenceExpression(targetObject, property.Name);
          var cpe = new CodePrimitiveExpression(property.GetValue(value));
          var cas = new CodeAssignStatement(cpre, cpe);
          statements.Add(cas);
          }
          return statements;
          }
          }


          Then MyString property will be serialized as well:



          // 
          // dummyControl1
          //
          ...
          this.dummyControl1.MyCollection.Add(collectionItem1);
          this.dummyControl1.MyCollection.Add(collectionItem2);
          this.dummyControl1.MyCollection.Add(collectionItem3);
          this.dummyControl1.MyCollection.MyString = "Hello";
          ...





          share|improve this answer























          • Thank you Reza!
            – Shunyata Kharg
            Nov 12 at 9:04










          • You're welcome :)
            – Reza Aghaei
            Nov 12 at 9:17














          1












          1








          1






          Default CodeDom collection serializer doesn't serialize properties of the collection. You can create a custom serializer by deriving from CodeDomSerializer. Then register the custom serializer for your class:



          [DesignerSerializer(typeof(MyCollectionSerializer), typeof(CodeDomSerializer))]
          public class MyCollection : List<CollectionItem>


          MyCollectionSerializer



          This serializer serializes MyString property of MyCollection:



          using System.CodeDom;
          using System.ComponentModel;
          using System.ComponentModel.Design.Serialization;
          public class MyCollectionSerializer : CodeDomSerializer
          {
          public override object Serialize(IDesignerSerializationManager manager, object value)
          {
          var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
          typeof(MyCollection).BaseType, typeof(CodeDomSerializer));
          var statements = (CodeStatementCollection)baseSerializer.Serialize(manager, value);
          var property = TypeDescriptor.GetProperties(value)[nameof(MyCollection.MyString)];
          if (property.ShouldSerializeValue(value))
          {
          var targetObject = base.GetExpression(manager, value);
          var cpre = new CodePropertyReferenceExpression(targetObject, property.Name);
          var cpe = new CodePrimitiveExpression(property.GetValue(value));
          var cas = new CodeAssignStatement(cpre, cpe);
          statements.Add(cas);
          }
          return statements;
          }
          }


          Then MyString property will be serialized as well:



          // 
          // dummyControl1
          //
          ...
          this.dummyControl1.MyCollection.Add(collectionItem1);
          this.dummyControl1.MyCollection.Add(collectionItem2);
          this.dummyControl1.MyCollection.Add(collectionItem3);
          this.dummyControl1.MyCollection.MyString = "Hello";
          ...





          share|improve this answer














          Default CodeDom collection serializer doesn't serialize properties of the collection. You can create a custom serializer by deriving from CodeDomSerializer. Then register the custom serializer for your class:



          [DesignerSerializer(typeof(MyCollectionSerializer), typeof(CodeDomSerializer))]
          public class MyCollection : List<CollectionItem>


          MyCollectionSerializer



          This serializer serializes MyString property of MyCollection:



          using System.CodeDom;
          using System.ComponentModel;
          using System.ComponentModel.Design.Serialization;
          public class MyCollectionSerializer : CodeDomSerializer
          {
          public override object Serialize(IDesignerSerializationManager manager, object value)
          {
          var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
          typeof(MyCollection).BaseType, typeof(CodeDomSerializer));
          var statements = (CodeStatementCollection)baseSerializer.Serialize(manager, value);
          var property = TypeDescriptor.GetProperties(value)[nameof(MyCollection.MyString)];
          if (property.ShouldSerializeValue(value))
          {
          var targetObject = base.GetExpression(manager, value);
          var cpre = new CodePropertyReferenceExpression(targetObject, property.Name);
          var cpe = new CodePrimitiveExpression(property.GetValue(value));
          var cas = new CodeAssignStatement(cpre, cpe);
          statements.Add(cas);
          }
          return statements;
          }
          }


          Then MyString property will be serialized as well:



          // 
          // dummyControl1
          //
          ...
          this.dummyControl1.MyCollection.Add(collectionItem1);
          this.dummyControl1.MyCollection.Add(collectionItem2);
          this.dummyControl1.MyCollection.Add(collectionItem3);
          this.dummyControl1.MyCollection.MyString = "Hello";
          ...






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 20:35

























          answered Nov 11 at 20:16









          Reza Aghaei

          63.7k851150




          63.7k851150












          • Thank you Reza!
            – Shunyata Kharg
            Nov 12 at 9:04










          • You're welcome :)
            – Reza Aghaei
            Nov 12 at 9:17


















          • Thank you Reza!
            – Shunyata Kharg
            Nov 12 at 9:04










          • You're welcome :)
            – Reza Aghaei
            Nov 12 at 9:17
















          Thank you Reza!
          – Shunyata Kharg
          Nov 12 at 9:04




          Thank you Reza!
          – Shunyata Kharg
          Nov 12 at 9:04












          You're welcome :)
          – Reza Aghaei
          Nov 12 at 9:17




          You're welcome :)
          – Reza Aghaei
          Nov 12 at 9:17


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186302%2fderived-collection-property-not-serialized-in-designer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ