VbCodeHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.VisualBasic;
  9. #else
  10. using System.CodeDom.Compiler;
  11. using Microsoft.VisualBasic;
  12. #endif
  13. using FastReport.Utils;
  14. using FastReport.Data;
  15. namespace FastReport.Code
  16. {
  17. internal partial class VbCodeHelper : 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 "DateTime":
  31. return "Date";
  32. case "Int16":
  33. return "Short";
  34. case "UInt16":
  35. return "UShort";
  36. case "Int32":
  37. return "Integer";
  38. case "UInt32":
  39. return "UInteger";
  40. case "Int64":
  41. return "Long";
  42. case "UInt64":
  43. return "ULong";
  44. }
  45. return s;
  46. }
  47. #endregion
  48. #region Protected Methods
  49. protected override string GetTypeDeclaration(Type type)
  50. {
  51. if (type.IsGenericType)
  52. {
  53. string result = type.Name;
  54. result = result.Substring(0, result.IndexOf('`'));
  55. result += "(Of ";
  56. foreach (Type elementType in type.GetGenericArguments())
  57. {
  58. result += GetTypeDeclaration(elementType) + ",";
  59. }
  60. result = result.Substring(0, result.Length - 1) + ")";
  61. return result;
  62. }
  63. else
  64. {
  65. string typeName = type.Name;
  66. typeName = typeName.Replace("[]", "()");
  67. return typeName;
  68. }
  69. }
  70. #endregion
  71. #region Public Methods
  72. public override string EmptyScript()
  73. {
  74. return
  75. @"Imports System
  76. Imports System.Collections
  77. Imports System.Collections.Generic
  78. Imports System.ComponentModel
  79. Imports System.Windows.Forms
  80. Imports System.Drawing
  81. Imports Microsoft.VisualBasic
  82. Imports FastReport
  83. Imports FastReport.Data
  84. Imports FastReport.Dialog
  85. Imports FastReport.Table
  86. Imports FastReport.Barcode
  87. Imports FastReport.Utils
  88. Namespace FastReport
  89. Public Class ReportScript
  90. End Class
  91. End Namespace
  92. ";
  93. }
  94. public override int GetPositionToInsertOwnItems(string scriptText)
  95. {
  96. int pos = scriptText.IndexOf("Public Class ReportScript");
  97. if (pos == -1)
  98. return -1;
  99. return scriptText.IndexOf('\n', pos) + 1;
  100. }
  101. public override string AddField(Type type, string name)
  102. {
  103. name = name.Replace(" ", "_");
  104. return " Public " + name + " as Global." + type.FullName + "\r\n";
  105. }
  106. public override string BeginCalcExpression()
  107. {
  108. return " Private Function CalcExpression(ByVal expression As String, ByVal Value as Global.FastReport.Variant) As Object\r\n ";
  109. }
  110. public override string AddExpression(string expr, string value)
  111. {
  112. expr = expr.Replace("\"", "\"\"");
  113. return "If expression = \"" + expr + "\" Then\r\n Return " + value + "\r\n End If\r\n ";
  114. }
  115. public override string EndCalcExpression()
  116. {
  117. return "Return Nothing\r\n End Function\r\n\r\n";
  118. }
  119. public override string ReplaceColumnName(string name, Type type)
  120. {
  121. string typeName = GetTypeDeclaration(type);
  122. string result = "CType(Report.GetColumnValue(\"" + name + "\"";
  123. result += "), " + typeName + ")";
  124. return result;
  125. }
  126. public override string ReplaceParameterName(Parameter parameter)
  127. {
  128. string typeName = GetTypeDeclaration(parameter.DataType);
  129. return "CType(Report.GetParameterValue(\"" + parameter.FullName + "\"), " + typeName + ")";
  130. }
  131. public override string ReplaceVariableName(Parameter parameter)
  132. {
  133. string typeName = GetTypeDeclaration(parameter.DataType);
  134. return "CType(Report.GetVariableValue(\"" + parameter.FullName + "\"), " + typeName + ")";
  135. }
  136. public override string ReplaceTotalName(string name)
  137. {
  138. return "Report.GetTotalValue(\"" + name + "\")";
  139. }
  140. public override string GenerateInitializeMethod()
  141. {
  142. Hashtable events = new Hashtable();
  143. string reportString = StripEventHandlers(events);
  144. string result = " Private Sub InitializeComponent\r\n ";
  145. // form the reportString
  146. result += "Dim reportString As String = _\r\n ";
  147. int totalLength = 0;
  148. while (reportString.Length > 0)
  149. {
  150. string part = "";
  151. if (reportString.Length > 80)
  152. {
  153. part = reportString.Substring(0, 80);
  154. reportString = reportString.Substring(80);
  155. }
  156. else
  157. {
  158. part = reportString;
  159. reportString = "";
  160. }
  161. part = "\"" + part.Replace("\"", "\"\"").Replace("\u201c", "\"\"").Replace("\u201d", "\"\"") + "\"";
  162. part = part.Replace("\r\n", "\" + ChrW(13) + ChrW(10) + \"");
  163. part = part.Replace("\r", "\" + ChrW(13) + \"");
  164. part = part.Replace("\n", "\" + ChrW(10) + \"");
  165. result += part;
  166. if (reportString != "")
  167. {
  168. if (totalLength > 1024)
  169. {
  170. totalLength = 0;
  171. result += "\r\n reportString = reportString + ";
  172. }
  173. else
  174. result += " + _\r\n ";
  175. totalLength += part.Length;
  176. }
  177. else
  178. {
  179. result += "\r\n ";
  180. }
  181. }
  182. result += "LoadFromString(reportString)\r\n ";
  183. result += "InternalInit()\r\n ";
  184. // form objects' event handlers
  185. foreach (DictionaryEntry de in events)
  186. {
  187. result += "AddHandler " + de.Key.ToString() + ", AddressOf " +
  188. de.Value.ToString() + "\r\n ";
  189. }
  190. result += "\r\n End Sub\r\n\r\n";
  191. result += " Public Sub New()\r\n InitializeComponent()\r\n End Sub\r\n";
  192. return result;
  193. }
  194. public override string ReplaceClassName(string scriptText, string className)
  195. {
  196. // replace the first occurence of "ReportScript"
  197. string replace = "Class ReportScript";
  198. int index = scriptText.IndexOf(replace);
  199. scriptText = scriptText.Remove(index, replace.Length);
  200. scriptText = scriptText.Insert(index, "Class " + className + "\r\n Inherits Report");
  201. // replace other items
  202. return scriptText.Replace("Private Function CalcExpression", "Protected Overrides Function CalcExpression");
  203. }
  204. public override string GetMethodSignature(MethodInfo info, bool fullForm)
  205. {
  206. string result = info.Name + "(";
  207. string fontBegin = "<font color=\"Blue\">";
  208. string fontEnd = "</font>";
  209. System.Reflection.ParameterInfo[] pars = info.GetParameters();
  210. foreach (System.Reflection.ParameterInfo par in pars)
  211. {
  212. // special case - skip "thisReport" parameter
  213. if (par.Name == "thisReport")
  214. continue;
  215. string modifier = "ByVal";
  216. if (par.IsOptional)
  217. modifier = "Optional " + modifier;
  218. object[] attr = par.GetCustomAttributes(typeof(ParamArrayAttribute), false);
  219. if (attr.Length > 0)
  220. modifier += " ParamArray";
  221. result += fullForm ? fontBegin + modifier + fontEnd + " " + par.Name + " " + fontBegin + "As" + fontEnd + " " : "";
  222. result += (fullForm ? fontBegin : "") + GetEquivalentKeyword(par.ParameterType.Name) + (fullForm ? fontEnd : "");
  223. #if DOTNET_4
  224. if (par.IsOptional && fullForm)
  225. result += CodeUtils.GetOptionalParameter(par, CodeUtils.Language.Vb);
  226. #endif
  227. result += ", ";
  228. }
  229. if (result.EndsWith(", "))
  230. result = result.Substring(0, result.Length - 2);
  231. result += ")";
  232. if (fullForm)
  233. result += " " + fontBegin + "As " + info.ReturnType.Name + fontEnd;
  234. return result;
  235. }
  236. public override string GetMethodSignatureAndBody(MethodInfo info)
  237. {
  238. string result = info.Name + "(";
  239. result = " Private Function " + result;
  240. System.Reflection.ParameterInfo[] pars = info.GetParameters();
  241. foreach (System.Reflection.ParameterInfo par in pars)
  242. {
  243. // special case - skip "thisReport" parameter
  244. if (par.Name == "thisReport")
  245. continue;
  246. string parName = "_" + par.Name;
  247. string modifier = "ByVal";
  248. if (par.IsOptional)
  249. modifier = "Optional " + modifier;
  250. object[] attr = par.GetCustomAttributes(typeof(ParamArrayAttribute), false);
  251. if (attr.Length > 0)
  252. modifier += " ParamArray";
  253. result += modifier + " " + parName + " As ";
  254. result += GetTypeDeclaration(par.ParameterType);
  255. #if DOTNET_4
  256. if (par.IsOptional)
  257. result += CodeUtils.GetOptionalParameter(par, CodeUtils.Language.Vb);
  258. #endif
  259. result += ", ";
  260. }
  261. if (result.EndsWith(", "))
  262. result = result.Substring(0, result.Length - 2);
  263. result += ")";
  264. result += " As " + GetTypeDeclaration(info.ReturnType);
  265. result += "\r\n";
  266. result += " Return Global." + info.ReflectedType.Namespace + "." +
  267. info.ReflectedType.Name + "." + info.Name + "(";
  268. foreach (System.Reflection.ParameterInfo par in pars)
  269. {
  270. string parName = "_" + par.Name;
  271. // special case - handle "thisReport" parameter
  272. if (parName == "_thisReport")
  273. parName = "Report";
  274. result += parName + ", ";
  275. }
  276. if (result.EndsWith(", "))
  277. result = result.Substring(0, result.Length - 2);
  278. result += ")\r\n";
  279. result += " End Function\r\n";
  280. result += "\r\n";
  281. return result;
  282. }
  283. public override string GetPropertySignature(PropertyInfo info, bool fullForm)
  284. {
  285. string result = GetEquivalentKeyword(info.PropertyType.Name) + " " + info.Name;
  286. if (fullForm)
  287. result += " { <font color=\"Blue\"> get;" + (info.CanWrite ? " set;" : "") + "</font> }";
  288. return result;
  289. }
  290. public override CodeDomProvider GetCodeProvider()
  291. {
  292. return new VBCodeProvider();
  293. }
  294. #endregion
  295. public VbCodeHelper(Report report) : base(report)
  296. {
  297. }
  298. }
  299. }