/// <summary> /// 执行模块语句 /// </summary> /// <param name="scope">The scope.</param> /// <param name="modules">The modules.</param> public static void RunScopeModule(ModuleRunScope scope, params ModuleBlock[] modules) { foreach (var sub in modules) { sub.InvokeInScope(scope); } }
/// <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> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> /// <returns>是否中途中止进行</returns> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { if (Conditions != null) { ModuleWhen whenMatched = null; for (int i = 0, j = Conditions.Length; i < j; i++) { if (Conditions[i].Match.CanRunInScope(scope)) { whenMatched = Conditions[i]; break; } } if (whenMatched != null) { return(whenMatched.InvokeInScope(instanceType, instance, scope)); } else { if (Else != null) { return(Else.InvokeInScope(instanceType, instance, scope)); } } } return(false); }
/// <summary> /// 执行步骤(包含是否中途退出) /// </summary> /// <param name="taregetType">目标类型</param> /// <param name="instance">实例</param> /// <param name="scope">执行区间</param> /// <param name="mySteps">创建步骤</param> /// <returns></returns> protected virtual bool InvokeStepsInScope(Type taregetType, object instance, ModuleRunScope scope, params ModuleBuildStepElement[] mySteps) { bool returnResult = false; try { foreach (var step in mySteps) { if (scope != null) { scope.AddStackFrame(step.ToString()); } returnResult = step.InvokeInScope(taregetType, instance, scope); if (returnResult) { break; } } } catch (Exception ivkError) { scope.LastError = ivkError; } return(returnResult); }
/// <summary> /// 当前条件是否通过 /// </summary> /// <param name="scope">作用域</param> /// <returns> /// <c>true</c> if the specified scope is passed; otherwise, <c>false</c>. /// </returns> public virtual bool IsPassed(ModuleRunScope scope) { if (ExpressionLeft != null && ExpressionRight != null) { string result = ExpressionLeft.Invoke(scope); string resultRight = ExpressionRight.Invoke(scope); if (!string.IsNullOrEmpty(BooleanCompareDelegate)) { return(ConditionalElement.GetBooleanDelegate(BooleanCompareDelegate)(result, resultRight)); } else { return(string.Equals(result, resultRight, ComparisonMode)); } } object boolKey = scope.GetVaraible(Key); if (boolKey == null) { return(false); } else { return(Convert.ToBoolean(boolKey)); } }
/// <summary> /// 判断是否能在当前作用域下运行 /// </summary> /// <param name="scope">作用域</param> /// <returns></returns> public bool CanRunInScope(ModuleRunScope scope) { bool result = false; foreach (IConditionalItem item in condItemList) { switch (item.Logic) { case LogicExpression.None: result = item.IsPassed(scope); break; case LogicExpression.AND: result = result && item.IsPassed(scope); break; case LogicExpression.OR: result = result || item.IsPassed(scope); break; case LogicExpression.AndNot: result = result && !item.IsPassed(scope); break; case LogicExpression.OrNot: result = result || !item.IsPassed(scope); break; default: break; } } return(result); }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { if (!string.IsNullOrEmpty(StaticType)) { instanceType = TypeCache.GetRuntimeType(StaticType); instance = null; } Type[] argTypes = new Type[0]; if (Parameters != null) { argTypes = Parameters.GetArgumentTypes(); } MethodInfo fun = instanceType.GetMethod(MethodName, argTypes); if (fun == null) { throw new System.Configuration.ConfigurationErrorsException("方法[" + MethodName + "]在类型(" + instanceType.FullName + ")中未找到,请确保系统配置正确!"); } else { object[] args = (Parameters == null) ? null : Parameters.GetArguments(scope); object ret = fun.Invoke(instance, args); scope.StepSwap = ret; } return(false); }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { PropertyInfo pInfo = instanceType.GetProperty(Name, BindingFlags.Public | BindingFlags.Instance); if (pInfo == null) { throw new System.Configuration.ConfigurationErrorsException("属性[" + Name + "]在类型(" + instanceType.FullName + ")中未找到,请确保系统配置正确!"); } else { object targetVal = Value; //if (string.IsNullOrEmpty(Value)) //{ //} int idx = Value.IndexOf('$'); if (idx != -1) { if (Value[idx + 1] != '$') { string str = "\\$([a-zA-z_][a-zA-z_0-9]*)"; string varName = Regex.Match(Value, str).Groups[1].Value; targetVal = scope.GetVaraible(varName); } } pInfo.SetValue(instance, targetVal, null); } return(false); }
/// <summary> /// 实现代码逻辑 /// </summary> /// <param name="scope">The scope.</param> public override VOID Invoke(ModuleRunScope scope) { if (Builder == null) { throw new ConfigurationErrorsException("请先设置构建器Builder:(" + typeof(SubModuleBuildElement).FullName + ")!"); } try { InvokeBuilderInScope(Builder, scope, false); } catch (Exception err) { if (Catch != null) { string delegateStr = Catch.Value.ToString(); Action <Exception> handler = delegateStr.CreateFromConfig <Action <Exception> >(); if (handler != null) { handler(err); } } } finally { if (Finally != null) { InvokeBuilderInScope(Finally, scope, false); } } return(VOID.Empty); }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { if (!string.IsNullOrEmpty(StaticType)) { instanceType = TypeCache.GetRuntimeType(StaticType); Flags |= BindingFlags.Static; instance = null; } FieldInfo mInfo = instanceType.GetField(Name, Flags); if (mInfo == null) { throw new System.Configuration.ConfigurationErrorsException("字段[" + Name + "]在类型(" + instanceType.FullName + ")中未找到,请确保系统配置(flags)正确!"); } else { if ((Flags & BindingFlags.SetField) == BindingFlags.SetField) { mInfo.SetValue(instance, ValueType.GetObjectValue()); } if ((Flags & BindingFlags.GetField) == BindingFlags.GetField) { scope.StepSwap = mInfo.GetValue(instance); } } return(false); }
/// <summary> /// 根据配置执行循环逻辑 /// </summary> /// <exception cref="System.Configuration.ConfigurationErrorsException">模块类型配置错误,至少需要有while满足的条件!</exception> public override void InvokeInScope(ModuleRunScope scope) { if (While == null) { throw new System.Configuration.ConfigurationErrorsException("模块类型(" + typeof(ModuleDoWhile).FullName + ")配置错误,至少需要有while满足的条件!"); } if (RunMode == DoWhileMode.While) { while (While.Match.CanRunInScope(scope)) { While.InvokeInScope(scope); } } else if (RunMode == DoWhileMode.DoWhile) { do { Do.InvokeInScope(scope); }while (While.Match.CanRunInScope(scope)); } else if (RunMode == DoWhileMode.WhileDo) { while (While.Match.CanRunInScope(scope)) { Do.InvokeInScope(scope); } } }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { if (scope != null) { scope.StepSwap = Activator.CreateInstance(instanceType, GetArguments()); } return(false); }
/// <summary> /// 获取运行域内的值 /// </summary> /// <param name="scope"></param> /// <returns></returns> public override object GetObjectValue(ModuleRunScope scope = null) { Type targetType = TypeCache.GetRuntimeType(Type); foreach (var step in Steps) { step.InvokeInScope(targetType, null, scope); } return(scope.StepSwap); }
public string Invoke(ModuleRunScope scope) { if (Value.IndexOf('$') != -1) { if (Value == "$StepSwap") { return(scope.StepSwap.ToString()); } } return(Value); }
/// <summary> /// 当前条件是否通过 /// </summary> /// <param name="scope">作用域</param> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public bool IsPassed(ModuleRunScope scope) { object cmp = scope.GetVaraible(ScopeInstanceKey); if (cmp == null) { throw new System.Configuration.ConfigurationErrorsException("作用域变量[" + ScopeInstanceKey + "]未找到,请确保系统配置正确!"); } Type cmpType = TypeCache.GetRuntimeType(CompareType); return(cmp.GetType().Equals(cmpType)); }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> /// <returns>是否中途中止进行</returns> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { object enumeratorObj = scope.GetVaraible(ScopeIEnumerableInstance); if (enumeratorObj == null) { throw new System.Configuration.ConfigurationErrorsException("作用域变量[" + ScopeIEnumerableInstance + "]未找到,请确保系统配置正确!"); } Type objType = enumeratorObj.GetType(); System.Collections.IEnumerator enumerator = null; if (objType.IsArray) { Array arr = enumeratorObj as Array; enumerator = arr.GetEnumerator(); } else { MethodInfo mInfo = objType.GetMethod("GetEnumerator", BindingFlags.InvokeMethod | BindingFlags.Instance); if (mInfo == null) { throw new System.Configuration.ConfigurationErrorsException("作用域变量[" + ScopeIEnumerableInstance + "]没有实现GetEnumerator()的方法,不是一个可枚举的实例类型,请确保系统配置正确!"); } enumerator = mInfo.Invoke(enumeratorObj, null) as System.Collections.IEnumerator; } if (enumerator == null) { throw new System.Configuration.ConfigurationErrorsException("系统传参错误,作用域变量[" + ScopeIEnumerableInstance + "]不是一个可枚举的实例类型,请确保系统配置正确!"); } bool stopInvoke = false; using (ModuleRunScope nScope = new ModuleRunScope(scope)) { while (enumerator.MoveNext()) { nScope.StepSwap = enumerator.Current; if (Match.CanRunInScope(nScope)) { bool exit = InvokeStepsInScope(instanceType, instance, nScope, base.Steps); if (exit) { stopInvoke = true; break; } } } } return(stopInvoke); }
public object[] GetArguments(ModuleRunScope scope = null) { List <object> args = new List <object>(); if (Arguments != null && Arguments.Any()) { foreach (var parm in Arguments) { args.Add(parm.GetObjectValue(scope)); } } return(args.ToArray()); }
/// <summary> /// 在作用域下单独执行 /// </summary> /// <param name="scope"></param> /// <exception cref="System.Configuration.ConfigurationErrorsException">系统配置错误</exception> public override void InvokeInScope(ModuleRunScope scope) { if (BuildInstance) { Type taregetType = TypeCache.GetRuntimeType(Type); object instance = InvokeInScopeContructor(scope); InvokeInScope(taregetType, instance, scope); } else { object instance = scope.StepSwap; Type taregetType = instance != null?instance.GetType() : TypeCache.GetRuntimeType(Type); InvokeInScope(taregetType, instance, scope); } }
public virtual Target Invoke(ModuleRunScope scope) { if (Builder == null) { throw new ConfigurationErrorsException("请先设置构建器Builder:(" + typeof(SubModuleBuildElement).FullName + ")!"); } object val = InvokeBuilderInScope(Builder, scope, false); if (Builder.Target == BuildTarget.Instance) { return((Target)val); } else { return((Target)val); } }
/// <summary> /// 当前条件是否通过 /// </summary> /// <param name="scope">作用域</param> /// <returns> /// <c>true</c> if the specified scope is passed; otherwise, <c>false</c>. /// </returns> public virtual bool IsPassed(ModuleRunScope scope) { if (Expression != null) { bool result = Expression.Invoke(scope); return(result); } object boolKey = scope.GetVaraible(Key); if (boolKey == null) { return(false); } else { return(Convert.ToBoolean(boolKey)); } }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> /// <returns> /// 是否中途中止进行 /// </returns> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { object targetInstance = null; switch (OriginType) { case InstanceOrigin.Context: targetInstance = scope.StepSwap; break; case InstanceOrigin.Arguments: targetInstance = instance; break; case InstanceOrigin.BuildInType: targetInstance = Activator.CreateInstance(instanceType); break; default: break; } instanceType = targetInstance.GetType(); Type delegateType = TypeCache.GetRuntimeType(MethodDelegatePattern); MethodInfo mInfo = instanceType.GetMethod(MethodName, BindingFlags.Instance | BindingFlags.Public); if (mInfo == null) { throw new System.Configuration.ConfigurationErrorsException("函数[" + MethodName + "]在类型(" + instanceType.FullName + ")中未找到,请确保系统配置正确!"); } Delegate target = Delegate.CreateDelegate(delegateType, targetInstance, mInfo, true); //委托绑定到实例方法 scope.StepSwap = target; return(false); }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { bool stepResult = InvokeStepsInScope(instanceType, instance, scope, Steps); return(stepResult); }
/// <summary> /// 在模块执行作用域执行(调用)模块代码 /// </summary> /// <param name="sub">构建配置项</param> /// <param name="scope">执行作用域</param> public static void BuildAndInvoke(SubModuleBuildElement sub, ModuleRunScope scope) { sub.InvokeInScope(scope); }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> /// <returns>是否中途中止进行</returns> public abstract bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null);
/// <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); } }
/// <summary> /// 获取运行域内的值 /// </summary> /// <returns></returns> public virtual object GetObjectValue(ModuleRunScope scope = null) { if (Value == null) { return(null); } Type targetType = TypeCache.GetRuntimeType(Type); #region 如果是静态委托设置 if (targetType.IsSubclassOf(typeof(Delegate))) { string delegateStr = Value.ToString(); int idx = delegateStr.LastIndexOf('.'); int idxSep = delegateStr.IndexOf(','); if (idxSep != -1 && idx > idxSep) //程序集有字段分隔符号. { idx = delegateStr.Substring(0, idxSep).LastIndexOf('.'); } string setTypeStr = delegateStr.Substring(0, idx); string methodName = delegateStr.Substring(idx + 1); if (idxSep != -1) { setTypeStr += delegateStr.Substring(idxSep); methodName = delegateStr.Substring(idx + 1, idxSep - idx - 1).Trim(); } Type setType = TypeCache.GetRuntimeType(setTypeStr); if (StaticField) { FieldInfo mInfo = setType.GetField(methodName, BindingFlags.Public | BindingFlags.Static); if (mInfo == null) { throw new System.Configuration.ConfigurationErrorsException("字段[" + methodName + "]在类型(" + setType.FullName + ")中未找到,请确保系统配置正确!"); } else { return(mInfo.GetValue(null)); } } else { MethodInfo mInfo = setType.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static); if (mInfo == null) { throw new System.Configuration.ConfigurationErrorsException("函数[" + methodName + "]在类型(" + setType.FullName + ")中未找到,请确保委托配置正确!"); } else { return(Delegate.CreateDelegate(targetType, mInfo, true)); } } } #endregion if (TypeCache.IsBasicType(targetType)) { RuntimeValue = Value; FoundValueInScope: if (targetType == typeof(Type)) { return(TypeCache.GetRuntimeType(Value.ToString())); } if (RuntimeValue != null && RuntimeValue.GetType() == typeof(string)) { string valDef = RuntimeValue.ToString(); if (valDef.IndexOf('$') != -1 && scope != null) { if (valDef == "$StepSwap") { RuntimeValue = scope.StepSwap; } goto FoundValueInScope; } else { if (targetType == typeof(string)) { return(valDef); } } } return(Convert.ChangeType(RuntimeValue, targetType)); } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="ModuleRunScope"/> class. /// </summary> /// <param name="parent">The parent.</param> public ModuleRunScope(ModuleRunScope parent) { Parent = parent; }
/// <summary> /// 在作用域下执行 /// </summary> /// <param name="instanceType">实例类型</param> /// <param name="instance">The instance.</param> /// <param name="scope">执行代码作用区间</param> /// <returns> /// 是否中途中止进行 /// </returns> public override bool InvokeInScope(Type instanceType, object instance, ModuleRunScope scope = null) { return(true); }