C#组件集合属性从属性设置器填充时未序列化 [英] C# Component collection property not serialized when filled from property setter
问题描述
我有一个C#组件,它有两个属性,Property1和Property2。 Property1是类型int的简单属性,Property2是一个List,其中T是一个自定义类。 Property2具有DesignerSerializationVisibility.Content属性集。
I have a C# component which has two properties, Property1 and Property2. Property1 is a simple property of type int and Property2 is a List where T is a custom class. Property2 has the DesignerSerializationVisibility.Content attribute set.
当在Designtime设置Property1时,组件应生成设置的自定义类的数量。这工作,但类没有序列化到Designer.cs文件。当我通过Visual Studio的标准集合编辑器添加一个自定义类时,该类被序列化为Designer.cs文件。
When Property1 is set at Designtime the component should generate the number of custom classes that is set. This works but the classes aren't serialized to the Designer.cs file. When I add a custom class through the standard collection editor of Visual Studio the class is serialized to the Designer.cs file.
如何获得Visual Studio也可以序列化生成的类到Designer.cs文件?
How can I get Visual Studio to also serialize the generated classes to the Designer.cs file?
这是我现在的一个小样本:
Here is a small sample of what I have now:
public class TestComponent : Component
{
private int _Count;
public int Count
{
get { return _Count; }
set
{
_Count = value;
Columns.Clear();
for (int i = 0; i < _Count; i++)
{
TestClass tClass = new TestClass();
tClass.Description = "TestClass" + i.ToString();
Columns.Add(tClass);
}
}
}
private List<TestClass> columns = new List<TestClass>();
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<TestClass> Columns
{
get { return columns; }
}
}
[ToolboxItem(false), DesignTimeVisible(false)]
public class TestClass : Component
{
private string _Description;
public string Description
{
get { return _Description; }
set { _Description = value; }
}
}
推荐答案
Columns属性没有setter。序列化程序将忽略此属性。更改为:
The Columns property does not have a setter. The serialiser will ignore this property. Change to this:
private List<TestClass> columns = new List<TestClass>();
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<TestClass> Columns
{
get { return columns; }
set { columns = value; }
}
这篇关于C#组件集合属性从属性设置器填充时未序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!