use*_*328 4 c# serialization designer
我在序列化我的对象时遇到了一些问题,并将问题缩小到特定情况(参见下面的代码).我收到以下错误:
错误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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先,该Serializable属性不用于设计器序列化.序列化对象时,设计器在不知道如何将其写入设计器代码时将序列化为资源文件.这将它作为blob使用InstanceDescriptorfor对象类型的默认构造函数写入resx (这会丢失您可能还想包含的任何属性值).这就是Blobs属性正在发生的事情,因为设计师没有很好地序列化通用列表(它确实知道如何序列化数组).
为了在这些持久化对象中保留信息,您需要创建一个TypeConverter指定不同构造函数的InstanceDescriptor(一个实际上需要一些状态来描述属性,例如您的Blobs属性).例如,如果你为你的BasicComponent类型添加了一个构造函数IEnumerable<Blob>,那么你就可以获得一个InstanceDescriptor构造函数,传入一个Blobs 数组(你List<Blob>可以在构造函数中创建一个新的).因为设计人员知道如何持久化InstanceDescriptor代码,并且因为它知道如何将数组持久化为代码,所以它会将其添加到设计器代码而不是resx中.
您还可以实现a 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;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,您可能还需要为该Blob类型编写类型转换器.要将类型转换器附加到类型,只需声明TypeConverter类型转换器将转换的类的属性,即上面示例的BasicConverter.