///<summary>Set variables specified in the sv parameter, execute a certain action, then restore the set variables back.</summary> ///<param name="method">Delegate to execute</param> ///<param name="sv">Variables to set/restore before/after execution</param> ///<param name="prefix">Add a given prefix to the variable names.</param> ///<returns></returns> public object ExecuteWithVars(ScriptExecuteMethod method, Vars sv, string prefix) { var save = new Dictionary <string, object>(); var delete = new List <string>(); try { foreach (var v in sv) { string vn = prefix + v.Name; object o = GetOrDefault(vn, null); if (o == null && !IsSet(vn)) { delete.Add(v.Name); } else { save[v.Name] = o; } this[vn] = v.Value; } return(method()); } finally { foreach (var savePair in save) { string vn = prefix + savePair.Key; if (IsSet(vn)) { sv[savePair.Key] = Get(vn); } else { sv.Remove(savePair.Key); } this[vn] = savePair.Value; } foreach (var d in delete) { string vn = prefix + d; if (IsSet(vn)) { sv[d] = Get(vn); } else { sv.Remove(d); } Remove(vn); } } }
/// Execute the given script action on call stack protected object RunOnStack(ScriptOperation operation, IScriptAction action, ScriptExecuteMethod method) { if (action == null) { return(null); } using (var scope = new ScriptContextScope(this)) { _callStack.Push(operation, action); OnProgress(0); try { // one can put a breakpoint here to be called on every method call object ret = method(); OnProgressInternal(100, null); return(ret); } catch (ScriptTerminateException) { throw; } catch (ScriptExceptionWithStackTrace) { throw; } catch (ThreadAbortException) { _abortStarted = true; throw; } catch (Exception e) { throw new ScriptExceptionWithStackTrace(this, e); } finally { _callStack.Pop(); } } }