2009-08-14 43 views
5

Tôi muốn sử dụng biểu thức LINQ cho một số tính năng động. Tôi cần Và, Hoặc và Không biểu thức .. Tôi không thể nhận được nhiều ..C# Biểu thức sử dụng và biểu thức Không và Không cùng nhau dựa trên AST

Chúng tôi muốn kiểm tra xem tính năng nào đó đã được bật hay không trong hệ thống của chúng tôi và dựa trên đó chúng tôi sẽ quyết định có hiển thị mục menu hay không hay không. chúng tôi đã hình thành các quy tắc ở định dạng XML, tôi biết để chuyển đổi quy tắc sang AST nhưng tôi không biết ánh xạ tới biểu thức LINQ.

Rules Are như: Feature1Enabled Và Feature2Eenabled hoặc (Feature3Disabled Và Không Feature5Enabled)

Here "Feature1Enabled", "Feature2Eenabled" vv là tên của tính năng này. Chúng tôi sẽ chuyển chuỗi này tới hàm IsFeatureEnabled để kiểm tra xem một tính năng đã được bật hay chưa.

public delegate bool IsEnabledDelegate(string name, string value); 

    public static bool IsFeatureEnabled(string name, string value) 
    { 
     if (name == "MV") 
      return true; 

     if (name == "GAS" && value == "G") 
      return true; 
     //More conditions goes here 
     return false; 
    } 

    static void Main(string[] args) 
    { 
     Expression<Func<string, string, bool>> featureEnabledExpTree = 
         (name, value) => IsFeatureEnabled(name, value); 

     //I want the equal of the following statement in C# Linq Expression 

     bool result = IsFeatureEnabled("MV", "") && IsFeatureEnabled("GAS", "G") || !IsFEatureEnabled("GAS", "F") 

    } 

Tôi muốn tương đương với kết quả bool = IsFeatureEnabled ("MV", "") & & IsFeatureEnabled ("GAS", "G") || ! IsFEatureEnabled ("GAS", "F")

trong LINQ Format biểu .. Tuy nhiên tôi có thể chuyển đổi chúng để tự động dựa trên ký hiệu AST của tôi ..

Cảm ơn bạn rất nhiều .. Nếu bạn cần thêm thông tin , hãy cho tôi biết trong các nhận xét ..

+0

(ví dụ sửa đổi để hiển thị xây dựng featureEnabledExpTree trong thời gian chạy dựa trên dữ liệu) –

Trả lời

4
ParameterExpression name = Expression.Parameter(typeof(string), "name"), 
     value = Expression.Parameter(typeof(string), "value"); 

    // build in reverse 
    Expression body = Expression.Constant(false); 
    body = Expression.Condition(
     Expression.AndAlso(
      Expression.Equal(name, Expression.Constant("GAS")), 
      Expression.Equal(value, Expression.Constant("G")) 
     ), Expression.Constant(true), body); 
    body = Expression.Condition(
     Expression.Equal(name, Expression.Constant("MV")), 
     Expression.Constant(true), body); 

    Expression<Func<string, string, bool>> featureEnabledExpTree = 
     Expression.Lambda<Func<string, string, bool>>(body, name, value); 


    // test in isolation 
    var featureEnabledFunc = featureEnabledExpTree.Compile(); 
    bool isMatch1 = featureEnabledFunc("MV", "") 
     && featureEnabledFunc("GAS", "G") || !featureEnabledFunc("GAS", "F"); 

Và sau đó, nếu bạn cần thứ hai phần như một cây biểu hiện quá:

//I want the equal of the following statement in C# Linq Expression 
    Expression<Func<bool>> test = 
     Expression.Lambda<Func<bool>>(
      Expression.OrElse(
       Expression.AndAlso(
        Expression.Invoke(featureEnabledExpTree, 
         Expression.Constant("MV"), 
         Expression.Constant("") 
        ), 
        Expression.Invoke(featureEnabledExpTree, 
         Expression.Constant("GAS"), 
         Expression.Constant("G") 
        ) 
       ), 
       Expression.Not(
        Expression.Invoke(featureEnabledExpTree, 
         Expression.Constant("GAS"), 
         Expression.Constant("F") 
        ) 
       ) 
      ) 
     ); 

    bool isMatch = test.Compile()(); 
+0

Xin chào Marc, Cảm ơn bạn rất nhiều. Có, tôi cần phải xây dựng các biểu hiện tại thời gian chạy .. Nếu có thể, xin vui lòng cho tôi một số chi tiết .. –

+0

Tôi không cần phải có nếu tuyên bố .. hoặc phần nào khác để đánh giá. Tôi chỉ cần biểu hiện –

+0

Không có thông báo nếu có. OrElse/AndAlso chỉ là C# && và || bình thường các toán tử (với chiến lược đánh giá ngắn mạch của chúng được chỉ ra bởi các tên trong cây biểu thức). –

1

như thế?

Expression<Func<bool>> featureEnabledExpTree =() => 
    IsFeatureEnabled("MV", "") && 
    IsFeatureEnabled("GAS", "G") || 
    !IsFEatureEnabled("GAS", "F"); 
+0

"Tuy nhiên tôi có thể chuyển đổi chúng thành động dựa trên các ký hiệu AST của tôi" - mà tôi giải thích có nghĩa là nó phải được xây dựng dựa trên dữ liệu, do đó khả năng dịch của trình biên dịch C# để biểu thị cây có thể không hữu ích. –

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