// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Reflection; using System.Text.RegularExpressions; using Microsoft.JScript; using Microsoft.JScript.Vsa; namespace E { public class Test { private static GlobalScope gs; static Test() { gs = VsaEngine.CreateEngineAndGetGlobalScope(false, new string[] { Assembly.GetExecutingAssembly().GetName().Name }); Import.JScriptImport("E", gs.engine); } public static string Evaluate(string s) { return Eval.JScriptEvaluate(s, gs.engine).ToString(); } public static string Address(int id) { return "DOTNET vej nr. " + id; } public static int BonusPercent(int salaryclass) { return (salaryclass - 5) / 2; } public static void Main(string[] args) { // simpelt Console.WriteLine(Evaluate("Test.Address(77)")); Console.WriteLine(Evaluate("Test.BonusPercent(18)")); // kombineret med regex string s = "bor ##Address(77)## og får ##BonusPercent(18)##% i bonus"; Console.WriteLine(s); string s2 = s; MatchCollection res = Regex.Matches(s,"(##)(.*?)(##)"); for(int i = 0; i < res.Count; i++) { s2 = s2.Replace(res[i].Groups[0].Value,Evaluate("Test." + res[i].Groups[2].Value)); } Console.WriteLine(s2); } } }