samedi 16 octobre 2021

C# Parsing string conditionals/variables into code?

Attempting to parse various strings (user inputted) into conditionals using class variable names referenced inside the string itself. For example a user entered string can be "var1>10" or "var1>var2>10" and I would want the code to check the condition between the < and > operators for example:

if(var1>10) //for string "var1>10", where var1 is defined already in the class/code itself if(var1>var2 && var2>10) //for string "var1>var2>10"

Question 1: I figure I need to use reflection (access this.properties) or Dictionary<string key, decimal number> to store it as Dictionary<"var1", decimal> then access the Dictionary["var1"]?

Question 2: Is there a easier way to convert the strings "var1>10", "var1<var2<10", "var2<10", etc into code rather than manually parsing each section delimited by the < and > operators within the string (in my example there will be a maximum of one < and one > operator in the string and a maximum of 3 values (for example: "var1<var2<10" or "var2<var1<10", etc)... Not sure if regex string manipulation or some existing library already exists for this? Otherwise it seems like I will need a lot of nested if/else statements and ?: conditional operators.





Aucun commentaire:

Enregistrer un commentaire