public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) { var genMethodInfo = typeof(Gen).GetMethod(nameof(Gen.Enum), BindingFlags.Public | BindingFlags.Static); var genericGenMethodInfo = genMethodInfo.MakeGenericMethod(type); return((IGen)genericGenMethodInfo.Invoke(null !, new object[] { })); }
private static IGen <T> CreateGenGeneric <T>(IReflectedGenHandler innerHandler, ReflectedGenHandlerContext context, ContextualErrorFactory errorFactory) { var constructor = TryFindConstructor(typeof(T)) !; var parameterGens = constructor .GetParameters() .Select(parameter => innerHandler .CreateGen(parameter.ParameterType, context.Next(parameter.Name, parameter.ParameterType)) // TODO: Indicate it's a ctor param in the path .Cast <object>()); return(Gen .Zip(parameterGens) .SelectMany(parameters => { try { return Gen.Constant((T)constructor.Invoke(parameters.ToArray())); } catch (TargetInvocationException ex) { var innerEx = ex.InnerException; var message = $"'{innerEx.GetType()}' was thrown while calling constructor with message '{innerEx.Message}'"; return errorFactory(message, context).Cast <T>(); } })); }
public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) { var methodInfo = typeof(DefaultConstructorReflectedGenHandler).GetMethod( nameof(CreateGenGeneric), BindingFlags.Static | BindingFlags.NonPublic) !; var genericMethodInfo = methodInfo.MakeGenericMethod(type); return((IGen)genericMethodInfo.Invoke(null !, new object[] { innerHandler, context, _errorFactory })); }
public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) { var elementType = type.GetElementType(); var elementGen = innerHandler.CreateGen(elementType, context); var methodInfo = typeof(ArrayReflectedGenHandler).GetMethod( nameof(CreateArrayGen), BindingFlags.Static | BindingFlags.NonPublic) !; var genericMethodInfo = methodInfo.MakeGenericMethod(elementType); return((IGen)genericMethodInfo.Invoke(null !, new object[] { elementGen })); }
public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) { var elementType = type.GetGenericArguments().Single(); var elementGen = innerHandler.CreateGen(elementType, context); var genericTypeDefinition = type.GetGenericTypeDefinition(); var methodName = GenMethodByGenericTypeDefinition[genericTypeDefinition]; var methodInfo = typeof(CollectionReflectedGenHandler).GetMethod( methodName, BindingFlags.Static | BindingFlags.NonPublic) !; var genericMethodInfo = methodInfo.MakeGenericMethod(elementType); return((IGen)genericMethodInfo.Invoke(null !, new object[] { elementGen })); }
public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) { foreach (var kvp in _registeredGensByType) { var registeredType = kvp.Key; var genTypeArgument = kvp.Value.ReflectGenTypeArgument(); if (registeredType.IsAssignableFrom(genTypeArgument) == false) { return(_errorFactory( $"type '{genTypeArgument}' was not assignable to the type it was registered to, '{registeredType}'", context)); } } return(_registeredGensByType[type]); }
public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) { if (context.TypeHistory.Skip(1).Any(t => t == type)) { return(_errorFactory($"detected circular reference on type '{type}'", context)); } var gen = _genHandlersByPriority .Where(genFactory => genFactory.CanHandleGen(type, context)) .Select(genFactory => genFactory.CreateGen(innerHandler, type, context)) .FirstOrDefault(); if (gen == null) { return(_errorFactory($"could not resolve type '{type}'", context)); } return(gen); }
private static IGen <T> CreateGenGeneric <T>(IReflectedGenHandler innerHandler, ReflectedGenHandlerContext context, ContextualErrorFactory errorFactory) { return(Gen .Zip( Gen.Zip(CreateSetPropertyActionGens(innerHandler, typeof(T), context)), Gen.Zip(CreateSetFieldActionGens(innerHandler, typeof(T), context))) .SelectMany((x) => { T instance; try { instance = (T)Activator.CreateInstance(typeof(T)); } catch (TargetInvocationException ex) { var innerEx = ex.InnerException; var message = $"'{innerEx.GetType()}' was thrown while calling constructor with message '{innerEx.Message}'"; return errorFactory(message, context).Cast <T>(); } foreach (var setPropertyAction in x.Item1) { try { setPropertyAction(instance); } catch (TargetInvocationException ex) { var innerEx = ex.InnerException; var message = $"'{innerEx.GetType()}' was thrown while setting property with message '{innerEx.Message}'"; return errorFactory(message, context).Cast <T>(); } } foreach (var setFieldAction in x.Item2) { setFieldAction(instance); } return Gen.Constant(instance); })); }
public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) => _memberOverrides .Where(mo => mo.Path == context.MemberPath) .Select(mo => mo.Gen) .Last(); // Last registered override wins
private static IEnumerable <IGen <Action <object> > > CreateSetFieldActionGens(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) { return(type .GetFields() .Where(field => field.IsPublic) .Select(field => innerHandler .CreateGen(field.FieldType, context.Next(field.Name, field.FieldType)) .Cast <object>() .Select <object, Action <object> >(value => obj => field.SetValue(obj, value)))); }
private static IEnumerable <IGen <Action <object> > > CreateSetPropertyActionGens(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) { return(type .GetProperties() .Where(property => property.CanWrite) .Select(property => innerHandler .CreateGen(property.PropertyType, context.Next(property.Name, property.PropertyType)) .Cast <object>() .Select <object, Action <object> >(value => obj => property.SetValue(obj, value)))); }
public static IGen CreateGen( this IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context) => innerHandler.CreateGen(innerHandler, type, context);