未能创建组件.. 类型未标记为可序列化 [英] Failed to Create Component .. Type is not Marked as Serializable

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

问题描述

我正在使用 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; } 

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆