示例#1
0
文件: ATMap.cs 项目: hoffmannj/ATM
        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);
        }
示例#2
0
文件: ATMap.cs 项目: hoffmannj/ATM
 private void AssertSingleton()
 {
     if (!isSingleton || singletonInstance != null)
     {
         return;
     }
     singletonInstance = generator.GetCallResult();
     generator         = GeneratorFuncFactory.CreateFromFuncGeneric <T>(() => singletonInstance);
 }
示例#3
0
        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));
        }
示例#4
0
文件: ATMap.cs 项目: hoffmannj/ATM
 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);
 }
示例#5
0
文件: ATMap.cs 项目: hoffmannj/ATM
 IATMOptions IATMap.To(object instance)
 {
     generator = GeneratorFuncFactory.CreateFromFuncGeneric <T>(() => (T)instance);
     validated = true;
     return(this);
 }
示例#6
0
文件: ATMap.cs 项目: hoffmannj/ATM
 IATMOptions IATMap.To(Func <object> func)
 {
     generator = GeneratorFuncFactory.CreateFromFuncGeneric <T>(() => (T)func());
     validated = true;
     return(this);
 }