你怎么序列化值类型的MongoDB的C#序列化? [英] How do you serialize value types with MongoDB C# serializer?

查看:1781
本文介绍了你怎么序列化值类型的MongoDB的C#序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The Mongodb C# Driver will not serialize structs/value types. How can this be done?

推荐答案

您可以创建一个自定义序列使用该代码来处理结构:

You can create a custom serializer to handle structs using this code:

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(); 
    } 
} 



然后,注册该串行器为你的结构:

Then, register that serializer for your struct:

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

这篇关于你怎么序列化值类型的MongoDB的C#序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆