diff --git a/source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs b/source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs index 60b3b340..5920779c 100644 --- a/source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs +++ b/source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs @@ -222,5 +222,28 @@ public void CanSerializeAndDeserializeHashSet() Assert.IsTrue(prop.Property.Contains("test1")); Assert.IsTrue(prop.Property.Contains("test2")); } + + public class EnumHelper + { + public enum Test + { + A=1, + B=2 + } + + public List Tests { get; set; } + } + + [Test] + public void CanSerializerAndDesializeEnumLists() + { + var helper = new EnumHelper {Tests = new List {EnumHelper.Test.A}}; + var bson = Serialize(helper); + var deserialize = Deserialize(bson); + + Assert.IsNotNull(deserialize); + Assert.IsNotNull(deserialize.Tests); + Assert.Contains(EnumHelper.Test.A, deserialize.Tests); + } } } \ No newline at end of file diff --git a/source/MongoDB/Configuration/CollectionAdapters/ArrayCollectionAdapter.cs b/source/MongoDB/Configuration/CollectionAdapters/ArrayCollectionAdapter.cs index e2edc0a3..d658e27e 100644 --- a/source/MongoDB/Configuration/CollectionAdapters/ArrayCollectionAdapter.cs +++ b/source/MongoDB/Configuration/CollectionAdapters/ArrayCollectionAdapter.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using MongoDB.Configuration.Mapping.Util; namespace MongoDB.Configuration.CollectionAdapters { @@ -16,9 +17,7 @@ public class ArrayCollectionAdapter : ICollectionAdapter /// public object CreateCollection(Type elementType, object[] elements) { - var array = Array.CreateInstance(elementType, elements.Length); - elements.CopyTo(array, 0); - return array; + return ValueConverter.ConvertArray(elements, elementType); } /// diff --git a/source/MongoDB/Configuration/CollectionAdapters/GenericListCollectionAdapter.cs b/source/MongoDB/Configuration/CollectionAdapters/GenericListCollectionAdapter.cs index 31f6dfd5..a10d718f 100644 --- a/source/MongoDB/Configuration/CollectionAdapters/GenericListCollectionAdapter.cs +++ b/source/MongoDB/Configuration/CollectionAdapters/GenericListCollectionAdapter.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using MongoDB.Configuration.Mapping.Util; namespace MongoDB.Configuration.CollectionAdapters { @@ -20,8 +21,7 @@ public class GenericListCollectionAdapter : ICollectionAdapter public object CreateCollection(Type elementType, object[] elements) { var closedListType = OpenListType.MakeGenericType(elementType); - var typedElements = Array.CreateInstance(elementType, elements.Length); - Array.Copy(elements, typedElements, elements.Length); + var typedElements = ValueConverter.ConvertArray(elements, elementType); return Activator.CreateInstance(closedListType, typedElements); } diff --git a/source/MongoDB/Configuration/CollectionAdapters/GenericSetCollectionAdapter.cs b/source/MongoDB/Configuration/CollectionAdapters/GenericSetCollectionAdapter.cs index 0cde5a42..7db51c9f 100644 --- a/source/MongoDB/Configuration/CollectionAdapters/GenericSetCollectionAdapter.cs +++ b/source/MongoDB/Configuration/CollectionAdapters/GenericSetCollectionAdapter.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using MongoDB.Configuration.Mapping.Util; namespace MongoDB.Configuration.CollectionAdapters { @@ -20,8 +21,7 @@ public class GenericSetCollectionAdapter : ICollectionAdapter public object CreateCollection(Type elementType, object[] elements) { var closedSetType = OpenSetType.MakeGenericType(elementType); - var typedElements = Array.CreateInstance(elementType, elements.Length); - Array.Copy(elements, typedElements, elements.Length); + var typedElements = ValueConverter.ConvertArray(elements,elementType); return Activator.CreateInstance(closedSetType, new[] { typedElements }); } diff --git a/source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs b/source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs index 36836ea0..7ab9d8bd 100644 --- a/source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs +++ b/source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs @@ -35,5 +35,15 @@ public static object Convert(object value, Type type) return value; } + + public static Array ConvertArray(object[] elements, Type type) + { + var array = Array.CreateInstance(type, elements.Length); + + for(var i = 0; i < elements.Length; i++) + array.SetValue(Convert(elements[i], type), i); + + return array; + } } } \ No newline at end of file