Skip to content

Commit 61fb9ac

Browse files
committed
#89 fix for dictionaries added
1 parent 35c55e2 commit 61fb9ac

File tree

3 files changed

+84
-23
lines changed

3 files changed

+84
-23
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace ObjectFiller.Test.BugfixTests
7+
{
8+
using Tynamix.ObjectFiller;
9+
10+
using Xunit;
11+
12+
13+
public class Bug89FillTypesInheritsFromDictionary
14+
{
15+
public class EntityA
16+
{
17+
public string Name { get; set; }
18+
public int ID { get; set; }
19+
public EntityBList Bs { get; set; }
20+
}
21+
public class EntityB
22+
{
23+
public string Name { get; set; }
24+
public Guid ID { get; set; }
25+
}
26+
public class EntityBList : Dictionary<string, EntityB>
27+
{
28+
public DateTime SomeDate { get; set; }
29+
}
30+
31+
[Fact]
32+
public void ADerivedDictionaryShallGetFilledAllProperties()
33+
{
34+
Filler<EntityA> filler = new Filler<EntityA>();
35+
36+
var result = filler.Create();
37+
38+
Assert.NotNull(result.Bs);
39+
Assert.InRange(result.Bs.SomeDate, DateTime.MinValue.AddSeconds(1), DateTime.MaxValue.AddSeconds(-1));
40+
Assert.True(result.Bs.Any());
41+
}
42+
}
43+
}

Tynamix.ObjectFiller.Test/BugfixTests/Bug89FillTypesInheritsFromList.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ namespace ObjectFiller.Test.BugfixTests
99

1010
using Xunit;
1111

12-
public class EntityA
13-
{
14-
public string Name { get; set; }
15-
public int ID { get; set; }
16-
public EntityBList Bs { get; set; }
17-
}
18-
public class EntityB
19-
{
20-
public string Name { get; set; }
21-
public Guid ID { get; set; }
22-
}
23-
public class EntityBList : List<EntityB>
24-
{
25-
public DateTime SomeDate { get; set; }
26-
}
27-
2812

2913
public class Bug89FillTypesInheritsFromList
3014
{
15+
public class EntityA
16+
{
17+
public string Name { get; set; }
18+
public int ID { get; set; }
19+
public EntityBList Bs { get; set; }
20+
}
21+
public class EntityB
22+
{
23+
public string Name { get; set; }
24+
public Guid ID { get; set; }
25+
}
26+
public class EntityBList : List<EntityB>
27+
{
28+
public DateTime SomeDate { get; set; }
29+
}
30+
3131
[Fact]
3232
public void ADerivedListShallGetFilledAllProperties()
3333
{

Tynamix.ObjectFiller/Filler.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private static bool TypeIsList(Type type)
293293
&& (typeToCheck.GetGenericTypeDefinition() == typeof(IEnumerable<>)
294294
|| typeToCheck.GetImplementedInterfaces().Any(x => x == typeof(IList)));
295295

296-
return typeIsList(type) || (type.GetTypeInfo().BaseType !=null && typeIsList(type.GetTypeInfo().BaseType));
296+
return typeIsList(type) || (type.GetTypeInfo().BaseType != null && typeIsList(type.GetTypeInfo().BaseType));
297297
}
298298

299299
/// <summary>
@@ -734,8 +734,16 @@ private IDictionary GetFilledDictionary(
734734
HashStack<Type> typeTracker)
735735
{
736736
IDictionary dictionary = (IDictionary)Activator.CreateInstance(propertyType);
737-
Type keyType = propertyType.GetGenericTypeArguments()[0];
738-
Type valueType = propertyType.GetGenericTypeArguments()[1];
737+
738+
bool derivedType = !propertyType.GetGenericTypeArguments().Any();
739+
740+
Type keyType = !derivedType
741+
? propertyType.GetGenericTypeArguments()[0]
742+
: propertyType.GetTypeInfo().BaseType.GetGenericTypeArguments()[0];
743+
744+
Type valueType = !derivedType
745+
? propertyType.GetGenericTypeArguments()[1]
746+
: propertyType.GetTypeInfo().BaseType.GetGenericTypeArguments()[1];
739747

740748
int maxDictionaryItems = 0;
741749

@@ -775,6 +783,16 @@ private IDictionary GetFilledDictionary(
775783
dictionary.Add(keyObject, valueObject);
776784
}
777785

786+
if (derivedType)
787+
{
788+
789+
var remainingProperties = propertyType.GetProperties(true)
790+
.Where(prop => this.GetSetMethodOnDeclaringType(prop) != null)
791+
.ToArray();
792+
793+
this.FillProperties(dictionary, remainingProperties, currentSetupItem, typeTracker);
794+
}
795+
778796
return dictionary;
779797
}
780798

@@ -831,10 +849,10 @@ private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem,
831849

832850
if (derivedList)
833851
{
834-
835-
var remainingProperties = propertyType.GetProperties(true)
836-
.Where(prop => this.GetSetMethodOnDeclaringType(prop) != null)
837-
.ToArray();
852+
853+
var remainingProperties = propertyType.GetProperties(true)
854+
.Where(prop => this.GetSetMethodOnDeclaringType(prop) != null)
855+
.ToArray();
838856

839857
this.FillProperties(list, remainingProperties, currentSetupItem, typeTracker);
840858
}

0 commit comments

Comments
 (0)