From 91b822c34fd50bac6bbede0ee55a9da8354b989d Mon Sep 17 00:00:00 2001 From: tivial19 <77505958+tivial19@users.noreply.github.com> Date: Fri, 15 Jan 2021 21:14:06 +0300 Subject: [PATCH] Update BsonMapper.Serialize.cs This changes will help to Serialize interfaces correct. Now it is working like this: public interface IItemData { bool Auto { get; set; } string Name { get; set; } int Value { get; set; } } public class ItemData : IItemData { public bool Auto { get; set; } public string Name { get; set; } public int Value { get; set; } public double DD { get; set; } } List Data = new List() { new ItemData(true, 87, "First", 111.111), new ItemData(false, 43, "Second", 222.222) }; using (var db = new LiteDatabase(DBname)) { var Col = db.GetCollection(CollectionName);//here i can set interface type Col.InsertBulk(Data);// and here i cannot set interface type and programm use type of object (BsonMapper.Serialize.cs - line 183(184)) } And i get doc with all 4 fields Auto, Value, Name and DD, but i need only 3 like in interface. --- LiteDB/Client/Mapper/BsonMapper.Serialize.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/LiteDB/Client/Mapper/BsonMapper.Serialize.cs b/LiteDB/Client/Mapper/BsonMapper.Serialize.cs index ed26073b6..337890c8c 100644 --- a/LiteDB/Client/Mapper/BsonMapper.Serialize.cs +++ b/LiteDB/Client/Mapper/BsonMapper.Serialize.cs @@ -176,9 +176,13 @@ private BsonDocument SerializeDictionary(Type type, IDictionary dict, int depth) return o; } + //some settings + bool _useInterface=true; + private BsonDocument SerializeObject(Type type, object obj, int depth) { - var t = obj.GetType(); + //var t = obj.GetType(); + var t = _useInterface ? type : obj.GetType(); var doc = new BsonDocument(); var entity = this.GetEntityMapper(t); @@ -209,4 +213,4 @@ private BsonDocument SerializeObject(Type type, object obj, int depth) return doc; } } -} \ No newline at end of file +}