使用 Mongo 驱动程序 2 升级 IBsonSerializer
Upgrading IBsonSerializer with Mongo driver 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)
{
在 .NET 驱动程序的 2.0 版本中,我们需要将更多信息传递给序列化程序。我们没有向方法中添加更多参数,而是将参数打包为两个新参数。 context 参数包含在整个序列化操作中应该保持不变的值,args 参数包含在序列化复杂类型时在每个级别更改的值。
移植到新设计应该相对容易:
- reader参数现在在context.Reader
- nominalType 参数现在在 args.NominalType
- actualType 参数已经消失
关于实际类型,现在每个序列化程序都有责任确定实际类型(使用它想要的任何约定),并在实际类型与标称类型不同时查找并委托给实际序列化程序。如果您正在序列化的 class 不是多态的,那么标称类型和实际类型无论如何始终相同。
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)
{
在 .NET 驱动程序的 2.0 版本中,我们需要将更多信息传递给序列化程序。我们没有向方法中添加更多参数,而是将参数打包为两个新参数。 context 参数包含在整个序列化操作中应该保持不变的值,args 参数包含在序列化复杂类型时在每个级别更改的值。
移植到新设计应该相对容易:
- reader参数现在在context.Reader
- nominalType 参数现在在 args.NominalType
- actualType 参数已经消失
关于实际类型,现在每个序列化程序都有责任确定实际类型(使用它想要的任何约定),并在实际类型与标称类型不同时查找并委托给实际序列化程序。如果您正在序列化的 class 不是多态的,那么标称类型和实际类型无论如何始终相同。