I have a custom property in my Component object that is an object that contains another object that is a generic collection of another object, as well as a single object. It looks something like this:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Editor(typeof(ControlsEditor), typeof(UITypeEditor))]
public DialogControls Controls { get; set; } = new DialogControls();
[Serializable]
public class DialogControls :
ControlGroup<Control> Controls { get; set; } = new ControlGroup<Control>();
Control SelectedItem { get; set; } = null;
}
[Serializable]
public class ControlGroup<T> : IList<T>, ICollection where T:Control{
List<T> _controls;
}
[Serializable]
public abstract class Control {
}
All of the classes that inherit from Control are also serializable.
When I enter a list of controls through the custom editor, everything works fine, and the property is saved in the resources file as is expected. However, when I load the designer for the form that contains the component, I get the error
Object of type 'System.Collections.Generic.List`1[CommonItemDialog.CommonFileDialog+Control]' cannot be converted to type 'System.Collections.Generic.List`1[CommonItemDialog.CommonFileDialog+Control]'. Line 264, position 5. cannot be parsed.
If I close Visual Studio, and then reopen the solution, the form comes up fine, and all my property settings are intact. If I modify another module, however (even a comment), that error returns in the designer, and I have to close VS and reopen it to get rid of it.
[Edit]
I have found that this only occurs when I have a complex Control object added - following code demonstrates:
public class ControlItem : Control {
public string Label { get; set; }
}
public interface IControlContainer<T> where T:Control {
ControlGroup<ControlItem> Controls { get; set; }
}
public class ComboBox : IControlContainer<ControlItem>, Control {
public ControlGroup<ControlItem> Controls { get; set; } = new ControlGroup<ControlItem>();
}
Its when I add a control like the ComboBox that my problems start.
Just a FYI - these controls become the custom controls in a IFileDialogCustomize implementation. When I run the test form, with these controls added, it works perfectly, and the ComboBox DOES appear correctly on the CommonItemDialog.
[Edit Jan 18]
The problem seems to be directly related to deserialising the elements of the generic lists, when the object in the list is a derivative of the generic type of the list (i.e. The list of Controls hits a problem deserialising a ComboBox object).
What I am trying is threefold:
- Removing the generic parameter from the outer collection classes, and declaring different types for each of the collections.
- Implementing ISerializable on all of the Control objects. This also involves making sure there is a default constructor for each type.
- Serialising and deserialising the generic lists as arrays rather than as List<T>s
Hopefully one of these changes will solve the problem - I'll post it as an answer if they do.
What I have tried:
I've tried resetting the DesignerSerializationVisibility to Content, and I get a similar error that I resolves itself in the same way (i.e. close VS and reopen solution).