private bool ValidateConstructor(ConstructorInfo constructor, List <IATMap> chain) { var parameters = constructor.GetParameters(); if (HasBadParameter(parameters)) { return(false); } var parameterGenerators = ValidateParameters(parameters, chain); if (parameterGenerators == null) { return(false); } NewExpression innerExpr; if (parameterGenerators.Length == 0) { innerExpr = Expression.New(constructor); } else { innerExpr = Expression.New(constructor, parameterGenerators.Select(p => p.GetCallExpression()).ToArray()); } generator = GeneratorFuncFactory.CreateFromFuncGeneric <T>(Expression.Lambda <Func <T> >(innerExpr)); validated = true; return(true); }
private void AssertSingleton() { if (!isSingleton || singletonInstance != null) { return; } singletonInstance = generator.GetCallResult(); generator = GeneratorFuncFactory.CreateFromFuncGeneric <T>(() => singletonInstance); }
private static void HandleMember(Dictionary <string, IGeneratorFunc> dict, MemberInfo member, object obj) { if (dict.ContainsKey(member.Name)) { return; } var mType = GetMemberType(member); var getExp = Expression.Lambda(Expression.PropertyOrField(Expression.Constant(obj), member.Name)).Compile(); var lExp = Expression.Lambda(Expression.Convert(Expression.Constant(getExp.DynamicInvoke()), mType)); dict.Add(member.Name, GeneratorFuncFactory.CreateFromFunc(mType, lExp)); }
private bool ValidateParameter(ParameterInfo parameter, IGeneratorFunc[] parameterGenerators, int index, List <IATMap> chain) { if (constructorParameters.ContainsKey(parameter.Name)) { parameterGenerators[index] = constructorParameters[parameter.Name]; } else { IATMFactory firstMap; if (!TryGetFirstMapForType(parameter.ParameterType, chain, out firstMap)) { return(false); } var method = typeof(IATMFactory <>).MakeGenericType(parameter.ParameterType).GetMethod("Get"); var lExp = Expression.Lambda(Expression.Call(Expression.Constant(firstMap), method)); parameterGenerators[index] = GeneratorFuncFactory.CreateFromFunc(parameter.ParameterType, lExp); } return(true); }
IATMOptions IATMap.To(object instance) { generator = GeneratorFuncFactory.CreateFromFuncGeneric <T>(() => (T)instance); validated = true; return(this); }
IATMOptions IATMap.To(Func <object> func) { generator = GeneratorFuncFactory.CreateFromFuncGeneric <T>(() => (T)func()); validated = true; return(this); }