我有什麼做的,說的InvokeMethod可以調用方法,並利用特殊的選項,如重複時,應當在重複後exexute C#樓流利的API。方法調用
我現在的問題是該方法在它知道必須被調用100次之前已經執行了exexute。
class Program
{
static void Main()
{
const bool shouldRun = true;
new MethodExecuter()
.ForAllInvocationsUseCondition(!Context.WannaShutDown)
.InvokeMethod(A.Process).Repeat(100)
.When(shouldRun).ThenInvokeMethod(B.Process).Repeat(10)
.ForAllInvocationsUseCondition(Context.WannaShutDown)
.When(shouldRun).ThenInvokeMethod(C.Process);
}
}
MethodExpression的
public class MethodExpression
{
private bool _isTrue = true;
private readonly MethodExecuter _methodExecuter;
public MethodExpression(bool isTrue, MethodExecuter methodExecuter)
{
_isTrue = isTrue;
_methodExecuter = methodExecuter;
}
public MethodExecuter ThenInvokeMethod(Action action)
{
if (_isTrue)
{
action.Invoke();
_isTrue = false;
}
return _methodExecuter;
}
}
MethodExecuter
public class MethodExecuter
{
private bool _condition;
private int _repeat = 1;
public MethodExpression When(bool isTrue)
{
return new MethodExpression(isTrue && _condition, this);
}
public MethodExecuter InvokeMethod(Action action)
{
if (_condition)
{
for (int i = 1; i <= _repeat; i++)
{
action.Invoke();
}
}
return this;
}
public MethodExecuter ForAllInvocationsUseCondition(bool condition)
{
_condition = condition;
return this;
}
public MethodExecuter Repeat(int repeat)
{
_repeat = repeat;
return this;
}
}
爲什麼不能有「重複」作爲的InvokeMethod像的InvokeMethod參數(行動行動,整合重複= 1) – Hath 2011-06-07 16:44:57
我想過太多,但我不喜歡它,因爲你不能看到什麼是多少因爲沒有讀取參數名稱。 – Rookian 2011-06-07 16:48:31
您可以在調用命名參數時使用更明確的參數。 – 2011-06-07 16:53:25