c# - 无法创建组件 .. 类型未标记为可序列化

标签 c# winforms serialization user-controls

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

一切都进行得很顺利,直到我发现我可以玩 List<>属性窗口中的集合属性。在尝试更改集合并运行项目后,我开始遇到错误,并尽我所能将一切恢复到正常工作时的状态。

现在,当我尝试将控件实例放置到窗体上时,出现以下错误。

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#。

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]但随后开始出现其他错误。我不记得确切的细节,但这并不重要,因为我不想序列化此数据。

我尝试了一个新的表单并得到了同样的错误。所以我创建了一个全新的项目并将我的类代码复制到一个新的用户控件中,但错误仍然存​​在。任何人都可以提出可能导致此错误的原因吗?我不想连载此合集。

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

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

最佳答案

毫无疑问,一些额外的属性不能被设计器序列化以显示在设计器表面上。

尝试将这些属性添加到用户控件的不可序列化属性中:

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

关于c# - 无法创建组件 .. 类型未标记为可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4973506/

相关文章:

C# 字典 - 如何解决项目数量限制?

vb.net - itextsharp : how do i position a table?

c# - 使用 Windows 窗体将文件上传到 ASP.NET Web 服务 (ASMX)

c# - 在 C# winform 中将选定的项目从一个列表框移动到另一个列表框

c# - 如何使用 json.net 构建用于序列化的对象层次结构?

c# - 如何在 .NET 中将 POST 请求内容保存为文件

javascript - 隐藏 Popup Div 无法正常工作

javascript - Python 到 JavaScript 对象图(反)序列化

c# - NHibernate/事件记录 : How to set foreign key without getting entire object?

java - 通过序列化和反序列化在 mqtt 中发送和接收对象会导致 classnotfound 异常