2009-03-27 37 views

Trả lời

6

Đây là lớp đánh giá tinh xảo cung cấp cho bạn hàm JScript.NET Eval trong 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; 
     } 
    } 
} 

Bạn chỉ cần gọi Evaluator.EvaluateToBool (chuỗi). Được nâng cấp từ một dự án hiện có, vì vậy bạn có thể muốn tinh chỉnh!

+0

tuyệt vời này! PS EvaluateToBool sẽ có bool return – Maciej

+0

BTW Hiệu suất như thế nào? Bạn có nghĩ đây có phải là vấn đề không? – Maciej

+0

"Maciej (bình luận đầu tiên) - sao chép học sinh, dán và không sửa lỗi đúng. Sẽ chỉnh sửa. Cảm ơn. –

0

Phương pháp tiêu chuẩn là:

  • tokenize chuỗi
  • xây dựng một cây đặt toán tử trong mỗi nút và một toán hạng trong mỗi lá
  • quý khách đến thăm cây đánh giá biểu thức
2

Chuỗi bạn mô tả là mã C# hợp lệ, vì vậy nếu bạn có thể giải thích nó khi bạn chạy xong. Trong nhiều ngôn ngữ, đây là một hàm dựng sẵn. Trong C# không, nhưng bạn có thể sử dụng thư viện của bên thứ 3 chẳng hạn như runtime C# interpreter

+0

Thời gian chạy C# Expression Evaluator bạn đã đề cập trông thú vị nhưng liên kết tải xuống có vẻ bị hỏng - bạn có nguồn không? – Maciej

+0

Xin lỗi - bắt đầu làm việc hiện nay – Maciej

Các vấn đề liên quan