2012-09-05 35 views
0

我工作的一種算法,其目的是創建日期的集合在其某一事件發生(這樣我可以大膽他們在MonthCalendar控件,或顯示它們一些其他方式)。動態改變在C#中的if語句的條件

現在,可以用常規功能(如果該事件發生時每星期一或每10天爲實例)來描述簡單的時間間隔,這是沒有問題的。

真正的問題(如果事件發生在每週五,如果每月的第一天是星期二,並且這類活動的實例),當我有不規則發生的事件發生。

因爲我無法預測該條件的格式,甚至是需要滿足的條件數量,所以我需要找到一種方法來在運行期間如何更改適當if語句內的條件-時間。

事件的定義存儲在一個SQL數據庫。現在,我的想法是使用一個列表,它的每個元素將是一個條件,然後將它們傳遞到適當的if語句(如果有多個語句),或將其連接成一個字符串,並傳遞給適當的if語句。

是這種做法有可能,我會怎麼做,或者我需要找到另一種方式,在這種情況下,請給我一個建議。

感謝

+0

你能提供一些示例代碼嗎? –

回答

3

可以使用泛型委託Predicate<T>。 (參見MSDN Predicate(T) Delegate

Predicate<IConditionParameters>將返回布爾值,IConditionParemeters抽象的一組條件參數和代表自身封裝一個邏輯爲返回值計算。

public class SimpleConditionParameters : IConditionParameters 
{ 
    public int XValue { get; set; } 
} 

public class ComplexConditionParameters : IConditionParameters 
{ 
    public int XValue { get; set; } 

    public int YValue { get; set; } 

    public bool SomeFlag { get; set; } 
} 

var conditions = new List<Predicate<IConditionParameters>>(); 
Predicate<SimpleConditionParameters> simpleCondition = (p) => 
{ 
    return p.XValue > 0; 
}; 

Predicate<ComplexConditionParameters> complexCondition = (p) => 
{ 
    return p.SomeFlag && p.XValue > 0 && p.YValue < 0; 
}; 

conditions.Add(simpleCondition); 
conditions.Add(complexCondition); 
0

你可以,也許,有不同類型的活動,如DailyEventWeeklyEventMonthlyEVent,等等,所有這些的實現給定的接口,它有一個名爲CanFire(或類似的東西)的方法,另一個名爲Execute

這將允許您創建不同類型的事件,其中的每一個將執行它自己的邏輯它決定是否需要被解僱或沒有。

這將允許您創建一個列表並只是遍歷這些事件,如果CanFire爲真,則請致電Execute。這將允許您讓每個事件類型具有自己的觸發和執行邏輯。

0

那麼你可以做這樣的事情:

public abstract class Condition 
{ 
    public abstract bool ConditionMet(params[] parameters); 
} 

public class DateTimeCondition : Condition 
{ 
    public override bool ConditionMet(params[] parameters) 
    { 
     // check condition against DateTime values available inside parameters 
    } 
} 

public class MoreThen10: Condition 
{ 
    public override bool ConditionMet(params[] parameters) 
    { 
     // check condition against numeric values more then 10 
    } 
} 

... // and so on 

後,你可以有條件somwhere全球徵集:

List<Condition> conditions = new List<Condition>() {new DateTimeCondition(), new MoreThen10Condition(),...}; 

,當你要檢查,如果所有條件滿足時,可以做

conditions.All(condition=>condition.ConditionMet(..parameters of if...)); 

自然,這只是基本的想法,一個sketchup,沒有具體的實現你可以複製/粘貼。

2

另一種可能是使用的CodeDOM在運行時動態生成的代碼,如:

public class DynamicIfStatementEvaluator 
{ 
    public bool EvaluateBools() 
    { 
     // Create a new instance of the C# compiler 
     //from http://mattephraim.com/blog/2009/01/02/treating-c-like-a-scripting-language/ 

     CSharpCodeProvider compiler = new CSharpCodeProvider(); 

     // Create some parameters for the compiler 
     CompilerParameters parms = new CompilerParameters 
     { 
      GenerateExecutable = false, 
      GenerateInMemory = true 
     }; 
     parms.ReferencedAssemblies.Add("System.dll"); 


     // Try to compile the string into an assembly 
     CompilerResults results = compiler.CompileAssemblyFromSource(parms, new string[] 
           {@"using System; 

            class BoolEvaluator 
            { 
             public bool EvalBoolean() 
             { 
              return " + InsertBooleanStringHere! + @"; 
             }    
            }"}); 

     if (results.Errors.Count == 0) 
     { 
      object myClass = results.CompiledAssembly.CreateInstance("BoolEvaluator"); 
      if (myClass != null) 
      { 
       object boolReturn = myClass.GetType(). 
             GetMethod("EvalBoolean"). 
             Invoke(myClass, null); 
       return Convert.ToBoolean(boolReturn); 
      } 
     } 
     else 
     { 
      foreach (object error in results.Errors) 
      { 
       MessageBox.Show(error.ToString()); 
      } 
     } 
     return false; 
    }   
} 

添加using語句使用System.CodeDom.Compiler;