The following class (a Windows Forms Control) is a type of list control,
and the ListControlItem don't inherit any Windows Control class.
public class ListControl : Control
{
private List<ListControlItem> items;
public ListControl()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.items = new List<ListControlItem>();
}
public List<ListControlItem> Items
{
get { return items; }
set { items = value; }
}
}
The problem is, at Design Time, Visual Studio tries to serialize the list content to the resource file of the Form, instead of creating the code for instantiating each item and then adding to the control like with ListView and ListViewItem.
Visual Studio design generated code for ListControl:
this.listControl1.Items = ((System.Collections.Generic.List<ListControlItem>)(resources.GetObject("listControl1.Items")));
For ListView:
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("");
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("");
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("");
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4,
listViewItem5,
listViewItem6});
I tried to search the ListView and ListViewItem to solve the problem,
ListView has its "own list class" named ListViewItemCollection that implements the interfaces IList, ICollection, IEnumerable, but List<T> implements the same interfaces.
Does I need to implement a custom serialization for it? Maybe this would just serialize to the resources file. I can't find much documentation as it don't inherit any Windows Forms Control base classes.
UPDATE
Putting the attribute [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] in the List<T> property gives one resource for each List<T> item.
this.listControl1.Items.Add(((ListControlItem)(resources.GetObject("listControl1.Items"))));
this.listControl1.Items.Add(((ListControlItem)(resources.GetObject("listControl1.Items1"))));
this.listControl1.Items.Add(((ListControlItem)(resources.GetObject("listControl1.Items2"))));
this.listControl1.Items.Add(((ListControlItem)(resources.GetObject("listControl1.Items3"))));
this.listControl1.Items.Add(((ListControlItem)(resources.GetObject("listControl1.Items4"))));
this.listControl1.Items.Add(((ListControlItem)(resources.GetObject("listControl1.Items5"))));
Thats like implementing a custom serialization for ListControlItem can help.
ListView. Making the Visual Studio designer generate code for eachListControlIteminstead of putting on the resource file.CodeDomSerializer, as you suspect in the last paragraph.Intas the type for theList<T>. @Octopoid yes, the MSDN documents theCodeDomSerializerlike you said, but only for custom types or if there is a need to serialize the property in a custom way. Otherwise it should serialize using the reflection api, so I don't think its needed. Maybe implementing a class for theList<T>can help.