Permalink
Browse files

Fix the failing test.

  • Loading branch information...
1 parent b8a62c2 commit 893fba8689ea1877d14fffc1aea508fb88439bb8 @lanwin lanwin committed Jul 14, 2010
View
7 source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs
@@ -337,13 +337,18 @@ public void SerializesAnEnumAsIntWhenItsUsedAsDictionaryKey()
[Test]
public void CanDeserializeADictionaryWithEnumAsKey()
{
- var bson = Serialize<Document>(new Document("Dict", new Dictionary<DateTimeKind,int> {{DateTimeKind.Utc,9}}));
+ var bson = Serialize<Document>(new Document("Dict", new Document(( (int)DateTimeKind.Utc ).ToString(), 9)));
var prop = Deserialize<DictionaryWithEnumAsKeyHelper>(bson);
Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Dict);
Assert.AreEqual(1,prop.Dict.Count);
Assert.AreEqual(9,prop.Dict[DateTimeKind.Utc]);
}
+
+ public class DictionaryWithEnumAsValueHelper
+ {
+ public Dictionary<int,DateTimeKind> Dict { get; set; }
+ }
}
}
View
2 source/MongoDB/Configuration/DictionaryAdapters/GenericDictionaryDictionaryAdapter.cs
@@ -35,7 +35,7 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
- return document.ToDictionary(pair => (TKey)Convert.ChangeType(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
+ return document.ToDictionary(pair => (TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
}
/// <summary>
View
2 source/MongoDB/Configuration/DictionaryAdapters/GenericSortedListDictionaryAdapter.cs
@@ -37,7 +37,7 @@ public object CreateDictionary(Document document)
var list = new SortedList<TKey, TValue>();
foreach(var pair in document)
- list.Add((TKey)Convert.ChangeType(pair.Key, typeof(TKey)), (TValue)pair.Value);
+ list.Add((TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), (TValue)pair.Value);
return list;
}
View
5 source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs
@@ -17,7 +17,10 @@ public static object Convert(object value, Type type)
var code = System.Convert.GetTypeCode(value);
if(type.IsEnum)
- value = Enum.ToObject(type, value);
+ if(value is string)
+ value = Enum.Parse(type, (string)value);
+ else
+ value = Enum.ToObject(type, value);
else if(type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Nullable<>))
value = System.Convert.ChangeType(value, Nullable.GetUnderlyingType(type));

0 comments on commit 893fba8

Please sign in to comment.