///<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); } } }
private void setCaptureVars(Regex regex, Match match, Vars variables) { string pref = Context.TransformStr(Name, Transform); for (int j = 0; j < match.Groups.Count; ++j) { Group g = match.Groups[j]; string name = regex.GroupNameFromNumber(j); if (name.Length > 0 && char.IsDigit(name[0])) { name = "_" + name; } if (!string.IsNullOrEmpty(pref)) { name = pref + name; } if (g.Success) { variables[name] = g.Value; } else { variables.Remove(name); } } }