Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
[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;
    // Implement interfaces here
}

[Serializable]
public abstract class Control {
   //... a couple of protected members and abstract methods
}

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; }
   // All virtual methods implemented
}

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:
  1. Removing the generic parameter from the outer collection classes, and declaring different types for each of the collections.
  2. Implementing ISerializable on all of the Control objects. This also involves making sure there is a default constructor for each type.
  3. 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).
Posted
Updated 17-Jan-17 20:31pm
v8
Comments
Ralf Meier 14-Jan-17 6:31am    
I don't understand your problem exactly ... but you could try the following :
- let the Property "Controls" only have a Getter (make a readonly Property from it).
- change the Attribute "DesignerSerializationVisibility" to "DesignerSerializationVisibility.Content"

If this not matches to your problem - please explain your class-structure, you want to have ...
Midi_Mick 14-Jan-17 6:40am    
1. I can't let the property have only a getter - it needs to be set by the UITypeEditor I have created to set the whole structure up.
2. I did try setting the Attribute to DesignerSerializationVisibility.Content, but I got a similar error with the same sort of symptoms. The error then occurred when setting a member of the Controls list, and it was complaining about the generic Type constraint (can't remember exactly what it said). However, resetting VS once again temporarily cleared the error, and the test program still worked fine.
Ralf Meier 14-Jan-17 7:54am    
OK ... then "please explain your class-structure, you want to have"

And ... for what exactly do you need a UITypeEditor a this special place ? Normally class-object should work with the ExpandableObjectConverter.
Midi_Mick 14-Jan-17 8:03am    
They are there in the code. All the classes defined are member classes of the CommonFileDialog Component, which contains the "DialogControls Controls" property. Each Control class inherits a uint ID property, and State property (which is an enum value), and a virtual AddToDialog() method, so the control knows how to add itself to the component. As mentioned, the problem occurs when I include a Control that contains a ControlGroup of other controls - There are numerous types that do this - The ComboBox above is an example.
Ralf Meier 18-Jan-17 6:16am    
I refer to your last changes of your question.
When I startet with my comment I thought that this is a part with which I'm very familiar. In fact I can't follow you and the thing you try to achieve. Perhaps you give some more Info ...

With List-Properties I agree with your observation. If there is a List of a specified type (List (of myClassTtype)) it is no problem for the designer to serialize it - but I never tried it with a List of an unspecified type ...

1 solution

It sounds like something I met a few years back, where VS decides that a class isn't the same as itself. Have a look here: Getting round Visual Studio when it decides a class isn't the same as itself...[^] and clean out the intermediate files - that may fix the problem.
 
Share this answer
 
Comments
Midi_Mick 14-Jan-17 5:43am    
Bugger - Didn't work (still same error).
However, I have narrowed down the occurrence of it to particular circumstances. I will edit the question to show the additional info.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900