CsCodeHelper.cs 11 KB

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