簡體   English   中英

C#Designer序列化問題

[英]C# Designer Serialisation problems

我在序列化我的對象時遇到了一些問題,並將問題縮小到特定情況(參見下面的代碼)。 我收到以下錯誤:

錯誤1無效的Resx文件。 無法加載類型Serialisation.Harness.Blob,Serialization,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null,在.RESX文件中使用。 確保已將必要的引用添加到項目中。 第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屬性不用於設計器序列化。 序列化對象時,設計器在不知道如何將其寫入設計器代碼時將序列化為資源文件。 這將它作為blob寫入resx,使用InstanceDescriptor作為對象類型的默認構造函數(這會丟失您可能還想包含的任何屬性值)。 這就是Blobs屬性正在發生的事情,因為設計器沒有很好地序列化通用列表(它確實知道如何序列化數組)。

為了在這些持久化對象中保留信息,您需要創建一個TypeConverter ,它在InstanceDescriptor中指定一個不同的構造函數(實際上需要一些狀態來描述屬性,例如Blobs屬性)。 例如,如果你將一個構造函數添加到接受IEnumerable<Blob> BasicComponent類型,那么你可以獲得一個InstanceDescriptor到該構造函數,傳入一個Blob數組(你可以創建一個新的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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM