c# - 如何使用 MongoDB C# 序列化程序序列化值类型?

标签 c# mongodb serialization struct value-type

Mongodb C# 驱动程序不会序列化结构/值类型。如何做到这一点?

最佳答案

您可以使用以下代码创建自定义序列化程序来处理结构:

public class StructBsonSerializer : IBsonSerializer 
{ 
    public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) 
    { 
        var fields = nominalType.GetFields(BindingFlags.Instance | BindingFlags.Public); 
        var propsAll = nominalType.GetProperties(BindingFlags.Instance | BindingFlags.Public); 

        var props = new List<PropertyInfo>(); 
        foreach (var prop in propsAll) 
        { 
            if (prop.CanWrite) 
            { 
                props.Add(prop); 
            } 
        } 

        bsonWriter.WriteStartDocument(); 

        foreach (var field in fields) 
        { 
            bsonWriter.WriteName(field.Name); 
            BsonSerializer.Serialize(bsonWriter, field.FieldType, field.GetValue(value)); 
        } 
        foreach (var prop in props) 
        { 
            bsonWriter.WriteName(prop.Name); 
            BsonSerializer.Serialize(bsonWriter, prop.PropertyType, prop.GetValue(value, null)); 
        } 

        bsonWriter.WriteEndDocument(); 
    } 

    public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) 
    { 
        var obj = Activator.CreateInstance(actualType); 

        bsonReader.ReadStartDocument(); 

        while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) 
        { 
            var name = bsonReader.ReadName(); 

            var field = actualType.GetField(name); 
            if (field != null) 
            { 
                var value = BsonSerializer.Deserialize(bsonReader, field.FieldType); 
                field.SetValue(obj, value); 
            } 

            var prop = actualType.GetProperty(name); 
            if (prop != null) 
            { 
                var value = BsonSerializer.Deserialize(bsonReader, prop.PropertyType); 
                prop.SetValue(obj, value, null); 
            } 
        } 

        bsonReader.ReadEndDocument(); 

        return obj; 
    } 

    public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) 
    { 
        return Deserialize(bsonReader, nominalType, nominalType, options); 
    } 

    public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) 
    { 
        throw new NotImplementedException(); 
    } 

    public void SetDocumentId(object document, object id) 
    { 
        throw new NotImplementedException(); 
    } 
} 

然后,为您的结构注册序列化程序:

BsonSerializer.RegisterSerializer(typeof(MyStruct), new StructBsonSerializer());

关于c# - 如何使用 MongoDB C# 序列化程序序列化值类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26788855/

相关文章:

c# - XNA - 保存 xml 文件时出现 UnauthorizedAccessException

mongodb - mongodump 断言 17369

mongodb - 使用 Mongodb 设置 Django-nonrel

linux - shell脚本中的Mongodb logrotate

django - 如何使用DjangoRestFramework序列化为多个模型

c# - 如何使用列表(而不是数组)反序列化此 xml

android:如何持久存储 Spanned ?

c# - MVC 核心获取 Controller ViewModel 结果

c# - 我有一个条件,使用 < 运算符比较 C# 中的 DateTime,即使不应该返回,它也会返回 false。为什么?

C#设置xml文件位置