-1

I need a way to check conditions set up in runtime.

I have classes that provide conditions created by themselves.

But I don't know how to do that.

At least I want something like that:

int x = 1;
int y = 2;
int z = 3;

string s = "(x == 1 && y == 3) || z == 3";
bool b = bool.Parse(s);

I already tried stating the conditions in a string and then convert them to a bool.

Mainly I tried using this

string s = "true || false";

and then

bool b = false;
bool.TryParse(s, out b);

or

bool b = Convert.ToBoolean(s);

The "true" or "false" statements in the string are put in later. The individual conditions are checked and being replaced with true or false.

Didn't work until now.

Edit: I am working on a Game and there are multiple objects that act according to their individual conditions. But these conditions are set up on runtime and their length and complexity is unknown before runtime. Using strings was my first attempt because i don't know how to do this. Also I know only a bit more than the basics so I don't know many methods and libraries.

  • 2
    *I have classes that provide conditions created by themselves*, use an interface – Liam Jan 09 '19 at 11:09
  • 3
    Why are the conditions being provided as a string? Are users typing in code that you are looking for evaluate? – David Jan 09 '19 at 11:10
  • Aye, why strings? Who will define those conditions? How exactly? If you want a direct answer, then you can compile and run your string using [CodeDOM](https://stackoverflow.com/q/826398/1997232). Imagine a string with placeholder like `"namespace a { class b { bool calculate() => return ... ; }}"` - put your condition string into `...`, compile, run and you will get a result. – Sinatr Jan 09 '19 at 11:16
  • Should have been a bit more precise. I am working on a Game and there are multiple objects acting depending on different conditions. But these conditions are set up on runtime and their length and complexity is unknown before runtime. Using strings was my first attempt because i don't know how to do this. – Christian Dieckmann Jan 09 '19 at 23:26

2 Answers2

0

You can try exploiting an old trick with DataTable:

  using System.Data;

  ... 

  private static bool Compute(string formula, int x, int y, int z) {
    using (var table = new DataTable()) {
      // variables of type int
      table.Columns.Add("x").DataType = typeof(int);
      table.Columns.Add("y").DataType = typeof(int);
      table.Columns.Add("z").DataType = typeof(int);

      table.Columns.Add("result").Expression = formula;

      table.Rows.Add(x, y, z);

      return Convert.ToBoolean(table.Rows[0]["result"]);
    }
  }

  ...

  // Please, not the syntax difference 
  Console.Write(Compute("(x = 1 and y = 3) or z = 3", 1, 2, 3));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You can use a C# script system like CS-Script; this allow you to execute C# as script (essentially strings). There is also the open source C# compiler Roslyn, but this may be too advanced for your needs.

Polyfun
  • 9,479
  • 4
  • 31
  • 39