Is there a method that will evaluate a string and produce an integer (assuming the string is an equation) in C#

后端 未结 3 1801
独厮守ぢ
独厮守ぢ 2020-12-22 08:28

Okay, I know that in Python, say you had a string called

strExpression and it\'s value was \"1+2-(3*5)\"

There is a python method (function, sorry I get the

相关标签:
3条回答
  • 2020-12-22 08:30

    Try this it uses CodeDOM to create a calculator

    0 讨论(0)
  • 2020-12-22 08:32

    There isn't exactly such a method in C#, but you can easily write one using the compiler at runtime to evaluate the expression. Check out CSharpCodeProvider.

    0 讨论(0)
  • 2020-12-22 08:57

    You can use the Microsoft.JScript which exposes all client side javascript functions in C#

    using System;
    using Microsoft.JScript;
    using Microsoft.JScript.Vsa;
    
    namespace JustTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                object value = Eval.JScriptEvaluate("1+2-(3*5)", VsaEngine.CreateEngine());
                Console.Write(value);
                Console.ReadLine();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题