创建组件失败..类型未标记为可序列化

Failed to Create Component .. Type is not Marked as Serializable(创建组件失败..类型未标记为可序列化)

本文介绍了创建组件失败..类型未标记为可序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual C# 2008 Express Edition 创建一个 WinForms 用户控件.

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

一切都很顺利,直到我发现我可以从属性窗口中使用 List<> 集合属性.在尝试更改集合并运行项目后,我开始遇到错误并尽我所能将所有内容恢复到工作时的位置.

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...'

消除此错误后,我开始收到以下错误,通常反复出现,直到我使用任务管理器关闭 Visual C#.

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.'

我尝试将我的 ColorData 类标记为 [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.

这是有问题的集合(这些是我的用户控件中的行——ColorData 类嵌套在我的用户控件中).

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; } 

这篇关于创建组件失败..类型未标记为可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:创建组件失败..类型未标记为可序列化

基础教程推荐