Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2015-02-01
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 10

Author Comment

by:Christian de Bellefeuille
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 600 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

by:Christian de Bellefeuille
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.
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

LVL 10

Author Comment

by:Christian de Bellefeuille
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.
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

by:Christian de Bellefeuille
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 1400 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

by:Christian de Bellefeuille
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 (https://msdn.microsoft.com/en-us/library/vstudio/system.runtime.serialization.enummemberattribute%28v=vs.100%29.aspx).

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Six Sigma Control Plans
Starting up a Project
Suggested Courses

618 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