Jangli Coder
Jangli Coder

Reputation: 99

String Convert To Ternary Operator

string str= "6 < 5 ? 1002 * 2.5: 6 < 10 ? 1002 * 3.5: 1002 * 4";
double Amount = str;

I want output : 3507

Upvotes: 1

Views: 361

Answers (1)

Szabolcs D&#233;zsi
Szabolcs D&#233;zsi

Reputation: 8843

This should solve it:

static void Main(string[] args)
{
    string str = "6 < 5 ? 1002 * 2.5: 6 < 10 ? 1002 * 3.5: 1002 * 4";

    Console.WriteLine(EvaluateExpression(str));
    Console.ReadKey();
}

public static object EvaluateExpression(string expression)
{
    var csc = new CSharpCodeProvider();
    var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" });
    parameters.GenerateExecutable = false;
    CompilerResults results = csc.CompileAssemblyFromSource(new CompilerParameters(new[] { "System.Core.dll" }),
        @"using System;

        class Program
        {
            public static object GetExpressionValue()
            {
                return " + expression + @";
            }
        }");

    if (results.Errors.Count == 0)
    {
        return results.CompiledAssembly.GetType("Program").GetMethod("GetExpressionValue").Invoke(null, null);
    }
    else
    {
        return string.Format("Error while evaluating expression: {0}", string.Join(Environment.NewLine, results.Errors.OfType<CompilerError>()));
    }
}

Although I'd suggest something less "brittle", I'm sure there are libraries that can help you.

Answer is based on this.

Upvotes: 5

Related Questions