-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecursiveHelper.cs
167 lines (161 loc) · 5.02 KB
/
RecursiveHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RecursiveHelper
{
class RecursiveHelper
{
public byte RecursivDeepLimit { get; set; }
public int WaitBetweenRecurseLevel { get; set; }
public RecursiveHelper()
{
RecursivDeepLimit = 3;
WaitBetweenRecurseLevel = 1000;
}
public RecursiveHelper(byte deepLimit, int waitBeetweenRecurseLevel)
{
RecursivDeepLimit = deepLimit;
WaitBetweenRecurseLevel = waitBeetweenRecurseLevel;
}
public RecursiveResult<TResult> GetRecursiveRepeatMethodWrapper<TResult>(IMethodInvoker<TResult> method)
{
return StartRecursiveRepeatMethodWrapper(method);
}
private RecursiveResult<TResult> StartRecursiveRepeatMethodWrapper<TResult>(IMethodInvoker<TResult> method, int deepLevel = 0)
{
RecursiveResult<TResult> invokeResult = method.Invoke();
if (invokeResult.IsSuccess)
return invokeResult;
if (CheckRecursivDeepLimit(deepLevel))
return RecursionDeeper(method, deepLevel);
else
return invokeResult;
}
private bool CheckRecursivDeepLimit(int deepLevel)
{
return deepLevel < RecursivDeepLimit;
}
private RecursiveResult<TResult> RecursionDeeper<TResult>(IMethodInvoker<TResult> method, int deepLevel)
{
Thread.Sleep(WaitBetweenRecurseLevel);
return StartRecursiveRepeatMethodWrapper(method, deepLevel + 1);
}
}
interface IMethodInvoker<TResult>
{
RecursiveResult<TResult> Invoke();
}
class ActionInvoker<TResult> : IMethodInvoker<TResult>
{
public Action Method { get; set; }
public RecursiveResult<TResult> Invoke()
{
RecursiveResult<TResult> result = new RecursiveResult<TResult>();
try
{
InnerInvoke();
result.IsSuccess = true;
}
catch(Exception ex)
{
result.IsSuccess = false;
result.CatchException = ex;
}
return result;
}
public virtual void InnerInvoke()
{
Method();
}
}
class ActionInvoker<T1, TResult> : ActionInvoker<TResult>
{
public new Action<T1> Method { get; set; }
public T1 Arg1 { get; set; }
public override void InnerInvoke()
{
Method(Arg1);
}
}
class ActionInvoker<T1, T2, TResult> : ActionInvoker<T1, TResult>
{
public new Action<T1, T2> Method { get; set; }
public T2 Arg2 { get; set; }
public override void InnerInvoke()
{
Method(Arg1, Arg2);
}
}
class ActionInvoker<T1, T2, T3, TResult> : ActionInvoker<T1, T2, TResult>
{
public new Action<T1, T2, T3> Method { get; set; }
public T3 Arg3 { get; set; }
public override void InnerInvoke()
{
Method(Arg1, Arg2, Arg3);
}
}
class FuncInvoker<TResult> : IMethodInvoker<TResult>
{
public Func<TResult> Method { get; set; }
public Func<TResult, bool> CheckResult { get; set; }
public RecursiveResult<TResult> Invoke()
{
RecursiveResult<TResult> result = new RecursiveResult<TResult>();
try
{
result.Result = InnerInvoke();
if (CheckResult?.Invoke(result.Result) ?? true)
result.IsSuccess = true;
else
result.IsSuccess = false;
}
catch(Exception ex)
{
result.IsSuccess = false;
result.CatchException = ex;
}
return result;
}
public virtual TResult InnerInvoke()
{
return Method();
}
}
class FuncInvoker<T1, TResult> : FuncInvoker<TResult>
{
public new Func<T1, TResult> Method { get; set; }
public T1 Arg1 { get; set; }
public override TResult InnerInvoke()
{
return Method(Arg1);
}
}
class FuncInvoker<T1, T2, TResult> : FuncInvoker<T1, TResult>
{
public new Func<T1, T2, TResult> Method { get; set; }
public T2 Arg2 { get; set; }
public override TResult InnerInvoke()
{
return Method(Arg1, Arg2);
}
}
class FuncInvoker<T1, T2, T3, TResult> : FuncInvoker<T1, T2, TResult>
{
public new Func<T1, T2, T3, TResult> Method { get; set; }
public T3 Arg3 { get; set; }
public override TResult InnerInvoke()
{
return Method(Arg1, Arg2, Arg3);
}
}
class RecursiveResult<TResult>
{
public bool IsSuccess { get; set; }
public TResult Result { get; set; }
public Exception CatchException { get; set; }
}
}