Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to Create Component .. Type is not Marked as Serializable

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;
    }
}
like image 984
Jonathan Wood Avatar asked Feb 11 '11 20:02

Jonathan Wood


1 Answers

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; } 
like image 96
Krishna Valiveti Avatar answered Oct 01 '22 07:10

Krishna Valiveti