是否有可能在Windows Phone 7/8上使用protobuf-net序列化/反序列化不可变类型?

本文关键字:protobuf-net 序列化 反序列化 类型 不可变 有可能 Windows Phone 是否 | 更新日期: 2023-09-27 18:09:46

是否可以在Windows Phone 7/8上使用protobuf-net序列化/反序列化类型?
我试过下面的代码,似乎构造函数跳过是不支持的(即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();
            }
        }
}

是否有可能在Windows Phone 7/8上使用protobuf-net序列化/反序列化不可变类型?

您可以使用替代符;这里我用属性装饰它,但它也可以手动配置:

[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));

序列化器将根据需要使用操作符在它们之间切换。序列化器将使用implicitexplicit自定义转换操作符。

编辑:像这样反序列化

ImmutablePoint ptDeSer = (ImmutablePoint)model.Deserialize(stream1, null, typeof(ImmutablePoint));

这在Windows Phone上是不可能的。反序列化器不能访问setter,并且使用反射访问私有成员会在Windows Phone上导致MemberAccessException。我能想到的唯一方法是创建一个序列化的包装器,然后调用ImmutablePoint的构造函数。不过这有点乱,因为包装器类需要X和y的公共读写属性。

您还可以考虑使用InternalsVisibleTo属性来允许序列化器访问属性(假设您将setter从private更改为internal)。