无法转换为类型'MongoDB.Bson.Serialization.IBsonSerializer`1 [System.String]'

时间:2019-02-18 10:48:27

标签: c# mongodb mongodb-.net-driver

我将mongoDB驱动程序用于C#,由于序列化/反序列化,在尝试执行一些查询时遇到问题。据我了解,我无法将Int32字段序列化为字符串。

System.ArgumentException:类型“ US.EndPointTests.AdditionalFunctionalities.TestingObjectTypeSerializer”的对象不能转换为类型“ MongoDB.Bson.Serialization.IBsonSerializer`1 [System.String]”。

这是我的收藏集

private class contactos
{
    public ObjectId _id { get; set; }
    public string nombre { get; set; }
    public string email { get; set; }

    [BsonSerializer(typeof(TestingObjectTypeSerializer))]
    public string telefono { get; set; }
    [BsonSerializer(typeof(TestingObjectTypeSerializer))]
    public string telefono2 { get; set; }

    public string direccion { get; set; }
    public string direccionNormalizada { get; set; }
    public string[] maestroTipoContacto { get; set; }
    public string maestroCanalEntrada { get; set; }

    [BsonSerializer(typeof(TestingObjectTypeSerializer))]
    public string nota { get; set; }
    public string cifnif { get; set; }

    [BsonElement("particion")]
    [BsonSerializer(typeof(TestingObjectTypeSerializer))]
    public string particion { get; set; }

    public DateTime fechaCreacion { get; set; }
    public DateTime fechaModificacion { get; set; }
    public string nombreNormalizado { get; set; }

    public string[] tipoMaestroContacto { get; set; }
}

我正尝试使用以下方法从数据库中删除一个文档:

            returnDictionary = GetContact();                

            string particion = (returnDictionary["particion"].ToString());

            var builder = Builders<contactos>.Filter;

            var filter = builder.Eq("particion", Int32.Parse(particion));

            var DeleteOne = collection.DeleteOne(filter);

但是我收到此错误:

System.ArgumentException:“ US.EndPointTests.AdditionalFunctionalities.TestingObjectTypeSerializer”类型的对象无法转换为“ MongoDB.Bson.Serialization.IBsonSerializer`1 [System.String]”类型。

这是我的自定义序列化程序(我在这里尝试了一些不同的方法,但是没有一个起作用):

public class TestingObjectTypeSerializer : IBsonSerializer
{
    public Type ValueType { get; } = typeof(string);

    public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
    {
        var bsonType = context.Reader.CurrentBsonType;
        switch (bsonType)
        {
            case BsonType.Null:
                context.Reader.ReadNull();
                return null;
            case BsonType.String:
                return context.Reader.ReadString();
            case BsonType.Int32:
                return context.Reader.ReadInt32().ToString(CultureInfo.InvariantCulture);
            default:
                var message = string.Format("Cannot deserialize BsonString or BsonInt32 from BsonType {0}.", bsonType);
                throw new BsonSerializationException(message);
        }
    }

    public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
    {
        if (value != null)
        {
            context.Writer.WriteString(value.ToString());
        }
        else
        {
            context.Writer.WriteNull();
        }
    }

    private static object GetNumberValue(BsonDeserializationContext context)
    {
        var value = context.Reader.ReadInt32();

        switch (value)
        {
            case 1:
                return "one";
            case 2:
                return "two";
            case 3:
                return "three";
            default:
                return "BadType";
        }
    }
}

}

收集模式:

Collection Schema

谢谢!

0 个答案:

没有答案