Skip to content

Commit

Permalink
Fix Conversion of Arrays while deserialization (in case the elements …
Browse files Browse the repository at this point in the history
…are int but the dest array a ENUM)
  • Loading branch information
lanwin committed Jun 1, 2010
1 parent 33c6f73 commit 30445d7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
Expand Up @@ -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<Test> Tests { get; set; }
}

[Test]
public void CanSerializerAndDesializeEnumLists()
{
var helper = new EnumHelper {Tests = new List<EnumHelper.Test> {EnumHelper.Test.A}};
var bson = Serialize<EnumHelper>(helper);
var deserialize = Deserialize<EnumHelper>(bson);

Assert.IsNotNull(deserialize);
Assert.IsNotNull(deserialize.Tests);
Assert.Contains(EnumHelper.Test.A, deserialize.Tests);
}
}
}
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.CollectionAdapters
{
Expand All @@ -16,9 +17,7 @@ public class ArrayCollectionAdapter : ICollectionAdapter
/// <returns></returns>
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);
}

/// <summary>
Expand Down
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.CollectionAdapters
{
Expand All @@ -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);
}

Expand Down
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.CollectionAdapters
{
Expand All @@ -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 });
}

Expand Down
10 changes: 10 additions & 0 deletions source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs
Expand Up @@ -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;
}
}
}

0 comments on commit 30445d7

Please sign in to comment.