-3

is it possible to evaluate a single string in c#. The string itself will only be determined during run-time and therefore cannot be set before hand. please see example:

var a = "a == b";

if(a){
 //do something
}

EDITED:

This is a actual example of what i would like computed:

var evaluationToBeDone = "MUST_CE_I = \"MUST_CE_I\"";

if(evaluationToBeDone){
   // i will do something if the above is true
}
  • Possible duplicate of [How can I evaluate C# code dynamically?](https://stackoverflow.com/questions/4629/how-can-i-evaluate-c-sharp-code-dynamically) – orhtej2 Jul 16 '18 at 19:59
  • Possible duplicate of [How to execute code that is in a string?](https://stackoverflow.com/questions/4800267/how-to-execute-code-that-is-in-a-string) – Magnetron Jul 16 '18 at 20:01
  • 5
    Instead of telling us what you're doing, explain what you are trying to achieve because [what you are doing makes no sense at all](https://meta.stackexchange.com/questions/66377). – Dour High Arch Jul 16 '18 at 20:01
  • all i want to do is evaluate a string such as "a == a" inside a IF statement. the string itself will be determined during runtime and set before the IF statement. Only inbuilt methods can be used ( such as "System.Expressions" etc) does this explain it? – Wade Martin Jul 16 '18 at 20:04
  • 1
    It sounds like you might have an [XY Problem](http://xyproblem.info/). Instead of asking us about what you _think_ the solution is, describe your actual problem because its likely easily solvable another way. – maccettura Jul 16 '18 at 20:04
  • essentially i have a single string that needs to be evaluated by the contents of it. i can use expression parsing, lambda expression and any other tool i can find aslong as i can get a boolean answer of true or false based on the strings contents – Wade Martin Jul 16 '18 at 20:06
  • 1
    @WadeMartin ok, I see the update. One more thing I want to ask. What is controlling this input? _Where_ are you getting this type of input from? Is this something you can control? – maccettura Jul 16 '18 at 20:14
  • @maccettura Yes its something i control however it is string that is built during runtime for the evaluation to occur and thats why i am not able to use the conventional IF statement [ such as if(evaluationToBeDone == "something") ] – Wade Martin Jul 16 '18 at 20:16
  • 2
    @WadeMartin What does "built during runtime" mean. Please describe what this input represents and how it ultimately gets into this string. I really, truly believe you have an XY Problem here. – maccettura Jul 16 '18 at 20:18
  • Built during runtime means i have this initially: "<#Case.Customer#> = 'MUST_CE_I' " after which "<#Case.Customer#>" is something that is set during runtime from a collection(makes use of a expression parsing algorythm). the end result after expression parsing results in: "MUST_CE_I = \"MUST_CE_I\"". – Wade Martin Jul 16 '18 at 20:28
  • This new string now needs to be computed in order to achieve either a true or false. The reason i do not use a substring or a split to do so in a IF statment is because if in the future i decide to use integers in my evaluation such as: "<#Case.Customer#> > 2 " then i do not want to recreate my IF statment as i might still have cases where i use to = operand. I hope i explained this well enough – Wade Martin Jul 16 '18 at 20:28

1 Answers1

0

I see what you're trying to do, but the approach doesn't make sense. When you make a variable into an object, the program only reads it as letters, not any logic inside of the object. Try doing this:

var a = "MUST_CE_I"
var b = "\"MUST_CE_I\""

if (a == b)
{
   do stuff
}

I assume you want your second string to have the " " quotes, so this should give you what you need. Even though the if statement will always return false since the two variables are not equal.

DarthTommy
  • 419
  • 2
  • 10