क्या मैं कहना है कि InvokeMethod एक विधि आह्वान कर सकते हैं और जब दोहराएँ जैसे विशेष विकल्पों का उपयोग कर यह दोहराएँ के बाद exexute करेगा मुझे क्या करना होगा के लिए सी # बिल्डिंग Fluent एपीआई।विधि आमंत्रण
अब मेरी समस्या यह है कि विधि पहले से ही निष्पादित होगी इससे पहले कि यह जानता है कि इसे 100 बार कहा जाना है।
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 (एक्शन एक्शन, int repeat = 1) – Hath
जैसे invokemethod के तर्क के रूप में 'दोहराना' क्यों नहीं है मैंने इसके बारे में भी सोचा, लेकिन मुझे यह पसंद नहीं है, क्योंकि आप नहीं देख सकते कि संख्या क्या है पैरामीटर नाम पढ़ने के बिना। – Rookian
आप कॉल करते समय अधिक स्पष्ट होने के लिए नामित पैरामीटर का उपयोग कर सकते हैं। –