Failed to Create Component .. Type is not Marked as Serializable
Asked Answered
B

5

17

I'm creating a WinForms user control using Visual C# 2008 Express Edition.

Everything was going on nicely until I found I could play with a List<> collection property from the properties window. After trying to change the collection and running the project, I started getting errors and did my best to get everything back to where it was when it was working.

Now when I try and place an instance of the control onto a form, I get the following error.

Failed to create component 'ColorPicker'.  The error message follows:
'System.Runtime.Serialization.SerializationException: Type 'WindowsFormsApplication1.ColorPicker+ColorData' in Assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)
   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)
  at System.Runtime.Serialization.Formatt...'

After dismissing this error, I start getting the following error, usually repeatedly until I use Task Manager to shut Visual C# down.

Code generation for property 'PaletteColors' failed.  Error was: 'Type 'WindowsFormsApplication1.ColorPicker+ColorData' in Assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.'

I tried flagging my ColorData class as [Serializable] but then started getting other errors. I don't recall the exact details but it doesn't really matter because I don't want this data serialized.

I tried a new form and got the same error. So I created a completely new project and copied my class' code over to a new user control, and the error still occurs. Can anyone suggest what might be causing this error? I do not want this collection serialized.

Here's the collection in question (these are lines in my user control--the ColorData class is nested in my user control).

public List<ColorData> PaletteColors { get; set; }

public class ColorData
{
    public string Text { get; set; }
    public Color Color { get; set; }

    public ColorData()
    {
        Text = String.Empty;
        Color = Color.White;
    }

    public ColorData(string text, Color color)
    {
        Text = text;
        Color = color;
    }

    public ColorData(KnownColor color)
    {
        Text = Enum.GetName(typeof(KnownColor), color);
        Color = Color.FromKnownColor(color);
    }

    public override string ToString()
    {
        return Text;
    }
}
Beatabeaten answered 11/2, 2011 at 20:6 Comment(0)
S
25

No doubt it is some extra attributes are not serializable by designer to show it on the designer surface.

Try adding these attributes to non-serializable properties of the user control:

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public List<ColorData> PaletteColors { get; set; } 
Salchunas answered 11/2, 2011 at 20:44 Comment(2)
I can't thank you enough. This was really driving me nuts! Making the requested change fixed the problem. I'm obviously in a little over my head with regards to user control attributes. I'll research these settings, but perhaps you'd share your thoughts on why this helps. Thanks.Beatabeaten
Also, if the property doesn't need to be public, don't make it so. I had a property that was lazily marked public when it could have been protected. Just setting it back to protected fixed the issue. I expect that actually initializing the property on the control in myform.designer.cs will fix it. I commonly get this issue when I have a control on a form, and I sub it out for another control that inherits from it. If you just added the second control, then VS adds the necessary initializersFurriery
E
3

You can use this :

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public List<ColorData> PaletteColors { get; set; }

But you also can add this, if your form is localised :

 [System.ComponentModel.Localizable(false)]
Effortless answered 6/4, 2011 at 20:18 Comment(0)
B
2

You can try this if you want to be browsable in design mode

[System.Xml.Serialization.XmlArray]
    public List<Pen> PenList
    {
        get { return penList; }
        set { penList = value; }
    }
Bobcat answered 6/11, 2012 at 7:2 Comment(0)
F
1

I added a comment to the accepted answer, but feel this probably deserves its own answer. While the accepted answer definitely works, it doesn't really address the root cause.

If you have a control (wincontrol) with a public property PropertyA, and you add it to a form (myForm), then the designer will add all the necessary initialization for properties into myForm.Designer.cs. Something like;

Wincontrol1.PropertyA = new List<widget>();

It is not uncommon to need to modify a control slightly, lets say we have a new control MyWinControl

public partial class MyWinControl : WinControl
{
    public List<wodget> PropertyDer1;
    protected List<wodget> PropertyDer2;
}

If you sub this new control for the old control in myForm.Designer.cs, then you may well encounter this issue. The reason is that PropertyDer1 has no initialization in the winforms designer. PropertyDer2 won't cause any issues because it is protected. Similarly if you had a custom component and you add a new public property after the component has been added to a form.

If however, you deleted the instance of WinControl on the form, and dragged an instance of the MyWinControl onto the form instead, the proper initialization would occur and you would not see the error. The designer will have created the new control like this

Wincontrol1.PropertyA = new List<widget>();
Wincontrol1.PropertyDer1= new List<wodget>();

There are two easy solutions that do not require hiding the property from the designer. 1. If the property doesn't need to be public, give it the right modifier 2. If the property does need to be public, then just edit the code in the myForm.Designer.cs as in the code above to add the missing initializer

Furriery answered 14/2, 2020 at 6:24 Comment(0)
M
0

I'm not sure about this, but have you tried to use the [Serializable] attribute?

[Serializable]
public class ColorData
{
.....
}
Mandrake answered 11/2, 2011 at 20:22 Comment(2)
I explained in my question that I did.Beatabeaten
This is the opposite of that the OP was looking for. That it's not serializable isn't the surprise, that Visual studio thinks it should be serializable is!Verrazano

© 2022 - 2024 — McMap. All rights reserved.