如果C#属性是子 - 子关系,则它们不在designer.cs中序列化

时间:2014-12-04 06:43:30

标签: c# visual-studio serialization designer

我绝对不知道为什么设计器序列化在这种情况下不能完全正常工作。 对我来说,看起来设计师不会序列化2秒子图层(Parent.ClassProperty.ClassProperty.ValueProperty)

这就是我所拥有的(简化) `

class MyClassA : System.Windows.Forms.Panel
{
   public string Name{get; set;}

   [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
   public MyClassB Property1 {get; private set;}
}

class MyClassB
{
   public string Name{get; set;}

   [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
   public MyClassC Property1 {get; private set;}

   [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
   public BindingList<MyClassC> Test{get; private set;}
}

class MyClassC
{
   public string Name{get; set;}
}

`

designer.cs中的预期结果是:

`

classA1.Name = "Test1";
classA1.Property1.Name = "Test2";
classA1.Property1.Property1.Name = "Test3";

`

但是我得到了

`

classA1.Name = "Test1";
classA1.Property1.Name = "Test2";

`

有趣的事实:B的绑定列表属性序列化工作正常但不是C类属性。我绝对不知道为什么!

有人可以帮忙解决问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个

[Serializable]
class MyClassB
{
public string Name{get; set;}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public MyClassC Property1 {get; private set;}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public BindingList<MyClassC> Test{get; private set;}
}

 [Serializable]
class MyClassC
{
public string Name{get; set;}
}

将[Serializable]放入您的班级中以进行序列化