CsCodeHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Reflection;
  6. #if NETSTANDARD || NETCOREAPP
  7. using FastReport.Code.CodeDom.Compiler;
  8. using FastReport.Code.CSharp;
  9. #else
  10. using System.CodeDom.Compiler;
  11. using Microsoft.CSharp;
  12. #endif
  13. using FastReport.Utils;
  14. using FastReport.Data;
  15. namespace FastReport.Code
  16. {
  17. internal partial class CsCodeHelper : CodeHelperBase
  18. {
  19. #region Private Methods
  20. private string GetEquivalentKeyword(string s, bool fullForm)
  21. {
  22. string fontKeywordBegin = "<font color=\"Blue\">";
  23. string fontTypeBegin = "<font color=\"DarkCyan\">";
  24. string fontEnd = "</font>";
  25. if (s.EndsWith("[]"))
  26. {
  27. string word = s.Substring(0, s.Length - 2);
  28. string keyword = GetEquivalentKeyword1(word);
  29. return fullForm ? (keyword == word ? fontTypeBegin : fontKeywordBegin) + keyword + "[]" + fontEnd : keyword + "[]";
  30. }
  31. else
  32. {
  33. string keyword = GetEquivalentKeyword1(s);
  34. return fullForm ? (keyword == s ? fontTypeBegin : fontKeywordBegin) + keyword + fontEnd : keyword;
  35. }
  36. }
  37. private string GetEquivalentKeyword1(string s)
  38. {
  39. switch (s)
  40. {
  41. case "Object":
  42. return "object";
  43. case "String":
  44. return "string";
  45. case "Char":
  46. return "char";
  47. case "Byte":
  48. return "byte";
  49. case "SByte":
  50. return "sbyte";
  51. case "Int16":
  52. return "short";
  53. case "UInt16":
  54. return "ushort";
  55. case "Int32":
  56. return "int";
  57. case "UInt32":
  58. return "uint";
  59. case "Int64":
  60. return "long";
  61. case "UInt64":
  62. return "ulong";
  63. case "Single":
  64. return "float";
  65. case "Double":
  66. return "double";
  67. case "Decimal":
  68. return "decimal";
  69. case "Boolean":
  70. return "bool";
  71. case "Void":
  72. return "void";
  73. }
  74. return s;
  75. }
  76. #endregion
  77. #region Protected Methods
  78. protected override string GetTypeDeclaration(Type type)
  79. {
  80. if (type.IsGenericType)
  81. {
  82. string result = type.Name;
  83. result = result.Substring(0, result.IndexOf('`'));
  84. result += "<";
  85. foreach (Type elementType in type.GetGenericArguments())
  86. {
  87. result += GetTypeDeclaration(elementType) + ",";
  88. }
  89. result = result.Substring(0, result.Length - 1) + ">";
  90. return result;
  91. }
  92. else
  93. return type.Name;
  94. }
  95. #endregion
  96. #region Public Methods
  97. public override string EmptyScript()
  98. {
  99. return
  100. @"using System;
  101. using System.Collections;
  102. using System.Collections.Generic;
  103. using System.ComponentModel;
  104. using System.Windows.Forms;
  105. using System.Drawing;
  106. using System.Data;
  107. using FastReport;
  108. using FastReport.Data;
  109. using FastReport.Dialog;
  110. using FastReport.Barcode;
  111. using FastReport.Table;
  112. using FastReport.Utils;
  113. namespace FastReport
  114. {
  115. public class ReportScript
  116. {
  117. }
  118. }
  119. ";
  120. }
  121. public override int GetPositionToInsertOwnItems(string scriptText)
  122. {
  123. int pos = scriptText.IndexOf("public class ReportScript");
  124. if (pos == -1)
  125. return -1;
  126. return scriptText.IndexOf('{', pos) + 3;
  127. }
  128. public override string AddField(Type type, string name)
  129. {
  130. name = name.Replace(" ", "_");
  131. return " public " + type.FullName + " " + name + ";\r\n";
  132. }
  133. public override string BeginCalcExpression()
  134. {
  135. return " private object CalcExpression(string expression, Variant Value)\r\n {\r\n ";
  136. }
  137. public override string AddExpression(string expr, string value)
  138. {
  139. expr = expr.Replace("\\", "\\\\");
  140. expr = expr.Replace("\"", "\\\"");
  141. return "if (expression == \"" + expr + "\")\r\n return " + value + ";\r\n ";
  142. }
  143. public override string EndCalcExpression()
  144. {
  145. return "return null;\r\n }\r\n\r\n";
  146. }
  147. public override string ReplaceColumnName(string name, Type type)
  148. {
  149. string typeName = GetTypeDeclaration(type);
  150. string result = "((" + typeName + ")Report.GetColumnValue(\"" + name + "\"";
  151. result += "))";
  152. return result;
  153. }
  154. public override string ReplaceParameterName(Parameter parameter)
  155. {
  156. string typeName = GetTypeDeclaration(parameter.DataType);
  157. return "((" + typeName + ")Report.GetParameterValue(\"" + parameter.FullName + "\"))";
  158. }
  159. public override string ReplaceVariableName(Parameter parameter)
  160. {
  161. string typeName = GetTypeDeclaration(parameter.DataType);
  162. return "((" + typeName + ")Report.GetVariableValue(\"" + parameter.FullName + "\"))";
  163. }
  164. public override string ReplaceTotalName(string name)
  165. {
  166. return "Report.GetTotalValue(\"" + name + "\")";
  167. }
  168. public override string GenerateInitializeMethod()
  169. {
  170. Hashtable events = new Hashtable();
  171. string reportString = StripEventHandlers(events);
  172. string result = "";
  173. // form the InitializeComponent method
  174. result += " private void InitializeComponent()\r\n {\r\n ";
  175. // form the reportString
  176. result += "string reportString = \r\n ";
  177. int totalLength = 0;
  178. while (reportString.Length > 0)
  179. {
  180. string part = "";
  181. if (reportString.Length > 80)
  182. {
  183. part = reportString.Substring(0, 80);
  184. reportString = reportString.Substring(80);
  185. }
  186. else
  187. {
  188. part = reportString;
  189. reportString = "";
  190. }
  191. part = part.Replace("\\", "\\\\");
  192. part = part.Replace("\"", "\\\"");
  193. part = part.Replace("\r", "\\r");
  194. part = part.Replace("\n", "\\n");
  195. result += "\"" + part + "\"";
  196. if (reportString != "")
  197. {
  198. if (totalLength > 1024)
  199. {
  200. totalLength = 0;
  201. result += ";\r\n reportString += ";
  202. }
  203. else
  204. result += " +\r\n ";
  205. totalLength += part.Length;
  206. }
  207. else
  208. {
  209. result += ";\r\n ";
  210. }
  211. }
  212. result += "LoadFromString(reportString);\r\n ";
  213. result += "InternalInit();\r\n ";
  214. // form objects' event handlers
  215. foreach (DictionaryEntry de in events)
  216. {
  217. result += de.Key.ToString() + " += " + de.Value.ToString() + ";\r\n ";
  218. }
  219. result += "\r\n }\r\n\r\n";
  220. result += " public ReportScript()\r\n {\r\n InitializeComponent();\r\n }\r\n";
  221. return result;
  222. }
  223. public override string ReplaceClassName(string scriptText, string className)
  224. {
  225. return scriptText.Replace("class ReportScript", "class " + className + " : Report").Replace(
  226. "public ReportScript()", "public " + className + "()").Replace(
  227. "private object CalcExpression", "protected override object CalcExpression");
  228. }
  229. public override string GetMethodSignature(MethodInfo info, bool fullForm)
  230. {
  231. string result = fullForm ? "<font color=\"#74531F\">" + info.Name + "</font>(" : info.Name + "(";
  232. if (fullForm)
  233. result = GetEquivalentKeyword(info.ReturnType.Name, fullForm) + " " + result;
  234. System.Reflection.ParameterInfo[] pars = info.GetParameters();
  235. foreach (System.Reflection.ParameterInfo par in pars)
  236. {
  237. // special case - skip "thisReport" parameter
  238. if (par.Name == "thisReport")
  239. continue;
  240. string paramType = "";
  241. object[] attr = par.GetCustomAttributes(typeof(ParamArrayAttribute), false);
  242. if (attr.Length > 0)
  243. paramType = "params ";
  244. paramType += GetEquivalentKeyword(par.ParameterType.Name, fullForm);
  245. result += paramType;
  246. result += (fullForm ? " " + par.Name : "");
  247. #if DOTNET_4
  248. if (par.IsOptional && fullForm)
  249. result += CodeUtils.GetOptionalParameter(par, CodeUtils.Language.Cs);
  250. #endif
  251. result += ", ";
  252. }
  253. if (result.EndsWith(", "))
  254. result = result.Substring(0, result.Length - 2);
  255. result += ")";
  256. return result;
  257. }
  258. public override string GetMethodSignatureAndBody(MethodInfo info)
  259. {
  260. string result = info.Name + "(";
  261. result = " private " + GetTypeDeclaration(info.ReturnType) + " " + result;
  262. System.Reflection.ParameterInfo[] pars = info.GetParameters();
  263. foreach (System.Reflection.ParameterInfo par in pars)
  264. {
  265. // special case - skip "thisReport" parameter
  266. if (par.Name == "thisReport")
  267. continue;
  268. string paramType = "";
  269. object[] attr = par.GetCustomAttributes(typeof(ParamArrayAttribute), false);
  270. if (attr.Length > 0)
  271. paramType = "params ";
  272. paramType += GetTypeDeclaration(par.ParameterType);
  273. result += paramType;
  274. result += " " + par.Name;
  275. #if DOTNET_4
  276. if (par.IsOptional)
  277. result += CodeUtils.GetOptionalParameter(par, CodeUtils.Language.Cs);
  278. #endif
  279. result += ", ";
  280. }
  281. if (result.EndsWith(", "))
  282. result = result.Substring(0, result.Length - 2);
  283. result += ")";
  284. result += "\r\n";
  285. result += " {\r\n";
  286. result += " return " + info.ReflectedType.Namespace + "." +
  287. info.ReflectedType.Name + "." + info.Name + "(";
  288. foreach (System.Reflection.ParameterInfo par in pars)
  289. {
  290. string parName = par.Name;
  291. // special case - handle "thisReport" parameter
  292. if (parName == "thisReport")
  293. parName = "Report";
  294. result += parName + ", ";
  295. }
  296. if (result.EndsWith(", "))
  297. result = result.Substring(0, result.Length - 2);
  298. result += ");\r\n";
  299. result += " }\r\n";
  300. result += "\r\n";
  301. return result;
  302. }
  303. public override string GetPropertySignature(PropertyInfo info, bool fullForm)
  304. {
  305. string result = GetEquivalentKeyword(info.PropertyType.Name, fullForm) + " " + info.Name;
  306. if (fullForm)
  307. result += " {<font color=\"Blue\"> get;" + (info.CanWrite ? " set;" : "") + "</font> }";
  308. return result;
  309. }
  310. public override CodeDomProvider GetCodeProvider()
  311. {
  312. return new CSharpCodeProvider();
  313. }
  314. #endregion
  315. public CsCodeHelper(Report report) : base(report)
  316. {
  317. }
  318. }
  319. }