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;
}
}
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With