From 594c467b5f0808ce3727f5b05da9e8cfa6e1a449 Mon Sep 17 00:00:00 2001 From: Recheis Date: Sat, 13 Aug 2016 21:35:29 +0200 Subject: [PATCH 1/2] changed code to compile with C#5 --- LiteDB.Tests/Tests/ShrinkTest.cs | 10 +++++----- LiteDB/Query/Linq/QueryVisitor.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/LiteDB.Tests/Tests/ShrinkTest.cs b/LiteDB.Tests/Tests/ShrinkTest.cs index 4df81615f..9cf690002 100644 --- a/LiteDB.Tests/Tests/ShrinkTest.cs +++ b/LiteDB.Tests/Tests/ShrinkTest.cs @@ -13,8 +13,8 @@ public class ShrinkTest : TestBase public ShrinkTest() { - _customerName = $"Jo{new string('h', 1000)}n Doe"; - _productName = $"Prod{new string('u', 1000)}ct"; + _customerName = "Jo"+new string('h', 1000)+"n Doe"; + _productName = "Prod"+new string('u', 1000)+"ct"; } [TestMethod] @@ -51,7 +51,7 @@ public void Shrink_One_Test() { var customers = db.GetCollection("customers"); - Create(customers, id => new Customer { Id = id, Name = $"{_customerName}{id}" }); + Create(customers, id => new Customer { Id = id, Name = ""+_customerName+id }); sizeAfterInserts = GetLocalDbSizeMegabytes(tmp.Filename); @@ -79,8 +79,8 @@ public void Shrink_Many_Test() var products = db.GetCollection("products"); var customers = db.GetCollection("customers"); - Create(products, id => new Product { ProductId = id, Name = $"{_customerName}{id}" }); - Create(customers, id => new Customer { Id = id, Name = $"{_productName}{id}" }); + Create(products, id => new Product { ProductId = id, Name = ""+_customerName+id }); + Create(customers, id => new Customer { Id = id, Name = "" + _customerName + id }); sizeAfterInserts = GetLocalDbSizeMegabytes(tmp.Filename); diff --git a/LiteDB/Query/Linq/QueryVisitor.cs b/LiteDB/Query/Linq/QueryVisitor.cs index fc9cff576..294600c33 100644 --- a/LiteDB/Query/Linq/QueryVisitor.cs +++ b/LiteDB/Query/Linq/QueryVisitor.cs @@ -139,8 +139,8 @@ private BsonValue VisitValue(Expression expr, Expression left) { // check if left side is an enum and convert to string before return Func convert = (type, value) => - { - var enumType = (left as UnaryExpression)?.Operand.Type; + { + var enumType = (left as UnaryExpression) == null ? null : (left as UnaryExpression).Operand.Type; if (enumType != null && enumType.GetTypeInfo().IsEnum) { From 8424975c6efcdab42925714d90159be7241c82bf Mon Sep 17 00:00:00 2001 From: Recheis Date: Sun, 14 Aug 2016 16:20:52 +0200 Subject: [PATCH 2/2] in BsonMapper generate meaningful error info on cast exception --- .../Mapper/BsonMapper.Deserialize.cs | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/LiteDB/Serializer/Mapper/BsonMapper.Deserialize.cs b/LiteDB/Serializer/Mapper/BsonMapper.Deserialize.cs index 88013cb24..ea9206c31 100644 --- a/LiteDB/Serializer/Mapper/BsonMapper.Deserialize.cs +++ b/LiteDB/Serializer/Mapper/BsonMapper.Deserialize.cs @@ -242,27 +242,41 @@ private void DeserializeDictionary(Type K, Type T, IDictionary dict, BsonDocumen private void DeserializeObject(Type type, object obj, BsonDocument value) { var props = this.GetPropertyMapper(type); - - foreach (var prop in props.Values) + PropertyMapper property=null; + BsonValue bson_value=null; + try { - // property is read only - if (prop.Setter == null) continue; - - var val = value[prop.FieldName]; - if (!val.IsNull) + foreach (var prop in props.Values) { - // check if has a custom deserialize function - if (prop.Deserialize != null) - { - prop.Setter(obj, prop.Deserialize(val, this)); - } - else + property = prop; + // property is read only + if (prop.Setter == null) continue; + + var val = value[prop.FieldName]; + bson_value = val; + + if (!val.IsNull) { - prop.Setter(obj, this.Deserialize(prop.PropertyType, val)); + // check if has a custom deserialize function + if (prop.Deserialize != null) + { + prop.Setter(obj, prop.Deserialize(val, this)); + + } + else + { + prop.Setter(obj, this.Deserialize(prop.PropertyType, val)); + } } } } + catch (InvalidCastException e) + { + var typename = property == null ? null : property.PropertyType.Name; + var msg = string.Format("Cast from '{0}' to '{1}' failed in document: {2}", bson_value, typename, value); + throw new InvalidCastException(msg, e); + } } } } \ No newline at end of file