Skip to content

Commit 967e8f6

Browse files
authored
Merge pull request #566 from FredericDgrv/master
Fix for Nullable Enum
2 parents 199b8df + 7db4768 commit 967e8f6

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

src/SQLite.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,12 +2069,23 @@ public Column(PropertyInfo prop, CreateFlags createFlags = CreateFlags.None)
20692069
StoreAsText = prop.PropertyType.GetTypeInfo().CustomAttributes.Any(x => x.AttributeType == typeof(StoreAsTextAttribute));
20702070
}
20712071

2072-
public void SetValue (object obj, object val)
2073-
{
2074-
_prop.SetValue (obj, val, null);
2075-
}
2076-
2077-
public object GetValue (object obj)
2072+
public void SetValue(object obj, object val)
2073+
{
2074+
#if !USE_NEW_REFLECTION_API
2075+
if (ColumnType.IsEnum)
2076+
#else
2077+
if (ColumnType.GetTypeInfo().IsEnum)
2078+
#endif
2079+
{
2080+
_prop.SetValue(obj, Enum.ToObject(ColumnType, val));
2081+
}
2082+
else
2083+
{
2084+
_prop.SetValue(obj, val, null);
2085+
}
2086+
}
2087+
2088+
public object GetValue (object obj)
20782089
{
20792090
return _prop.GetValue (obj, null);
20802091
}

tests/EnumNullableTest.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
7+
#if NETFX_CORE
8+
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
9+
using SetUp = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestInitializeAttribute;
10+
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
11+
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
12+
#else
13+
using NUnit.Framework;
14+
#endif
15+
16+
namespace SQLite.Tests
17+
{
18+
[TestFixture]
19+
public class EnumNullableTests
20+
{
21+
public enum TestEnum
22+
{
23+
Value1,
24+
25+
Value2,
26+
27+
Value3
28+
}
29+
30+
public class TestObj
31+
{
32+
[PrimaryKey]
33+
public int Id { get; set; }
34+
public TestEnum? Value { get; set; }
35+
36+
public override string ToString()
37+
{
38+
return string.Format("[TestObj: Id={0}, Value={1}]", Id, Value);
39+
}
40+
41+
}
42+
43+
public class TestDb : SQLiteConnection
44+
{
45+
public TestDb(String path)
46+
: base(path)
47+
{
48+
CreateTable<TestObj>();
49+
}
50+
}
51+
52+
[Test]
53+
public void ShouldPersistAndReadEnum()
54+
{
55+
var db = new TestDb(TestPath.GetTempFileName());
56+
57+
var obj1 = new TestObj() { Id = 1, Value = TestEnum.Value2 };
58+
var obj2 = new TestObj() { Id = 2, Value = TestEnum.Value3 };
59+
60+
var numIn1 = db.Insert(obj1);
61+
var numIn2 = db.Insert(obj2);
62+
Assert.AreEqual(1, numIn1);
63+
Assert.AreEqual(1, numIn2);
64+
65+
var result = db.Query<TestObj>("select * from TestObj").ToList();
66+
Assert.AreEqual(2, result.Count);
67+
Assert.AreEqual(obj1.Value, result[0].Value);
68+
Assert.AreEqual(obj2.Value, result[1].Value);
69+
70+
Assert.AreEqual(obj1.Id, result[0].Id);
71+
Assert.AreEqual(obj2.Id, result[1].Id);
72+
73+
db.Close();
74+
}
75+
}
76+
}

tests/SQLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Compile Include="ByteArrayTest.cs" />
4646
<Compile Include="ConcurrencyTest.cs" />
4747
<Compile Include="EnumCacheTest.cs" />
48+
<Compile Include="EnumNullableTest.cs" />
4849
<Compile Include="EnumTest.cs" />
4950
<Compile Include="ExceptionAssert.cs" />
5051
<Compile Include="InsertTest.cs" />

0 commit comments

Comments
 (0)