4

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

错误 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);
        }

    }

}
4

1 回答 1

6

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

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

您还可以实现 aCodeDomSerializer来指定用于描述您的实例的代码,设计器可以使用它来将您的对象保存到设计器代码而不是 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。

于 2009-03-03T18:24:19.983 回答