LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

System.InvalidCastException for nullable decimal

Open automada opened this issue 11 years ago • 3 comments

If I use a POCO with a nullable decimal, it looks to be stored correctly as a double, but I get an System.InvalidCastException when I try to retrieve the object.

Fetching it as a BsonDocument works fine, and using a non nullable decimal also works fine.

automada avatar Sep 17 '15 19:09 automada

Changing this (in BsonMapper.Deserialize) seems to fix the immediate problem, but not sure if it breaks something else.

Before

// if is nullable, get underlying type
else if (Reflection.IsNullable(type))
{
    type = Reflection.UnderlyingTypeOf(type);
}

After

// if is nullable, use underlying type
else if (Reflection.IsNullable(type))
{
    return Convert.ChangeType(value.RawValue, Nullable.GetUnderlyingType(type));
}

automada avatar Sep 17 '15 20:09 automada

Hi @automada, thanks for find this. Has an extra else before if after change type value. Please, compile master branch and test in your app.

mbdavid avatar Sep 19 '15 02:09 mbdavid

Hi @mbdavid! Just tested it and it works as expected. Thanks for both resolving this so quickly and a great project!

automada avatar Sep 21 '15 09:09 automada