EDN Admin
Well-known member
Hello,
Im having difficultly trying to creating a simple dynamic expression evaluator type.
I have the following simple expression of code (the green part is stored in a database as a string):
<span style="color:rgb(0,0,255) namespace MyCompany.Utility
<span style="color:rgb(0,0,255) {<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) using System;<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) using Symyx.Framework.Vault;<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) class ExpressionEval : MarshalByRefObject<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) {<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) public static bool Evaluate(VaultObject candidate)<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) { return <span style="color:rgb(0,128,0) candidate.Format.StartsWith("MyCompany");<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) }<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) }<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) }
With the C# dynamic compiler, i compile it:
<span style="color:rgb(0,0,255) CSharpCodeProvider myCodeProvider = new CSharpCodeProvider();<br style="color:rgb(0,0,255)
<div style="margin-left:40px <span style="color:rgb(0,0,255) CompilerResults cr = myCodeProvider.CompileAssemblyFromSource(cp, codeToCompile);
It compiles fine, but when i try to extract the methodinfo (to call later for expression evaluation),
<span style="color:rgb(0,0,255) MethodInfo methodInfo =<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) cr.CompiledAssembly.GetType("Symyx.Workflow.ExpressionEval").GetMethod("Evaluate");
Problem: It (most of the time) throws a "ThreadAbortException"
<span style="color:rgb(255,0,0) "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack"
I wrote on this in Sept, but didnt figure it out. Since then, on this attempt, i created a separate app domain to do the compilation, but still get the same (occasional) ThreadAbortException.
Anyone? Alternatively...does anyone know a simple (no strings attached) library to compile sniplets of code like this in order to evaluate a simple expression? in the code above, i compile it and grab the method info to call, passing in a VaultObject (our type) to evaluate.
View the full article
Im having difficultly trying to creating a simple dynamic expression evaluator type.
I have the following simple expression of code (the green part is stored in a database as a string):
<span style="color:rgb(0,0,255) namespace MyCompany.Utility
<span style="color:rgb(0,0,255) {<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) using System;<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) using Symyx.Framework.Vault;<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) class ExpressionEval : MarshalByRefObject<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) {<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) public static bool Evaluate(VaultObject candidate)<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) { return <span style="color:rgb(0,128,0) candidate.Format.StartsWith("MyCompany");<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) }<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) }<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) }
With the C# dynamic compiler, i compile it:
<span style="color:rgb(0,0,255) CSharpCodeProvider myCodeProvider = new CSharpCodeProvider();<br style="color:rgb(0,0,255)
<div style="margin-left:40px <span style="color:rgb(0,0,255) CompilerResults cr = myCodeProvider.CompileAssemblyFromSource(cp, codeToCompile);
It compiles fine, but when i try to extract the methodinfo (to call later for expression evaluation),
<span style="color:rgb(0,0,255) MethodInfo methodInfo =<br style="color:rgb(0,0,255) <span style="color:rgb(0,0,255) cr.CompiledAssembly.GetType("Symyx.Workflow.ExpressionEval").GetMethod("Evaluate");
Problem: It (most of the time) throws a "ThreadAbortException"
<span style="color:rgb(255,0,0) "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack"
I wrote on this in Sept, but didnt figure it out. Since then, on this attempt, i created a separate app domain to do the compilation, but still get the same (occasional) ThreadAbortException.
Anyone? Alternatively...does anyone know a simple (no strings attached) library to compile sniplets of code like this in order to evaluate a simple expression? in the code above, i compile it and grab the method info to call, passing in a VaultObject (our type) to evaluate.
View the full article