Link to home
Start Free TrialLog in
Avatar of Christian de Bellefeuille
Christian de BellefeuilleFlag for Canada

asked on

Code generation for property failed : Type ... is not marked as serializable

I'm designing a custom control.  It has several properties and one of them is a List<ColumnItem>.  ColumnItem is a class that i've made.  This list is using an CollectionEditor.  I change some stuff, compile and weird thing happen.  So i try to debug it, i check at the form containing my custom control and i get this error: "Code generation for property 'ColumnItems' failed.  Error was: 'Type 'System.Windows.Forms.UserControl' in Assembly 'System.Windows.Forms ...    is not marked as serializable".  If i remove my usercontrol from my test form, then put a new one, i get the same error.   If i create a new form, voilà!  It work.

Why am i getting this serialization havoc?
How can i avoid it?
Does it mean that once a custom control is added to my forms, i can't modify my control anymore?  That wouldn't make much sense

Thanks for your help
Avatar of Christian de Bellefeuille
Christian de Bellefeuille
Flag of Canada image

ASKER

Here's some part of my code

My List<ColumnItem> property

        private List<ColumnItem> mColumnsItems = new List<ColumnItem>();
        [Category("_MyControlProperties")]
        [Description("Define your grid headers")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(CollectionEditor))]
        public List<ColumnItem> Columns
        {
            get
            {
                return mColumnsItems;
            }
        }

Open in new window


My ColumnItem Class

    public class ColumnItem
    {
        public float Width { get; set; }
        public string DBColumnName { get; set; }
        public string GridColumnName { get; set; }

        public DataGridViewContentAlignment Alignment { get; set; }
        public eColumnType ColumnType { get; set; }
        public bool Visible { get; set; }
        public int FilterPlaceholder { get; set; }

        public ColumnItem()
        {
            Width = 1;
            ColumnType = eColumnType.eNone;
            Alignment = DataGridViewContentAlignment.MiddleCenter;
            DBColumnName = "";
            GridColumnName = "";
            Visible = true;
            FilterPlaceholder = -1;
        }


        public override string ToString()
        {
            return DBColumnName;
        } 

Open in new window



ColumnItem have a constructor to initialize some values, and it has a ToString so the Editor can show the name i want in the list, otherwise its difficult to manage when all of them have similar names.
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Another detail...

I've a solution with 3 project in it:
My "ComponentSuite" project
A project with test forms
Another project with test forms

Can it be linked to that?  Because it's in the same solution it get mixed up someway?  Because i've just closed Visual Studio 2010 because i was not able to add this control again without getting this error.  Then when i restarted VS, and added this control, the problem went away.

Next time it occur, i won't delete the object and just close VS and reopen it to see if it can get back on track, because i really can't delete every control of this app in every form and re-create them each time this error occur.
kaufmed: [Serializable] shouldn't be used in this case.  I can't really explain if myself and i don't know if it's allowed to post link from other forums but here's the explanation i've found about this:

What you want is a design time support with CodeDom serialization. You do not need SerializableAttribute or ISerializable, those are for binary serialization. Since you want to serialize the collection, you must tell the designer to serialize it as such. That is done with the DesignerSerializationVisibiliby attribute - value of Content tells the designer to serialize property contents rather than property itself. Contents of the property should of course be CodeDom serializable, which simple classes with simple properties are by default.
What do you intend to achieve with         [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

This is an instruction on how to generate the serialization code. This is probably why kaufmed suggested to make it Serializable. If you class is not Serializable, then this line is useless and this can create strange behavior.

Also, we do not see the whole code, but where and how do you use your custom control? Do you use is as a member in one of your own classes? When you drop it on a Form, is it on the Form itself or on a container Control?

System.Windows.Forms.UserControl' in Assembly 'System.Windows.Forms ...    is not marked as serializable usually implies that you are referencing your control in an object that is itself serializable. By default, in order to serialize an object, all its members must also be serializable.

Finally, when you show us something that you have found elsewhere, tell us where you found it. You might have found it in an article that has nothing to do with your situation.
Jacques:

I've not been able to reproduce this problem yet.  I'll do some more tests (add methods and properties) to reproduce the problem and i'll test what kaufmed suggedted.  I think i've tested it, but i'm not sure if it was on my ColumnItem class.  I think it was on my user control itself.

To asnwer your questions...
How do i use my custom control? I drop it on the form itself, for the moment.

System.Windows.Forms.UserControl' in Assembly 'System.Windows.Forms .  Maybe it happen just because ColumnItem is not serialisable even if i thought it would be.  As i said, i thought it would be serialized because of  [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]  

And finally, the link where i've found that article: S.O. Article
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jacques:  Thanks for the pointer!   I'll try the conversion, or even just define my own alignment enum.
Your own enum will have the same problem by default.

Once again, it's an attribute that solves the problem (https://msdn.microsoft.com/en-us/library/vstudio/system.runtime.serialization.enummemberattribute%28v=vs.100%29.aspx).