更改自动实现的属性,以正常和反序列化与BinaryFormatter的 [英] Change auto implemented properties to normal and deserialization with BinaryFormatter
问题描述
我有一个对象的属性来实现像
I have an object with a property implemented like
public String Bla {get;set;}
变更实施类似后
After changing the implementation to something like
private String _bla;
public String Bla
{
get { return _bla; }
set { _bla = value; }
}
在deserialzation,这个属性出现空白。
on deserialzation, this Property comes up empty.
我有很多从旧的实现序列化的数据,并希望加载这些新的实施
i have lots of serialized data from the old implementation, and would like to load them with the new implementation
有没有什么办法,来改变implentation是对旧的二进制文件兼容?
is there any way, to change the implentation to be compatible with older binary files?
编辑:
有些人可能会遇到同样的问题,所以这是我的hackish的解决方案:
Some people might run into the same problem, so here's my hackish solution:
自动生成的领域有命名约定是不合法的C#code:
the autogenerated fields have a naming convention that is non-valid c# code:
[CompilerGenerated]
private string <MyField>k__BackingField;
[CompilerGenerated]
public void set_MyField(string value)
{
this.<MyField>k__BackingField = value;
}
[CompilerGenerated]
public string get_MyField()
{
return this.<MyField>k__BackingField;
}
我的快速和肮脏的修复程序是创建一个名为 xMyFieldxK__BackingField
在源代码中的私人支持字段,
the quick and dirty fix for me was to create a private backing field called xMyFieldxK__BackingField
in the source,
和替换所有出现的&LT修补系列化binarydata; MyField的&GT;
与 xMyFieldx
deserialisation之前
and patching the serialized binarydata by replacing all occurences of <MyField>
with xMyFieldx
before deserialisation
推荐答案
尝试实施 ISerializable的
[SecurityCritical]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new ArgumentNullException("info");
info.AddValue("name of compiler generated field", _bla);
}
这篇关于更改自动实现的属性,以正常和反序列化与BinaryFormatter的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!