public static int GetCollectionSize(this IObjectCreationContext context, Type type) { var config = context.Config; int size; // absolute if (config.AbsolutePathCollectionSizes.TryGetValue(context.InvocationPath, out size)) { return(size); } // relative var bestMemberInfo = config.RelativeCollectionSizes.GetBestMatch(context); if (bestMemberInfo != null) { return(bestMemberInfo.Size); } // type if (config.TypeCollectionSizes.TryGetValue(type, out size)) { return(size); } //generic return(config.DefaultCollectionsSize); }
public EvaluationResult Evaluate(Type type, IObjectCreationContext context) { if (type == typeof(string)) { return(new EvaluationResult(string.Empty)); } return(EvaluationResult.Empty); }
public EvaluationResult Evaluate(Type type, IObjectCreationContext context) { if (context.Config.TypeFillers.TryGetValue(type, out var filler)) { var result = filler(context); return(new EvaluationResult(result)); } return(EvaluationResult.Empty); }
public EvaluationResult Evaluate(Type type, IObjectCreationContext context) { if (context.Config.AbsolutePathFillers.TryGetValue(context.InvocationPath, out var fillEvaluator)) { var result = fillEvaluator(context); return(new EvaluationResult(result)); } return(EvaluationResult.Empty); }
public EvaluationResult Evaluate(Type type, IObjectCreationContext context) { var bestMemberInfo = context.Config.RelativeTypeFillers.GetBestMatch(context); if (bestMemberInfo != null) { var result = bestMemberInfo.Evaluate(context); return(new EvaluationResult(result)); } return(EvaluationResult.Empty); }
internal static object NewObject(Type type, IObjectCreationContext context) { foreach (var evaluator in context.Evaluators) { var result = evaluator.Evaluate(type, context); if (result.IsSuccessful) { return(result.Value); } } throw new FillingException(context.InvocationPath, type); }
public EvaluationResult Evaluate(Type type, IObjectCreationContext context) { if (!CanBeArray(type)) { return(EvaluationResult.Empty); } var elementsInCollections = context.GetCollectionSize(type); var elementType = type.GetGenericArguments().FirstOrDefault(); Array array; if (elementType != null) { array = Array.CreateInstance(elementType, elementsInCollections); } else { array = (Array)Activator.CreateInstance(type, elementsInCollections); elementType = type.GetElementType(); } for (var i = 0; i < elementsInCollections; i++) { object value; if (context.Config.TypeElementsFillers.TryGetValue(type, out var filler)) { value = filler(i); } else { if (context.Config.AbsoluteElementsFillers.TryGetValue(context.InvocationPath, out filler)) { value = filler(i); } else { value = context.NewObject(elementType); } } array.SetValue(value, i); } return(new EvaluationResult(array)); }
public EvaluationResult Evaluate(Type type, IObjectCreationContext context) { var result = CreateByActivator(type); var propertyInfos = GetProperties(type); foreach (var propertyInfo in propertyInfos) { context.PushInvocation(propertyInfo); if (context.GetCyclicReferencesDepth() <= MaxCyclicDepth) { var value = context.NewObject(propertyInfo.PropertyType); propertyInfo.SetValue(result, value); } context.PopInvocation(); } return(new EvaluationResult(result)); }
public EvaluationResult Evaluate(Type type, IObjectCreationContext context) { if (!IsGenericList(type)) { return(EvaluationResult.Empty); } var elementType = type.HasElementType ? type.GetElementType() : type.GenericTypeArguments.First(); var elementsInCollections = context.GetCollectionSize(type); var list = (IList)Activator.CreateInstance(type); for (var i = 0; i < elementsInCollections; i++) { var value = context.NewObject(elementType); list.Add(value); } return(new EvaluationResult(list)); }
public static TMemberInfo GetBestMatch <TMemberInfo>(this IEnumerable <TMemberInfo> relativeMemberInfos, IObjectCreationContext context) where TMemberInfo : BaseRelativeMemberInfo { return(relativeMemberInfos .ToLookup(context.GetMatchScore) .Where(pair => pair.Key > 0) .OrderByDescending(pair => pair.Key) .Select(pair => pair.First()) .FirstOrDefault()); }
public object Evaluate(IObjectCreationContext context) { return(this.factoryFunc(context)); }