Dynamic Script Method/Functions

Good afternoon,

 

We are trying to tackle a problem wherein we have a table of requirements or criteria that we want to reference to determine eligibility for something else. In order to accomplish this we decided to have the table store two text values: one the script that we want to run for that specific requirement, and two the error response if the script element in question returns false.

 

In order to test and figure out how to implement we created a record in the table and stored a simple script as text on it:

 

var TestBool = false;
 
if(TestBool)
{
Set("Parameter1", true);
}
else
{
Set("Parameter1", false);
}

 

We can pass this text into a Script Element as a string - but have no idea how evaluate it as a method or function from that point (apologies if I am using the wrong terminology, low-code only so I'm stretching a bit here to try and accomplish this.)

 

Can anyone advise how I would take this string and evaluate/execute it as a script element?

 

Thanks so much for your help!

Like 0

Like

5 comments
Best reply

Hi Gary

 

You can do Dynamic Code Execution using Compiler Scripting:

https://itnext.io/getting-start-with-roslyn-c-scripting-api-d2ea10338d2b



I hope that helps!

 

Hi Gary

 

You can do Dynamic Code Execution using Compiler Scripting:

https://itnext.io/getting-start-with-roslyn-c-scripting-api-d2ea10338d2b



I hope that helps!

 

Mohamed Ouederni,

 

Thanks for this splendid tip! 

 

Mohamed Ouederni,

Thank you for the help, we will check out this article and see if we can put it into practice !

Mohamed Ouederni,

 

We have been trying to work out how to use this "Microsoft.CodeAnalysis.CSharp.Scripting" inside of the script element in the process editor, any pointers you could share?

Gary Amoe,



This article may help you with your implementation: 

https://joshvarty.com/2015/10/15/learn-roslyn-now-part-14-intro-to-the-…



You can use CSharpScript.RunAsync method to run the script and get variable values after the execution.

var state = CSharpScript.RunAsync(@"int x = 5; int y = 3; int z = x + y;""");
ScriptVariable x = state.Variables["x"];
ScriptVariable y = state.Variables["y"];
 
Console.Write($"{x.Name} : {x.Value} : {x.Type} "); // x : 5
Console.Write($"{y.Name} : {y.Value} : {y.Type} "); // y : 3

 

Show all comments