Skip to content

Commit ab192b1

Browse files
authored
Merge pull request #131 from wztech0192/develop
Add IDictionary support
2 parents 17a9264 + a95d9a9 commit ab192b1

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
namespace Tynamix.ObjectFiller.Test
8+
{
9+
[TestClass]
10+
public class DictionaryFillingTest
11+
{
12+
public class EntityA
13+
{
14+
public string Name { get; set; }
15+
public int ID { get; set; }
16+
public IDictionary<string, string> InterfaceDictionary { get; set; }
17+
public Dictionary<string, string> ConcreteDictionary { get; set; }
18+
19+
public NestedEntity NestedEntity { get; set; }
20+
}
21+
22+
public class NestedEntity
23+
{
24+
public IDictionary<string, string> InterfaceDictionary { get; set; }
25+
public Dictionary<string, string> ConcreteDictionary { get; set; }
26+
}
27+
28+
29+
[TestMethod]
30+
public void TestDictionaryType()
31+
{
32+
Filler<EntityA> filler = new Filler<EntityA>();
33+
34+
var result = filler.Create();
35+
36+
Assert.IsNotNull(result.Name);
37+
Assert.IsNotNull(result.ID);
38+
Assert.IsNotNull(result.InterfaceDictionary);
39+
Assert.IsTrue(result.InterfaceDictionary.Any());
40+
Assert.IsNotNull(result.ConcreteDictionary);
41+
Assert.IsTrue(result.ConcreteDictionary.Any());
42+
43+
Assert.IsNotNull(result.NestedEntity);
44+
Assert.IsNotNull(result.NestedEntity.InterfaceDictionary);
45+
Assert.IsTrue(result.NestedEntity.InterfaceDictionary.Any());
46+
Assert.IsNotNull(result.NestedEntity.ConcreteDictionary);
47+
Assert.IsTrue(result.NestedEntity.ConcreteDictionary.Any());
48+
49+
}
50+
}
51+
}

Tynamix.ObjectFiller/Filler.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,19 @@ private static bool ListParamTypeIsValid(Type listType, FillerSetupItem currentS
284284
/// </returns>
285285
private static bool TypeIsDictionary(Type type)
286286
{
287-
return type.GetImplementedInterfaces().Any(x => x == typeof(IDictionary));
287+
Type interfaceType = typeof(IDictionary);
288+
Type genericType = typeof(IDictionary<,>);
289+
290+
if (type.IsInterface())
291+
{
292+
return type.IsGenericType() &&
293+
(
294+
type.GetGenericTypeDefinition() == genericType ||
295+
type.GetImplementedInterfaces().Any(x => x.IsGenericType() && x.GetGenericTypeDefinition() == typeof(IDictionary<,>))
296+
);
297+
}
298+
299+
return type.GetImplementedInterfaces().Any(x => x == interfaceType);
288300
}
289301

290302
/// <summary>
@@ -751,7 +763,6 @@ private IDictionary GetFilledDictionary(
751763
FillerSetupItem currentSetupItem,
752764
HashStack<Type> typeTracker)
753765
{
754-
IDictionary dictionary = (IDictionary)Activator.CreateInstance(propertyType);
755766

756767
bool derivedType = !propertyType.GetGenericTypeArguments().Any();
757768

@@ -763,6 +774,24 @@ private IDictionary GetFilledDictionary(
763774
? propertyType.GetGenericTypeArguments()[1]
764775
: propertyType.GetTypeInfo().BaseType.GetGenericTypeArguments()[1];
765776

777+
IDictionary dictionary;
778+
if (!propertyType.IsInterface()
779+
&& propertyType.GetImplementedInterfaces().Any(x => x == typeof(IDictionary)))
780+
{
781+
dictionary = (IDictionary)Activator.CreateInstance(propertyType);
782+
}
783+
else if (propertyType.IsGenericType() && propertyType.GetGenericTypeDefinition() == typeof(IDictionary<,>)
784+
|| propertyType.GetImplementedInterfaces().Any(x => x.IsGenericType() && x.GetGenericTypeDefinition() == typeof(IDictionary<,>)))
785+
{
786+
Type openDictionaryType = typeof(Dictionary<,>);
787+
Type genericDictionaryType = openDictionaryType.MakeGenericType(keyType, valueType);
788+
dictionary = (IDictionary)Activator.CreateInstance(genericDictionaryType);
789+
}
790+
else
791+
{
792+
dictionary = (IDictionary)Activator.CreateInstance(propertyType);
793+
}
794+
766795
int maxDictionaryItems = 0;
767796

768797
if (keyType.IsEnum())

0 commit comments

Comments
 (0)