-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
303 lines (284 loc) · 7.66 KB
/
Program.cs
File metadata and controls
303 lines (284 loc) · 7.66 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using DynamicScriptExecutor;
/**
* HelloWorld test
*/
string codeHelloWorld = @"
using System;
class Exec
{
public void Main()
{
Console.WriteLine(""Hello World"");
}
}
";
ScriptExecutor.Exec(codeHelloWorld);
/**
* Param test
*/
string codeDraw = @"
using System;
using System.Text;
class Exec
{
public string Main(int mid)
{
for (int i = 0; true; ++i)
{
int x = Math.Abs(mid - i);
int y = mid * 2 - x;
OutputLog(x, y, 2 * mid + 1);
if (x == y && i > 0)
{
break;
}
}
return ""codeDraw end"";
}
private void OutputLog(int x, int y, int length)
{
StringBuilder stringBuilder = new (new string(' ', length));
stringBuilder[x] = '*';
stringBuilder[y] = '*';
string value = stringBuilder.ToString();
Console.WriteLine(value);
}
}
";
object[] paramList = new object[1] { 30 };
string codeDrawRes = (string)ScriptExecutor.Exec(codeDraw, new ExecOption(paramList));
Console.WriteLine(codeDrawRes);
/**
* InstanceObject test
*/
ExecOption execOptionHelloWorld = new ExecOption();
execOptionHelloWorld.InstanceObject = new InstanceObject(codeHelloWorld);
ScriptExecutor.Exec(execOptionHelloWorld);
/**
* Async test
*/
string codeCostTime = @"
using System;
using System.Threading;
class Exec
{
public int Main()
{
Console.WriteLine(""Start sleep"");
Thread.Sleep(3000);
Console.WriteLine(""Sleeped 3s"");
return 1024;
}
}
";
Task<object> task = ScriptExecutor.ExecAsync(codeCostTime);
Console.WriteLine("output after ExecAsync");
Thread.Sleep(1500);
Console.WriteLine("output when sleeping");
int result = (int)task.GetAwaiter().GetResult();
Console.WriteLine("output after GetResult, result is: " + result);
/**
* Dependency test
*/
string codeMain = @"
using System;
class Exec
{
public void Main()
{
Console.WriteLine(""Hello World In Main"");
ExtraClass.ExtraOutput();
}
}
";
string codeExtra = @"
using System;
public static class ExtraClass
{
public static void ExtraOutput()
{
Console.WriteLine(""Hello World In Extra"");
}
}
";
ScriptExecutor.Exec(new string[] { codeMain, codeExtra });
/**
* Dependency + InstanceObject test
*/
ExecOption execOptionMainExtra = new ExecOption();
execOptionMainExtra.InstanceObject = new InstanceObject(new List<string> { codeMain, codeExtra });
ScriptExecutor.Exec(execOptionMainExtra);
/**
* VB.NET HelloWorld test
*/
string codeHelloWorldVB = @"
Imports System
Public Class Exec
Public Sub Main()
Console.WriteLine(""Hello World VB.NET"")
End Sub
End Class
";
ExecOption execOptionVB = new ExecOption() { ScriptLanguage = ScriptLanguage.VisualBasic };
ScriptExecutor.Exec(codeHelloWorldVB, execOptionVB);
/**
* Private test
*/
string codeHelloWorldPrivate = @"
using System;
class Exec
{
private void Main()
{
Console.WriteLine(""Hello World Private"");
}
}
";
ExecOption execOptionHelloWorldPrivate = new ExecOption();
execOptionHelloWorldPrivate.NonPublic = true;
ScriptExecutor.Exec(codeHelloWorldPrivate, execOptionHelloWorldPrivate);
/**
* Static test
*/
string codeStatic = @"
using System;
public static class Exec
{
public static int count = 1;
public static void Main()
{
Console.WriteLine(""Hello World Static: "" + count++);
}
}
";
ExecOption execOptionStatic = new ExecOption() { IsStatic = true };
execOptionStatic.InstanceObject = new InstanceObject(codeStatic, execOptionStatic);
ScriptExecutor.Exec(execOptionStatic);
ScriptExecutor.Exec(execOptionStatic);
/**
* Private Static test
*/
string codePrivateStatic = @"
using System;
static class Exec
{
private static void Main()
{
Console.WriteLine(""Hello World Private & Static"");
}
}
";
ExecOption execOptionPrivateStatic = new ExecOption() { IsStatic = true, NonPublic = true };
ScriptExecutor.Exec(codePrivateStatic, execOptionPrivateStatic);
/**
* Delegate HelloWorld
*/
string codeDelegateHelloWorld = @"
using System;
public class Exec
{
public string DelegateHelloWorldFunc()
{
return ""Delegate Hello World"";
}
}
";
var DelegateHelloWorldFunc = ScriptExecutor.GenerateFunc<string>(codeDelegateHelloWorld, new ExecOption() { MethodName = "DelegateHelloWorldFunc" });
Console.WriteLine(DelegateHelloWorldFunc(null));
/**
* Delegate test
*/
string codeDelegate = @"
using System;
using System.Text;
public class Exec
{
public string DelegateTestFunc(int length, char c, int textIndex)
{
StringBuilder stringBuilder = new (new string('-', length));
stringBuilder[textIndex] = c;
return stringBuilder.ToString();
}
}
";
var DelegateTestFunc = ScriptExecutor.GenerateFunc<string>(codeDelegate, new ExecOption() { MethodName = "DelegateTestFunc" });
Console.WriteLine(DelegateTestFunc(new object[] { 10, 't', 3 }));
/**
* Delegate no generic test
*/
string codeDelegateNoGeneric = @"
using System;
using System.Text;
public class Exec
{
public string DelegateNoGenericTestFunc()
{
return ""Delegate no generic test"";
}
}
";
var DelegateNoGenericTestFunc = ScriptExecutor.GenerateFunc(codeDelegateNoGeneric, new ExecOption() { MethodName = "DelegateNoGenericTestFunc" });
Console.WriteLine(DelegateNoGenericTestFunc(null));
/**
* Delegate test
*/
string codeDelegateMultipleParams = @"
using System;
using System.Text;
public class Exec
{
public string CodeDelegateMultipleParamsTestFunc(int length, char c, int textIndex)
{
StringBuilder stringBuilder = new (new string('-', length));
stringBuilder[textIndex] = c;
return stringBuilder.ToString();
}
}
";
var CodeDelegateMultipleParamsTestFunc = ScriptExecutor.GenerateFunc<int, char, int, string>(codeDelegateMultipleParams, new ExecOption() { MethodName = "CodeDelegateMultipleParamsTestFunc" });
Console.WriteLine(CodeDelegateMultipleParamsTestFunc(10, 't', 6));
/**
* Delegate Static test
*/
string codeDelegateStatic = @"
using System;
public static class Exec
{
public static int count = 1;
public static void DelegateStaticFunc()
{
Console.WriteLine(""Hello World Delegate Static: "" + count++);
}
}
";
ExecOption execOptionDelegateStatic = new ExecOption() { IsStatic = true };
execOptionStatic.InstanceObject = new InstanceObject(codeDelegateStatic, execOptionDelegateStatic);
execOptionStatic.MethodName = "DelegateStaticFunc";
var DelegateStaticFunc = ScriptExecutor.GenerateFunc<object>(execOptionStatic);
DelegateStaticFunc(null);
DelegateStaticFunc(null);
/**
* GenerateClassWithFunction test
*/
string codeGenerateClassWithFunction = @"
public int GenerateClassWithFunctionTestFunc()
{
List<DiffRes> res = DiffTool.Diff(
new List<string>() { ""11111111"", ""2222222"", ""3333333"", ""4444444"", ""555"", ""666"", ""777"", ""888"", """", ""999"", ""99"", ""88"", ""77"" },
new List<string>() { ""11111111"", ""22222222"", ""33333333"", ""44444444"", """", ""666"", ""99"", ""88"", ""77"" });
List<GroupedDiffRes> groupedDiffRes = DiffTool.GetGroupedResult(res);
return groupedDiffRes.Count;
}
";
ExecOption generateClassWithFunctionOption = new ExecOption();
generateClassWithFunctionOption.ExtraDllFileList = new List<string> { "Diff.dll" };
generateClassWithFunctionOption.MethodName = "GenerateClassWithFunctionTestFunc";
try
{
string codeGeneratedClassWithFunction = ScriptExecutor.GenerateClassWithFunction(codeGenerateClassWithFunction, generateClassWithFunctionOption);
Console.WriteLine(ScriptExecutor.Exec(codeGeneratedClassWithFunction, generateClassWithFunctionOption));
}
catch
{
Console.WriteLine("DLL NOT FOUND");
}