是否可以在Windows Phone 7/8上使用protobuf-net序列化/反序列化类型?
我已经尝试了下面的代码,似乎不支持Constructor跳过(即UseConstructor = false)所以我创建了一个无参数构造函数,但反序列化失败,“尝试访问该方法失败:Wp7Tests.ImmutablePoint.set_X(System.Double) )“
public class ImmutablePoint
{
public double X { get; private set; }
public double Y { get; private set; }
public ImmutablePoint() {}
public ImmutablePoint(double x, double y)
{
X = x;
Y = y;
}
}
public sub Test()
{
ImmutablePoint pt = new ImmutablePoint(1, 2);
var model = TypeModel.Create();
var ptType = model.Add(typeof(ImmutablePoint), false);
ptType.AddField(1, "X");
ptType.AddField(2, "Y");
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
using (var stream1 = new IsolatedStorageFileStream("test.bin", FileMode.Create, store))
{
try
{
model.Serialize(stream1, pt);
}
catch (Exception e)
{
Debugger.Break();
}
}
const double EPSILON = 0.001;
using (var stream1 = new IsolatedStorageFileStream("test.bin", FileMode.Open, store))
{
try
{
ImmutablePoint ptDeSer = (ImmutablePoint) model.Deserialize(stream1,null, typeof(ImmutablePoint));
Debug.Assert(Math.Abs(pt.X - ptDeSer.X) < EPSILON);
Debug.Assert(Math.Abs(pt.Y - ptDeSer.Y) < EPSILON);
}
catch (Exception e)
{
Debugger.Break();
}
}
}
答案 0 :(得分:8)
您可以使用代理;这里我用属性装饰它,但它也可以手动配置:
[ProtoContract]
public class MutablePoint {
[ProtoMember(1)] public double X { get; set; }
[ProtoMember(2)] public double Y { get; set; }
public static implicit operator MutablePoint(ImmutablePoint value) {
return value == null ? null : new MutablePoint {X=value.X, Y=value.Y};
}
public static implicit operator ImmutablePoint(MutablePoint value) {
return value == null ? null : new ImmutablePoint(value.X, value.Y);
}
}
然后告诉模型在看到ImmutablePoint
时使用它:
var model = TypeModel.Create();
model.Add(typeof(MutablePoint), true);
model.Add(typeof(ImmutablePoint), false).SetSurrogate(typeof(MutablePoint));
序列化器将根据需要使用运算符在它们之间切换。序列化程序将使用implicit
或explicit
自定义转换运算符。
编辑: 像这样反序列化
ImmutablePoint ptDeSer = (ImmutablePoint)model.Deserialize(stream1, null, typeof(ImmutablePoint));
答案 1 :(得分:2)
这在Windows Phone上无法实现。反序列化程序无法访问setter并使用反射访问私有成员会导致Windows Phone上出现MemberAccessException。我能想到让这样的东西工作的唯一方法是创建一个序列化的包装器,然后可以调用你的ImmutablePoint的构造函数。这有点乱,因为包装类需要X和Y的公共读写属性。
您还可以查看使用InternalsVisibleTo属性来允许序列化程序访问属性(假设您将setter从private更改为internal)。虽然还是凌乱......