Esempio n. 1
0
        /// <summary>
        /// 得到任务执行器
        /// </summary>
        /// <param name="taskType"></param>
        /// <returns></returns>
        public ISysTaskExecutor GetExecutor(string taskType)
        {
            taskType.CheckStringIsNullOrEmpty("taskType");

            TypeMappings.ContainsKey(taskType).FalseThrow("不能在配置信息sysTaskSettings/typeMappings中找到任务类型为{0}的实现类", taskType);

            return(TypeMappings[taskType].CreateInstance <ISysTaskExecutor>());
        }
 /// <summary>
 /// Factory method to get registered instance of type
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="constructorParameters"></param>
 /// <returns></returns>
 public T GetInstance <T>(params object[] constructorParameters)
 {
     if (TypeMappings.ContainsKey(typeof(T)))
     {
         var constructorTypes = constructorParameters.Select(x => x.GetType()).ToArray();
         var instance         = TypeMappings[typeof(T)].FirstOrDefault(x => x.ContainsConstructor(constructorTypes));
         if (instance == null)
         {
             throw new KeyNotFoundException($"Type {typeof(T).Name} has not been registered!");
         }
         return((T)IOCInstanceFactory.CreateInstance(instance.TargetType, constructorParameters));
     }
     throw new KeyNotFoundException($"Type {typeof(T).Name} has not been registered!");
 }
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var parameterExpression = node.GetParameterExpression();

            if (parameterExpression == null)
            {
                return(base.VisitMethodCall(node));
            }

            InfoDictionary.Add(parameterExpression, TypeMappings);

            var listOfArgumentsForNewMethod = node.Arguments.Aggregate(new List <Expression>(), (lst, next) =>
            {
                var mappedNext = ArgumentMapper.Create(this, next).MappedArgumentExpression;
                TypeMappings.AddTypeMapping(next.Type, mappedNext.Type);

                lst.Add(mappedNext);
                return(lst);
            });//Arguments could be expressions or other objects. e.g. s => s.UserId  or a string "ZZZ".  For extention methods node.Arguments[0] is usually the helper object itself

            //type args are the generic type args e.g. T1 and T2 MethodName<T1, T2>(method arguments);
            var typeArgsForNewMethod = node.Method.IsGenericMethod
                ? node.Method.GetGenericArguments().Select(i => TypeMappings.ContainsKey(i) ? TypeMappings[i] : i).ToList()//not converting the type it is not in the typeMappings dictionary
                : null;

            MethodCallExpression resultExp;

            if (!node.Method.IsStatic)
            {
                var instance = ArgumentMapper.Create(this, node.Object).MappedArgumentExpression;

                resultExp = node.Method.IsGenericMethod
                    ? Expression.Call(instance, node.Method.Name, typeArgsForNewMethod.ToArray(), listOfArgumentsForNewMethod.ToArray())
                    : Expression.Call(instance, node.Method, listOfArgumentsForNewMethod.ToArray());
            }
            else
            {
                resultExp = node.Method.IsGenericMethod
                    ? Expression.Call(node.Method.DeclaringType, node.Method.Name, typeArgsForNewMethod.ToArray(), listOfArgumentsForNewMethod.ToArray())
                    : Expression.Call(node.Method, listOfArgumentsForNewMethod.ToArray());
            }

            return(resultExp);
        }
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            var parameterExpression = node.GetParameterExpression();

            if (parameterExpression != null)
            {
                InfoDictionary.Add(parameterExpression, TypeMappings);
            }

            var listOfArgumentsForNewMethod = node.Arguments.Aggregate(new List <Expression>(), (lst, next) =>
            {
                var mappedNext = this.Visit(next);
                TypeMappings.AddTypeMapping(ConfigurationProvider, next.Type, mappedNext.Type);

                lst.Add(mappedNext);
                return(lst);
            });//Arguments could be expressions or other objects. e.g. s => s.UserId  or a string "ZZZ".  For extention methods node.Arguments[0] is usually the helper object itself

            //type args are the generic type args e.g. T1 and T2 MethodName<T1, T2>(method arguments);
            var typeArgsForNewMethod = node.Method.IsGenericMethod
                ? node.Method.GetGenericArguments().Select(i => TypeMappings.ContainsKey(i) ? TypeMappings[i] : i).ToList()//not converting the type it is not in the typeMappings dictionary
                : null;

            ConvertTypesIfNecessary(node.Method.GetParameters(), listOfArgumentsForNewMethod, node.Method);

            return(node.Method.IsStatic
                    ? GetStaticExpression()
                    : GetInstanceExpression(this.Visit(node.Object)));

            MethodCallExpression GetInstanceExpression(Expression instance)
            => node.Method.IsGenericMethod
                    ? Expression.Call(instance, node.Method.Name, typeArgsForNewMethod.ToArray(), listOfArgumentsForNewMethod.ToArray())
                    : Expression.Call(instance, node.Method, listOfArgumentsForNewMethod.ToArray());

            MethodCallExpression GetStaticExpression()
            => node.Method.IsGenericMethod
                    ? Expression.Call(node.Method.DeclaringType, node.Method.Name, typeArgsForNewMethod.ToArray(), listOfArgumentsForNewMethod.ToArray())
                    : Expression.Call(node.Method, listOfArgumentsForNewMethod.ToArray());
        }