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

Posted on 2015-02-01
Last Modified: 2015-03-08
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
Question by:cdebel
  • 5
  • 3
LVL 10

Author Comment

ID: 40583336
Here's some part of my code

My List<ColumnItem> property

        private List<ColumnItem> mColumnsItems = new List<ColumnItem>();
        [Description("Define your grid headers")]
        [Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(CollectionEditor))]
        public List<ColumnItem> Columns
                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.
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 40583344
Do you have the Serializable attribute at the top of your class definition?


public class YourClass

Open in new window

LVL 10

Author Comment

ID: 40583350
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.
LVL 10

Author Comment

ID: 40583353
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.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 40
ID: 40584312
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.
LVL 10

Author Comment

ID: 40591891

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
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 350 total points
ID: 40592036
However, to be able to serialize a class, all of its members should also be serializable.

Reviewing your class, I see that the alignment member is a DataGridViewContentAlignment. This is an Enum, and enums are not serializable by default.

If it was a custom Enum, there are ways to declare it that enable serialization. But since it is a system enum, there is nothing you can do to make it serializable.

You could always implement the ISerializable interface and provide the serialization code yourself, but it is too much work simply for one member, which seems to be your problem at first sight.

In order to enable your class to be serializable without going through the implementation, you need to first give it the Serializable attribute, as pointed to by kaufmed. This tells the computer to do the job for you. But by default, this will fail because of your enum member.

The solution is to store the value of your property in an integer variable that will be serializable, and write a full property that will make the necessary cast in the get and the set. I have not tested it and I am more proficient in VB than in C#, but it would look something like the following:
		using System.ComponentModel;

		[EditorBrowsable(EditorBrowsableState.Never)] public int _Alignment;

		public DataGridViewContentAlignment Alignment
				return (DataGridViewContentAlignment)_Alignment;
				_Alignment = (int) value;

Open in new window

LVL 10

Author Comment

ID: 40592091
Jacques:  Thanks for the pointer!   I'll try the conversion, or even just define my own alignment enum.
LVL 40
ID: 40592148
Your own enum will have the same problem by default.

Once again, it's an attribute that solves the problem (

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now