|
| 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