Skip to content

Commit

Permalink
Merge pull request #131 from wztech0192/develop
Browse files Browse the repository at this point in the history
Add IDictionary support
  • Loading branch information
Tynamix authored Jan 22, 2022
2 parents 17a9264 + a95d9a9 commit ab192b1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
51 changes: 51 additions & 0 deletions Tynamix.ObjectFiller.Test/DictionaryFillingTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Tynamix.ObjectFiller.Test
{
[TestClass]
public class DictionaryFillingTest
{
public class EntityA
{
public string Name { get; set; }
public int ID { get; set; }
public IDictionary<string, string> InterfaceDictionary { get; set; }
public Dictionary<string, string> ConcreteDictionary { get; set; }

public NestedEntity NestedEntity { get; set; }
}

public class NestedEntity
{
public IDictionary<string, string> InterfaceDictionary { get; set; }
public Dictionary<string, string> ConcreteDictionary { get; set; }
}


[TestMethod]
public void TestDictionaryType()
{
Filler<EntityA> filler = new Filler<EntityA>();

var result = filler.Create();

Assert.IsNotNull(result.Name);
Assert.IsNotNull(result.ID);
Assert.IsNotNull(result.InterfaceDictionary);
Assert.IsTrue(result.InterfaceDictionary.Any());
Assert.IsNotNull(result.ConcreteDictionary);
Assert.IsTrue(result.ConcreteDictionary.Any());

Assert.IsNotNull(result.NestedEntity);
Assert.IsNotNull(result.NestedEntity.InterfaceDictionary);
Assert.IsTrue(result.NestedEntity.InterfaceDictionary.Any());
Assert.IsNotNull(result.NestedEntity.ConcreteDictionary);
Assert.IsTrue(result.NestedEntity.ConcreteDictionary.Any());

}
}
}
33 changes: 31 additions & 2 deletions Tynamix.ObjectFiller/Filler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,19 @@ private static bool ListParamTypeIsValid(Type listType, FillerSetupItem currentS
/// </returns>
private static bool TypeIsDictionary(Type type)
{
return type.GetImplementedInterfaces().Any(x => x == typeof(IDictionary));
Type interfaceType = typeof(IDictionary);
Type genericType = typeof(IDictionary<,>);

if (type.IsInterface())
{
return type.IsGenericType() &&
(
type.GetGenericTypeDefinition() == genericType ||
type.GetImplementedInterfaces().Any(x => x.IsGenericType() && x.GetGenericTypeDefinition() == typeof(IDictionary<,>))
);
}

return type.GetImplementedInterfaces().Any(x => x == interfaceType);
}

/// <summary>
Expand Down Expand Up @@ -751,7 +763,6 @@ private IDictionary GetFilledDictionary(
FillerSetupItem currentSetupItem,
HashStack<Type> typeTracker)
{
IDictionary dictionary = (IDictionary)Activator.CreateInstance(propertyType);

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

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

IDictionary dictionary;
if (!propertyType.IsInterface()
&& propertyType.GetImplementedInterfaces().Any(x => x == typeof(IDictionary)))
{
dictionary = (IDictionary)Activator.CreateInstance(propertyType);
}
else if (propertyType.IsGenericType() && propertyType.GetGenericTypeDefinition() == typeof(IDictionary<,>)
|| propertyType.GetImplementedInterfaces().Any(x => x.IsGenericType() && x.GetGenericTypeDefinition() == typeof(IDictionary<,>)))
{
Type openDictionaryType = typeof(Dictionary<,>);
Type genericDictionaryType = openDictionaryType.MakeGenericType(keyType, valueType);
dictionary = (IDictionary)Activator.CreateInstance(genericDictionaryType);
}
else
{
dictionary = (IDictionary)Activator.CreateInstance(propertyType);
}

int maxDictionaryItems = 0;

if (keyType.IsEnum())
Expand Down

0 comments on commit ab192b1

Please sign in to comment.