2

Mongo 驱动程序的旧实现导致了这种代码:

public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType)
{
    if (nominalType == typeof(T))
    {
        if (typeof(V) == typeof(string))
            return _deSerializeFunc(bsonReader.ReadString());
        else if (typeof(V) == typeof(int))
            return _deSerializeFunc(bsonReader.ReadInt32());
        else if (typeof(V) == typeof(double))
            return _deSerializeFunc(bsonReader.ReadDouble());
        else if (typeof(V) == typeof(decimal))
            return _deSerializeFunc((decimal)bsonReader.ReadDouble());
    }
    return null;
}

新界面完全不同。如何使用这个新接口开始实现以前的代码?

public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
4

1 回答 1

2

在 .NET 驱动程序的 2.0 版本中,我们需要将更多信息传递给序列化程序。我们没有向方法添加更多参数,而是将参数打包成两个新参数。context 参数保存在整个序列化操作中应该保持不变的值,而 args 参数保存在序列化复杂类型时在每个级别更改的值。

移植到新设计应该相对容易:

  1. reader 参数现在在 context.Reader
  2. nominalType 参数现在位于 args.NominalType
  3. actualType 参数已消失

关于actualType,现在每个序列化器有责任确定实际类型(使用它想要的任何约定),并在实际类型与名义类型不同时查找并委托给实际序列化器。如果您要序列化的类不是多态的,那么名义类型和实际类型总是相同的。

于 2015-05-26T21:34:11.480 回答