public RegistryReflectedGenHandler( IReadOnlyDictionary <Type, IGen> registeredGensByType, ContextualErrorFactory errorFactory) { _registeredGensByType = registeredGensByType; _errorFactory = errorFactory; }
public CompositeReflectedGenHandler( IReadOnlyList <IReflectedGenHandler> genHandlersByPriority, ContextualErrorFactory errorFactory) { _genHandlersByPriority = genHandlersByPriority; _errorFactory = errorFactory; }
public static IGen Build( Type type, IReadOnlyDictionary <Type, IGen> registeredGensByType, IReadOnlyList <ReflectedGenMemberOverride> memberOverrides, ErrorFactory errorFactory, ReflectedGenHandlerContext context) { ContextualErrorFactory contextualErrorFactory = (message, context) => { var suffix = context.Members.Count() == 1 ? "" : $" at path '{context.MemberPath}'"; return(errorFactory(message + suffix)); }; var genFactoriesByPriority = new List <IReflectedGenHandler> { new MemberOverrideReflectedGenHandler(memberOverrides), new RegistryReflectedGenHandler(registeredGensByType, contextualErrorFactory), new CollectionReflectedGenHandler(), new SetReflectedGenHandler(), new ArrayReflectedGenHandler(), new EnumReflectedGenHandler(), new DefaultConstructorReflectedGenHandler(contextualErrorFactory), new NonDefaultConstructorReflectedGenHandler(contextualErrorFactory), }; var compositeReflectedGenFactory = new CompositeReflectedGenHandler(genFactoriesByPriority, contextualErrorFactory); return(compositeReflectedGenFactory.CreateGen(type, context)); }
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 DefaultConstructorReflectedGenHandler(ContextualErrorFactory errorFactory) { _errorFactory = errorFactory; }
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>(); } })); }