IBsonSerializer
またはSerializerBase<>
を作成し、BsonSerializerAttribute
を使用してシリアル化するプロパティに接続する必要があります。以下のような何か:
public class BsonStringNumericSerializer : SerializerBase<double>
{
public override double Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var type = context.Reader.GetCurrentBsonType();
if (type == BsonType.String)
{
var s = context.Reader.ReadString();
if (s.Equals("N/A", StringComparison.InvariantCultureIgnoreCase))
{
return 0.0;
}
else
{
return double.Parse(s);
}
}
else if (type == BsonType.Double)
{
return context.Reader.ReadDouble();
}
// Add any other types you need to handle
else
{
return 0.0;
}
}
}
public class YourClass
{
[BsonSerializer(typeof(BsonStringNumericSerializer))]
public double YourDouble { get; set; }
}
あなたがIBsonSerializationProvider
を作成し、BsonSerializer.RegisterSerializationProvider
を使用して登録することができ、属性を使用しない場合。人々はコメントせずにdownvotedなぜMongoDBのC#BSONの直列化の
完全なドキュメントはhere
を見つけることができますか?もしそうなら、どのように私の質問に間違っているのか分かりますか? – kvuong