Repository navigation
Expand file tree
/
Copy pathCsCodeHelper.cs
More file actions
344 lines (303 loc) · 12.1 KB
/
Copy pathCsCodeHelper.cs
File metadata and controls
344 lines (303 loc) · 12.1 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
#if NETSTANDARD || NETCOREAPP
using FastReport.Code.CodeDom.Compiler;
using FastReport.Code.CSharp;
#else
using System.CodeDom.Compiler;
using Microsoft.CSharp;
#endif
using FastReport.Utils;
using FastReport.Data;
namespace FastReport.Code
{
internal partial class CsCodeHelper : CodeHelperBase
{
#region Private Methods
private string GetEquivalentKeyword(string s, bool fullForm)
{
string fontKeywordBegin = "<font color=\"Blue\">";
string fontTypeBegin = "<font color=\"DarkCyan\">";
string fontEnd = "</font>";
if (s.EndsWith("[]"))
{
string word = s.Substring(0, s.Length - 2);
string keyword = GetEquivalentKeyword1(word);
return fullForm ? (keyword == word ? fontTypeBegin : fontKeywordBegin) + keyword + "[]" + fontEnd : keyword + "[]";
}
else
{
string keyword = GetEquivalentKeyword1(s);
return fullForm ? (keyword == s ? fontTypeBegin : fontKeywordBegin) + keyword + fontEnd : keyword;
}
}
private string GetEquivalentKeyword1(string s)
{
switch (s)
{
case "Object":
return "object";
case "String":
return "string";
case "Char":
return "char";
case "Byte":
return "byte";
case "SByte":
return "sbyte";
case "Int16":
return "short";
case "UInt16":
return "ushort";
case "Int32":
return "int";
case "UInt32":
return "uint";
case "Int64":
return "long";
case "UInt64":
return "ulong";
case "Single":
return "float";
case "Double":
return "double";
case "Decimal":
return "decimal";
case "Boolean":
return "bool";
case "Void":
return "void";
}
return s;
}
#endregion
#region Protected Methods
protected override string GetTypeDeclaration(Type type)
{
if (type.IsGenericType)
{
string result = type.Name;
result = result.Substring(0, result.IndexOf('`'));
result += "<";
foreach (Type elementType in type.GetGenericArguments())
{
result += GetTypeDeclaration(elementType) + ",";
}
result = result.Substring(0, result.Length - 1) + ">";
return result;
}
else
return type.Name;
}
#endregion
#region Public Methods
public override string EmptyScript()
{
return "using System;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\n" +
"using System.ComponentModel;\r\nusing System.Windows.Forms;\r\nusing System.Drawing;\r\n" +
"using System.Data;\r\nusing FastReport;\r\nusing FastReport.Data;\r\nusing FastReport.Dialog;\r\n" +
"using FastReport.Barcode;\r\nusing FastReport.Table;\r\nusing FastReport.Utils;\r\n\r\n" +
"namespace FastReport\r\n{\r\n public class ReportScript\r\n {\r\n }\r\n}\r\n";
}
public override int GetPositionToInsertOwnItems(string scriptText)
{
int pos = scriptText.IndexOf("public class ReportScript");
if (pos == -1)
return -1;
return scriptText.IndexOf('{', pos) + 3;
}
public override string AddField(Type type, string name)
{
name = name.Replace(" ", "_");
return " public " + type.FullName + " " + name + ";\r\n";
}
public override string BeginCalcExpression()
{
return " private object CalcExpression(string expression, Variant Value)\r\n {\r\n ";
}
public override string AddExpression(string expr, string value)
{
expr = expr.Replace("\\", "\\\\");
expr = expr.Replace("\"", "\\\"");
return "if (expression == \"" + expr + "\")\r\n return " + value + ";\r\n ";
}
public override string EndCalcExpression()
{
return "return null;\r\n }\r\n\r\n";
}
public override string ReplaceColumnName(string name, Type type)
{
string typeName = GetTypeDeclaration(type);
string result = "((" + typeName + ")Report.GetColumnValue(\"" + name + "\"";
result += "))";
return result;
}
public override string ReplaceParameterName(Parameter parameter)
{
string typeName = GetTypeDeclaration(parameter.DataType);
return "((" + typeName + ")Report.GetParameterValue(\"" + parameter.FullName + "\"))";
}
public override string ReplaceVariableName(Parameter parameter)
{
string typeName = GetTypeDeclaration(parameter.DataType);
return "((" + typeName + ")Report.GetVariableValue(\"" + parameter.FullName + "\"))";
}
public override string ReplaceTotalName(string name)
{
return "Report.GetTotalValue(\"" + name + "\")";
}
public override string GenerateInitializeMethod()
{
Hashtable events = new Hashtable();
string reportString = StripEventHandlers(events);
string result = "";
// form the InitializeComponent method
result += " private void InitializeComponent()\r\n {\r\n ";
// form the reportString
result += "string reportString = \r\n ";
int totalLength = 0;
while (reportString.Length > 0)
{
string part = "";
if (reportString.Length > 80)
{
part = reportString.Substring(0, 80);
reportString = reportString.Substring(80);
}
else
{
part = reportString;
reportString = "";
}
part = part.Replace("\\", "\\\\");
part = part.Replace("\"", "\\\"");
part = part.Replace("\r", "\\r");
part = part.Replace("\n", "\\n");
result += "\"" + part + "\"";
if (reportString != "")
{
if (totalLength > 1024)
{
totalLength = 0;
result += ";\r\n reportString += ";
}
else
result += " +\r\n ";
totalLength += part.Length;
}
else
{
result += ";\r\n ";
}
}
result += "LoadFromString(reportString);\r\n ";
result += "InternalInit();\r\n ";
// form objects' event handlers
foreach (DictionaryEntry de in events)
{
result += de.Key.ToString() + " += " + de.Value.ToString() + ";\r\n ";
}
result += "\r\n }\r\n\r\n";
result += " public ReportScript()\r\n {\r\n InitializeComponent();\r\n }\r\n";
return result;
}
public override string ReplaceClassName(string scriptText, string className)
{
return scriptText.Replace("class ReportScript", "class " + className + " : Report").Replace(
"public ReportScript()", "public " + className + "()").Replace(
"private object CalcExpression", "protected override object CalcExpression");
}
public override string GetMethodSignature(MethodInfo info, bool fullForm)
{
string result = fullForm ? "<font color=\"#74531F\">" + info.Name + "</font>(" : info.Name + "(";
if (fullForm)
result = GetEquivalentKeyword(info.ReturnType.Name, fullForm) + " " + result;
System.Reflection.ParameterInfo[] pars = info.GetParameters();
foreach (System.Reflection.ParameterInfo par in pars)
{
// special case - skip "thisReport" parameter
if (par.Name == "thisReport")
continue;
string paramType = "";
object[] attr = par.GetCustomAttributes(typeof(ParamArrayAttribute), false);
if (attr.Length > 0)
paramType = "params ";
paramType += GetEquivalentKeyword(par.ParameterType.Name, fullForm);
result += paramType;
result += (fullForm ? " " + par.Name : "");
#if DOTNET_4
if (par.IsOptional && fullForm)
result += CodeUtils.GetOptionalParameter(par, CodeUtils.Language.Cs);
#endif
result += ", ";
}
if (result.EndsWith(", "))
result = result.Substring(0, result.Length - 2);
result += ")";
return result;
}
public override string GetMethodSignatureAndBody(MethodInfo info)
{
string result = info.Name + "(";
result = " private " + GetTypeDeclaration(info.ReturnType) + " " + result;
System.Reflection.ParameterInfo[] pars = info.GetParameters();
foreach (System.Reflection.ParameterInfo par in pars)
{
// special case - skip "thisReport" parameter
if (par.Name == "thisReport")
continue;
string paramType = "";
object[] attr = par.GetCustomAttributes(typeof(ParamArrayAttribute), false);
if (attr.Length > 0)
paramType = "params ";
paramType += GetTypeDeclaration(par.ParameterType);
result += paramType;
result += " " + par.Name;
#if DOTNET_4
if (par.IsOptional)
result += CodeUtils.GetOptionalParameter(par, CodeUtils.Language.Cs);
#endif
result += ", ";
}
if (result.EndsWith(", "))
result = result.Substring(0, result.Length - 2);
result += ")";
result += "\r\n";
result += " {\r\n";
result += " return " + info.ReflectedType.Namespace + "." +
info.ReflectedType.Name + "." + info.Name + "(";
foreach (System.Reflection.ParameterInfo par in pars)
{
string parName = par.Name;
// special case - handle "thisReport" parameter
if (parName == "thisReport")
parName = "Report";
result += parName + ", ";
}
if (result.EndsWith(", "))
result = result.Substring(0, result.Length - 2);
result += ");\r\n";
result += " }\r\n";
result += "\r\n";
return result;
}
public override string GetPropertySignature(PropertyInfo info, bool fullForm)
{
string result = GetEquivalentKeyword(info.PropertyType.Name, fullForm) + " " + info.Name;
if (fullForm)
result += " {<font color=\"Blue\"> get;" + (info.CanWrite ? " set;" : "") + "</font> }";
return result;
}
public override CodeDomProvider GetCodeProvider()
{
return new CSharpCodeProvider();
}
#endregion
public CsCodeHelper(Report report) : base(report)
{
}
}
}