Hi,
(Without using test suite)
Is it possible to verify string expression passing as an input?
The issue is making this dynamic, gathering expression from file and x from app
eg.
input expression (string) e.g. “x>5 and x<=8”
input parameter x (string)
result (boolean)
ppr
(Peter Preuss)
February 24, 2022, 1:21pm
2
Vinncent_0:
x>5 and x<=8
we would modell as
CInt(x) > 5 and CInt(x) <= 8
A fully functionality like an EVAL() (javascript) we dont have Out-Of-The-Box
But lets loop in @StefanSchnell as maybe we can integrate an inline compilation / execution e.g. with Roslyn and build a EVAL feature by ourself
1 Like
Thanks for info Peter. It would be useful while getting an expression from an input source file. It looks like right now I can only hardcode it
@Vinncent_0
Hello Vinncent,
here an approach how you can handle your requirement via an Invoke Code activity.
Best regards
Stefan
//-Input Arguments------------------------------------------------------
string condition = "x > 5 && x <= 8";
string variable = "7";
//-Output Arguments-----------------------------------------------------
bool Result;
//-UiPath Invoke Code Activity Begin------------------------------------
string Code = @"
using System;
class Eval {{
public bool CheckCondition() {{
int x = {1};
if({0}) {{
return true;
}}
return false;
}}
}}";
//-Set condition to {0} and variable to {1}-----------------------------
string code = string.Format(Code, condition, variable);
CodeDomProvider CodeProvider = new CSharpCodeProvider();
CompilerResults CompResult = CodeProvider.CompileAssemblyFromSource(new CompilerParameters(), code);
object oInstance = CompResult.CompiledAssembly.CreateInstance("Eval", false, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, null, null, null, null);
MethodInfo MethInfo = oInstance.GetType().GetMethod("CheckCondition");
object oResult = MethInfo.Invoke(oInstance, null);
Result = Convert.ToBoolean(oResult);
//-UiPath Invoke Code Activity End--------------------------------------
Thank you for your engage. In the meantime I found a package from nuget.org source named “C# Eval Expression | Eval Function” which uses same approach.
system
(system)
Closed
March 3, 2022, 9:22am
6
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.