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>(); } })); }
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)))); }