配合2.x版本的驱动使用
https://stackoverflow.com/questions/26788855/how-do-you-serialize-value-types-with-mongodb-c-sharp-serializer

public class BasicStructSerializer<T> : StructSerializerBase<T> where T: struct
    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value)
        var nominalType = args.NominalType;
        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);
        var bsonWriter = context.Writer;
        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 override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        //boxing is required for SetValue to work
        var obj = (object)(new T());
        var actualType = args.NominalType;
        var bsonReader = context.Reader;
        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 (T)obj;
cm.GetMemberMap(c => c.SomeMemberName).SetSerializer(new BasicStructSerializer<SomeMemberType>());
    在使用Mongodb进行开发的过程中,遇到一些Bson序列化的问题,以及如何将Mongo的Bson格式转化为标准的Json格式。所以在此处记下一些遇到的问题及我认为并不完善的解决方法以供参考和回顾。一、在Bson的ToJson方法中int类型表示为“Number(x)”,可以使用如下方法改变Bson对象序列化时的输出格式:var jsonWriterSettings = new JsonW... 如果您需要DateTimeOffsets在模型中使用,并且希望将其存储为BsonType DateTime,则会出现“ 'DateTime' is not a valid DateTimeOffset representa... 由于MongoDb存储时间按照UTC时间存储的,其官方驱动MongoDB.driver存储时间的时候将本地时间转换为了utc时间,但它有个蛋疼的bug,读取的时候非常蛋疼的是返回的是utc使时间。一个非常直观的体现是时间类型字段存储后和再读入的是不一致的,一个简单的示例如下:table.InsertOne(newMyClass(){Time=DateTime.Now,});foreach (var... 由于MongoDb存储时间按照UTC时间存储的,其官方驱动MongoDB.driver存储时间的时候将本地时间转换为了utc时间,但它有个蛋疼的bug,读取的时候非常蛋疼的是返回的是utc使时间。一个非常直观的体现是时间类型字段存储后和再读入的是不一致的,一个简单的示例如下:  table.InsertOne(new MyClass() Time = DateTime.No... 序列化又称串行化,是.NET运行时环境用来支持用户定义类型的流化的机制。其目的是以某种存储形成使自定义对象持久化,或者将这种对象从一个地方传输到另一个地方。 .NET框架提供了两种种串行化的方式:1、是使用BinaryFormatter进行串行化;2、使用XmlSerializer进行串行化。第一种方式提供了一个简单的二进制数据流以及某些附加的类型信息,而第二种将数据流格式化为XML存储。 生成命令:./ bin / auto -d dbname -path ./models ②修改生成工具代码(支持linux或mac或windows)如果生成出来的结构不是我们所需要的可以修改automatic.go文件 命令:go run automatic.go -d dbname -path ./models 二,生成单个多个表结构体: 命令(支持linux或mac):./ bin / auto -d dbname -t account,user 命令(支持linux或mac或windows):go run automatic.go -d dbname -acc root -pwd 123123 -t account 1. c# 序列化时,如果没有指名_id , 如果class,struct有MemberName为 Id ,_id , 则自动识别为Id . 如果此时,这个"Id"是只读属性,就会引发异常。 2. 同样的,如果指名一个Member,此Member如果也是只读,则同样引发异常 3. KLine 序列化时,遇到问题 monodb 只序列化 ...