public object Execute(AutoAttributeContext context) { string classScript = ""; if (_parser.NextToken() != TokenType.Eof) { if (_parser.Token == TokenType.Comment || _parser.Token == TokenType.LineComment) { _parser.SkipComments(); } if (_parser.Token == TokenType.Symbol && _parser.TokenSymbol == "{") { classScript = DefaultUsings + " class Expression { \n " + " public static object Execute(AutoAttributeContext context) " + PreparedScript + " } "; } } Console.WriteLine(classScript); var assembly = CSScript.LoadCode(classScript); MethodDelegate method = assembly.GetStaticMethod("*.Execute", context); return(method(context)); }
public virtual bool HasConditionScript() { if (String.IsNullOrEmpty(Condition)) { return(false); } var parser = new CsParser(Condition); while (parser.NextToken() != TokenType.Eof) { if (parser.Token != TokenType.Comment && parser.Token != TokenType.LineComment && parser.Token != TokenType.Eof) { return(true); } } return(false); }
public static void LoadScript(string script) { if (String.IsNullOrEmpty(script)) { return; } var parser = new CsParser(script); while (parser.NextToken() != TokenType.Eof) { if (parser.Token == TokenType.Comment || parser.Token == TokenType.LineComment) { parser.SkipComments(); } else if (parser.Token != TokenType.Eof) { var classScript = DefaultUsings + "\n" + script; /* ObjectCacheItem<string, Assembly> cached; * // lock (ScriptLoadLock) * cached = ScriptCache.Find(classScript); * * if (cached == null) * { * //lock (ScriptLoadLock) * { * var assembly = CSScript.LoadCode(classScript); * ScriptCache.Add(assembly, classScript); * } * }*/ // 09-02-17 /*ScriptCache.GetOrAdd(classScript, s => * new Lazy<Assembly>(() => CSScript.LoadCode(s)));*/ // 10-02-17 ScriptCacheLock.AcquireReaderLock(LockTimeout); try { var cached = ScriptCache.Find(classScript); if (cached == null) { var lc = ScriptCacheLock.UpgradeToWriterLock(LockTimeout); try { cached = ScriptCache.Find(classScript); if (cached != null) { return; } var assembly = CSScript.LoadCode(classScript); ScriptCache.Add(assembly, classScript); } finally { ScriptCacheLock.DowngradeFromWriterLock(ref lc); } } return; } finally { ScriptCacheLock.ReleaseReaderLock(); } } } }
public Assembly Compile(string resultType) { //TODO: дописать различные виды вызова скрипта в зависимости от типа // try // { string classScript = ""; /* * switch (ScriptType) * { * case ScriptType.Class: * classScript = DefaultUsings + PreparedScript; * break; * * case ScriptType.Function: * classScript = DefaultUsings + * " class Expression { \n " + * " public static " + resultType + " Execute(WorkflowContext context) " + * PreparedScript + * " } "; * break; * * case ScriptType.Expression: * classScript = DefaultUsings + * " class Expression { \n " + * " public static " + resultType + " Execute(WorkflowContext context) { " + * " return " + PreparedScript + " ; " + * " } " + * " } "; * break; * }*/ if (_parser.NextToken() != TokenType.Eof) { if (_parser.Token == TokenType.Comment || _parser.Token == TokenType.LineComment) { _parser.SkipComments(); } if (_parser.Token == TokenType.Symbol && _parser.TokenSymbol == "{") { classScript = DefaultUsings + " class Expression { \n " + " public static " + resultType + " Execute(WorkflowContext context) " + PreparedScript + " } "; } else if (_parser.Token == TokenType.Symbol || _parser.TokenSymbol == "(") { classScript = DefaultUsings + " class Expression { \n " + " public static " + resultType + " Execute(WorkflowContext context) { " + " return " + PreparedScript + " ; " + " } " + " } "; } else { classScript = DefaultUsings + PreparedScript; } } else { classScript = DefaultUsings + " class Expression { \n " + " public static " + resultType + " Execute(WorkflowContext context) {" + PreparedScript + " }} "; } /*ObjectCacheItem<string, Assembly> cached; * // lock (ScriptLoadLock) * cached = ScriptCache.Find(classScript); * * if (cached != null) return cached.CachedObject;*/// 09-02-17 //lock (ScriptLoadLock) // 09-02-17 /*return ScriptCache.GetOrAdd(classScript, s => * new Lazy<Assembly>(() => * { * CSScript.AssemblyResolvingEnabled = true; * //Evaluator.ReferenceAssembliesFromCode(code); * return CSScript.LoadCode(s); * //ScriptCache.Add(assembly, classScript); * * //return assembly; * })).Value;*/// 10-02-17 ScriptCacheLock.AcquireReaderLock(LockTimeout); try { var cached = ScriptCache.Find(classScript); if (cached != null) { return(cached.CachedObject); } var lc = ScriptCacheLock.UpgradeToWriterLock(LockTimeout); try { cached = ScriptCache.Find(classScript); if (cached != null) { return(cached.CachedObject); } CSScript.AssemblyResolvingEnabled = true; //Evaluator.ReferenceAssembliesFromCode(code); var assembly = CSScript.LoadCode(classScript); ScriptCache.Add(assembly, classScript); return(assembly); } finally { ScriptCacheLock.DowngradeFromWriterLock(ref lc); } } finally { ScriptCacheLock.ReleaseReaderLock(); } }