C# Designer 序列化问题

标签 c# serialization designer

我在序列化我的对象时遇到了一些问题,并且已将问题缩小到特定情况(参见下面的代码)。我收到以下错误:

错误 1 ​​无效的 Resx 文件。无法加载 .RESX 文件中使用的类型 Serialisation.Harness.Blob、Serialisation、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null。确保已将必要的引用添加到您的项目中。第 129 行,位置 5。...

现在真正奇怪的是,重新启动 Visual Studio 会导致错误消失并且代码可以工作,但是在看似随机数量的构建之后(在此期间所述代码更改)它会再次破裂。

你能看出我做错了什么/遗漏了什么吗?

非常感谢,

梅托

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design; using System.ComponentModel.Design;

namespace Serialisation.Harness
{    

    [Serializable]
    public class Blob
    {
        public Blob()
        {
        }
    }

    [Serializable]
    public class Basic
    {

        private List<Blob> blobs;
        public List<Blob> Blobs
        {
            get { return blobs; }
            set { this.blobs= value; }
        }

        public Basic()
        {
            basics = new List<Blob>();
        }

    }

    public class BasicComponent : Component
    {

        private Basic basic = new Basic();

        private IContainer components = new Container();

        public List<Blob> Blobs
        {
            get { return basic.Blobs; }
            set { basic.Blobs= value; }
        }

        public BasicComponent(IContainer container)
        {
            container.Add(this);
        }

    }

}

最佳答案

首先, Serializable 属性不用于设计器序列化。序列化对象时,设计器不知道如何写入设计器代码时,会序列化到资源文件。这将使用 InstanceDescriptor 将其作为 blob 写入 resx。对于对象类型的默认构造函数(这会丢失您可能还想包含的任何属性值)。这就是 Blobs 正在发生的事情属性,因为设计者不能很好地序列化通用列表(尽管它确实知道如何序列化数组)。

为了保留这些持久对象中的信息,您需要创建一个 TypeConverter InstanceDescriptor 中指定不同的构造函数(实际上需要一些状态来描述属性,例如您的 Blobs 属性)。例如,如果您向 BasicComponent 添加了一个构造函数采用 IEnumerable<Blob> 的类型,然后您可以获得 InstanceDescriptor 给那个构造函数,传入一个 Blob 的数组s(你会在构造函数中围绕它创建一个新的 List<Blob>)。因为设计师知道如何坚持 InstanceDescriptor 代码,并且因为它知道如何将数组持久化到代码中,所以它会将其添加到您的设计器代码中而不是 resx。

您还可以实现 CodeDomSerializer 指定用于描述您的实例的代码,设计者可以使用它来将您的对象保存到设计者代码而不是 resx。

类型转换器

要使用类型转换器方法,您可以这样做:

public class BasicComponentTypeConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        bool canConvert = base.CanConvertTo(context, destinationType);

        if (!canConvert &&
            (destinationType == typeof(InstanceDescriptor))
        {
            canConvert = true;
        }

        return canConvert;
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        object conversion = null;

        if (culture == null)
        {
            culture = CultureInfo.CurrentCulture;
        }

        BasicComponent component = value as BasicComponent;
        if (basicComponent != null)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
               // Note that we convert the blobs to an array as this makes for nicer persisted code output.
               // Without it, we might just get a resource blob which is not human-readable.
               conversion = new InstanceDescriptor(
                   typeof(BasicComponent).GetConstructor(new Type[] { typeof(IEnumerable<Blob>) }),
                   new object[] { basicComponent.Blobs.ToArray() },
                   true);
            }
        }

        if (conversion == null)
        {
            conversion = base.ConvertTo(context, culture, value, destinationType);
        }

        return conversion;
    }
}

请注意,您可能需要为 Blob 编写类型转换器键入以及。要将类型转换器附加到类型,只需声明 TypeConverter 类型转换器将转换的类的属性,即上面示例的 BasicConverter。

关于C# Designer 序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/607417/

相关文章:

c# - 具有多个级别的对象上的 LINQ GroupBy

c# - 如何使用 ONVIF 验证 AXIS 摄像机

c# - MsDeploy - 无法更新应用程序池

c# - InvalidConstraintException 仅在设计器中?

c# - 为什么我的字符串值在插入 SQL 数据库时会累积空格?

c++ - 二进制数据 : write an entire object into a file or separated variables (no object)? 哪个性能更好

java.lang.OutOfMemory错误: Java heap space creating a long list of Myclass

java - 如何在多树中找到最大的公共(public)子树

c# - 控件的自定义设计器

c# - WinForms 设计器在控件更改时删除事件