Skip to content

Commit 31e642b

Browse files
committed
[QUICKFIX] Support generating dicts by reflection
1 parent 4dd7b01 commit 31e642b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/GalaxyCheck/Gens/ReflectedGenHelpers/ReflectedGenBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static IGen Build(
1919
new NullableGenHandler(),
2020
new RegistryReflectedGenHandler(registeredGensByType),
2121
new CollectionReflectedGenHandler(),
22+
new DictionaryReflectedGenHandler(),
2223
new ArrayReflectedGenHandler(),
2324
new EnumReflectedGenHandler(),
2425
new DefaultConstructorReflectedGenHandler(),
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace GalaxyCheck.Gens.ReflectedGenHelpers.ReflectedGenHandlers
7+
{
8+
internal class DictionaryReflectedGenHandler : IReflectedGenHandler
9+
{
10+
public bool CanHandleGen(Type type, ReflectedGenHandlerContext context)
11+
{
12+
if (type.IsGenericType)
13+
{
14+
var genericTypeDefinition = type.GetGenericTypeDefinition();
15+
return GenMethodByGenericTypeDefinition.ContainsKey(genericTypeDefinition);
16+
}
17+
18+
return false;
19+
}
20+
21+
public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context)
22+
{
23+
var keyType = type.GetGenericArguments().First();
24+
var elementType = type.GetGenericArguments().Skip(1).First();
25+
26+
var elementNullabilityInfo = context.NullabilityInfo?.GenericTypeArguments.Skip(1).SingleOrDefault();
27+
var keyGen = innerHandler.CreateGen(keyType, context.Next("[*]", keyType, null));
28+
var elementGen = innerHandler.CreateGen(elementType, context.Next("[*]", elementType, elementNullabilityInfo));
29+
30+
var genericTypeDefinition = type.GetGenericTypeDefinition();
31+
var methodName = GenMethodByGenericTypeDefinition[genericTypeDefinition];
32+
33+
var methodInfo = typeof(DictionaryReflectedGenHandler).GetMethod(
34+
methodName,
35+
BindingFlags.Static | BindingFlags.NonPublic)!;
36+
37+
var genericMethodInfo = methodInfo.MakeGenericMethod(keyType, elementType);
38+
39+
return (IGen)genericMethodInfo.Invoke(null!, new object[] { keyGen, elementGen })!;
40+
}
41+
42+
private static readonly IReadOnlyDictionary<Type, string> GenMethodByGenericTypeDefinition = new Dictionary<Type, string>
43+
{
44+
{ typeof(Dictionary<,>), nameof(CreateDictionaryGen) },
45+
};
46+
47+
private static IGen<Dictionary<TKey, TValue>> CreateDictionaryGen<TKey, TValue>(IGen<TKey> keyGen, IGen<TValue> elementGen) where TKey : notnull
48+
{
49+
return Gen
50+
.Int32()
51+
.Between(0, 2)
52+
.SelectMany(count => Gen.Zip(keyGen.SetOf().WithCount(count), elementGen.ListOf().WithCount(count)))
53+
.Select(keysAndValues => Enumerable.Zip(keysAndValues.Item1, keysAndValues.Item2).ToDictionary(x => x.First, x => x.Second));
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)