예제 #1
0
        /// <summary>
        /// 构建实例对象
        /// </summary>
        protected object InvokeInScopeContructor(ModuleRunScope scope)
        {
            object instance    = null;
            Type   taregetType = TypeCache.GetRuntimeType(Type);
            ModuleConstructorElement constr = Steps.FirstOrDefault(s => s.GetType().Equals(typeof(ModuleConstructorElement))) as ModuleConstructorElement;

            #region 构建对象
            if (constr == null)
            {
                instance = Activator.CreateInstance(taregetType);
            }
            else
            {
                if (scope != null)
                {
                    scope.AddStackFrame(constr.ToString());
                }
                constr.InvokeInScope(taregetType, null, scope);
                instance = scope.StepSwap;
            }

            if (instance == null)
            {
                throw new System.Configuration.ConfigurationErrorsException("创建实例:(" + taregetType.FullName + ")失败,请确保系统配置正确!");
            }
            #endregion

            return(instance);
        }
        /// <summary>
        /// Invokes the builder in scope.
        /// </summary>
        /// <param name="sub">The sub.</param>
        /// <param name="scope">执行作用域</param>
        /// <param name="ignoreException">是否忽略执行异常</param>
        /// <returns></returns>
        protected static object InvokeBuilderInScope(SubModuleBuildElement sub, ModuleRunScope scope, bool ignoreException = false)
        {
            Type taregetType = TypeCache.GetRuntimeType(sub.Type);
            ModuleConstructorElement constr = sub.Steps.FirstOrDefault(s => s.GetType().Equals(typeof(ModuleConstructorElement))) as ModuleConstructorElement;
            object instance = null;

            #region 构建对象
            if (constr == null)
            {
                if (sub.BuildInstance)
                {
                    //if (taregetType.IsValueType)
                    if (taregetType == typeof(string))
                    {
                        instance = string.Empty;
                    }
                    else
                    {
                        instance = Activator.CreateInstance(taregetType);
                    }
                }
                else
                {
                    instance = default(Target);
                }
            }
            else
            {
                if (scope != null)
                {
                    scope.AddStackFrame(constr.ToString());
                }
                constr.InvokeInScope(taregetType, null, scope);
                instance = scope.StepSwap;
            }

            if (sub.BuildInstance && instance == null)
            {
                throw new ConfigurationErrorsException("创建实例:(" + taregetType.FullName + ")失败,请确保系统配置正确!");
            }

            #endregion

            try
            {
                foreach (var step in sub.Steps)
                {
                    if (scope != null)
                    {
                        scope.AddStackFrame(step.ToString());
                    }
                    step.InvokeInScope(taregetType, instance, scope);
                }
            }
            catch (Exception ivkError)
            {
                scope.LastError = ivkError;
                if (!ignoreException)
                {
                    throw ivkError;
                }
            }

            if (sub.Target == BuildTarget.Instance)
            {
                return(instance);
            }
            else
            {
                return(scope.StepSwap);
            }
        }