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参数保存在序列化复杂类型时在每个级别上更改的值。
移植到新设计应该相对容易:
关于actualType,现在,每个序列化程序都有责任确定实际类型(使用所需的任何约定),并在实际类型不同于标称类型时查找并委托给实际的序列化程序。如果您要序列化的类不是多态的,则标称类型和实际类型始终始终相同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句