C# Can a comparison in a String value return a Boolean value. eg. “5 < 10” return true

后端 未结 6 1769
故里飘歌
故里飘歌 2020-12-22 14:11

Is there a way a comparison in a string value can return a Boolean value. Example. If (5 > 5000) would obviously return a false value. But what i wanted to do i

6条回答
  •  忘掉有多难
    2020-12-22 14:42

    Consider using FLEE:

    Flee is an expression parser and evaluator for the .NET framework. It allows you to compute the value of string expressions such as sqrt(a^2 + b^2) at runtime. It uses a custom compiler, strongly-typed expression language, and lightweight codegen to compile expressions directly to IL. This means that expression evaluation is extremely fast and efficient. Try out the demo, which lets you generate images based on expressions, and see for yourself.

    With FLEE you can easily accomplish this using something like:

            var con = new ExpressionContext();
            const string com = @"5 > 5000";
            var comparison = con.CompileDynamic(com);
            var result = comparison.Evaluate();
            MessageBox.Show(result.ToString());
    

    HTH...

提交回复
热议问题