本文介绍了将复杂的布尔条件的字符串在.NET中为bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析复杂的EX presion公司从字符串为BOOL。

I need to parse complex expresion from string to bool.

它只能包含:
*布尔值(真/假)
*括号中,
* AND / OR操作数(安培;&安培;,||)

It can only contain:
* boolean values (true/false),
* parenthesis,
* AND/OR operands (&&, ||)

例如:

bool.Parse("((true || false) && (false || false)) || (true || false)"

不知道如何实现这一目标?

Any idea how to achieve this?

推荐答案

下面是一个狡猾的评估类,让你在C#code中的JScript.NET eval函数:

Here's a cunning evaluator class that gives you the JScript.NET Eval function within C# code:

static public class Evaluator
{
    private const string _jscriptSource =
        @"package Evaluator
        {
           class Evaluator
           {
              public function Eval(expr : String) : String
              {
                 return eval(expr);
              }
           }
        }";

    static private object _evaluator;
    static private Type _evaluatorType;

    [SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline",
        Justification = "Can't be done inline - too complex")]
    static Evaluator()
    {
        InstantiateInternalEvaluator();
    }

    static private void InstantiateInternalEvaluator()
    {
        JScriptCodeProvider compiler = new JScriptCodeProvider();

        CompilerParameters parameters;
        parameters = new CompilerParameters();
        parameters.GenerateInMemory = true;

        CompilerResults results;
        results = compiler.CompileAssemblyFromSource(parameters, _jscriptSource);

        Assembly assembly = results.CompiledAssembly;
        _evaluatorType = assembly.GetType("Evaluator.Evaluator");

        _evaluator = Activator.CreateInstance(_evaluatorType);
    }

    static public int EvaluateToInteger(string statement)
    {
        string s = EvaluateToString(statement);
        return int.Parse(s);
    }

    static public double EvaluateToDouble(string statement)
    {
        string s = EvaluateToString(statement);
        return double.Parse(s);
    }

    static public decimal ForceEvaluateToDecimal(string statement)
    {
        decimal result;
        bool s = Decimal.TryParse(statement, out result);
        return result;
    }

    static public decimal EvaluateToDecimal(string statement)
    {
        string s = EvaluateToString(statement);
        return decimal.Parse(s);
    }

    static public string EvaluateToString(string statement)
    {
        object o = EvaluateToObject(statement);
        return o.ToString();
    }

    static public bool EvaluateToBool(string statement)
    {
        object o = EvaluateToObject(statement);
        return (bool)o;
    }

    static public object EvaluateToObject(string statement)
    {
        try
        {
            return _evaluatorType.InvokeMember(
                "Eval",
                BindingFlags.InvokeMethod,
                null,
                _evaluator,
                new object[] {statement}
                );
        }
        catch (Exception)
        {
            InstantiateInternalEvaluator();
            return null;
        }
    }
}

您只需再调用Evaluator.EvaluateToBool(串)。从现有项目解除,所以你可能需要调整!

You just then call Evaluator.EvaluateToBool(string). Lifted from an existing project, so you may want to tweak!

这篇关于将复杂的布尔条件的字符串在.NET中为bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 05:01