【问题标题】:Data of this type has inbuilt behaviour, and cannot be added to a model in this way: System.Int64这种类型的数据具有内置行为,不能以这种方式添加到模型中:System.Int64
【发布时间】:2012-06-19 01:25:58
【问题描述】:

这个错误:

Data of this type has inbuilt behaviour, and cannot be added to a model in this way: System.Int64

仅在以下行为 protobuf-net 在序列化具有 TimeStamp 类型的对象时使用的 long 代理类型时发生。

RuntimeTypeModel.Default.Add(typeof(TimeStamp),false).SetSurrogate(typeof(long));

TimeStamp 类定义了以下显式运算符:

public static explicit operator TimeStamp( long timeStamp)
{
    return new TimeStamp(timeStamp);
}

public static explicit operator long( TimeStamp TimeStamp)
{
    return TimeStamp.Internal;
}

那么为什么它不接受 long 作为 TimeStamp 的代理类型呢? 在开始尝试protobuf-net之前,已经手写序列化了 在序列化/反序列化时使用 long 作为 TimeStamp 的代理项。

【问题讨论】:

    标签: c# serialization protobuf-net


    【解决方案1】:

    目前,代理行为期望映射到“消息”类型(在 protobuf 术语中)。能够将其映射到诸如long 之类的原语似乎是一个不寻常的情况。我很可能会调整代码以使其正常工作,但是:简单地说 - 这不是此功能的预期用例,因此它目前不支持(messages和原语在存储内容方面存在显着差异)。

    顺便说一句,根据你有多少,你可以这样做:

    public TimeStamp Foo {get;set;}
    
    [ProtoMember(n)]
    private long FooValue {
        get { return (long)Foo; }
        set { Foo = (TimeStamp)value; }
    }
    

    【讨论】:

    • 我今天遇到了这个确切的问题,并想为启用此功能投 +1 票。我们在数据库层使用强类型标识符,如果我们不必为每个包含标识符类型属性的 protobuf 可序列化类添加 shim 属性,那就太好了。这些标识符类型中的每一个都公开了与 int 的显式转换。
    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 2011-08-13
    • 2021-07-19
    • 2011-07-24
    相关资源
    最近更新 更多