VbCodeHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "Imports System\r\nImports System.Collections\r\nImports System.Collections.Generic\r\n" +
  75. "Imports System.ComponentModel\r\nImports System.Windows.Forms\r\nImports System.Drawing\r\n" +
  76. "Imports Microsoft.VisualBasic\r\n" +
  77. "Imports FastReport\r\nImports FastReport.Data\r\nImports FastReport.Dialog\r\nImports FastReport.Table\r\n" +
  78. "Imports FastReport.Barcode\r\nImports FastReport.Utils\r\n\r\nNamespace FastReport\r\n" +
  79. " Public Class ReportScript\r\n\r\n End Class\r\nEnd Namespace\r\n";
  80. }
  81. public override int GetPositionToInsertOwnItems(string scriptText)
  82. {
  83. int pos = scriptText.IndexOf("Public Class ReportScript");
  84. if (pos == -1)
  85. return -1;
  86. return scriptText.IndexOf('\n', pos) + 1;
  87. }
  88. public override string AddField(Type type, string name)
  89. {
  90. name = name.Replace(" ", "_");
  91. return " Public " + name + " as Global." + type.FullName + "\r\n";
  92. }
  93. public override string BeginCalcExpression()
  94. {
  95. return " Private Function CalcExpression(ByVal expression As String, ByVal Value as Global.FastReport.Variant) As Object\r\n ";
  96. }
  97. public override string AddExpression(string expr, string value)
  98. {
  99. expr = expr.Replace("\"", "\"\"");
  100. return "If expression = \"" + expr + "\" Then\r\n Return " + value + "\r\n End If\r\n ";
  101. }
  102. public override string EndCalcExpression()
  103. {
  104. return "Return Nothing\r\n End Function\r\n\r\n";
  105. }
  106. public override string ReplaceColumnName(string name, Type type)
  107. {
  108. string typeName = GetTypeDeclaration(type);
  109. string result = "CType(Report.GetColumnValue(\"" + name + "\"";
  110. result += "), " + typeName + ")";
  111. return result;
  112. }
  113. public override string ReplaceParameterName(Parameter parameter)
  114. {
  115. string typeName = GetTypeDeclaration(parameter.DataType);
  116. return "CType(Report.GetParameterValue(\"" + parameter.FullName + "\"), " + typeName + ")";
  117. }
  118. public override string ReplaceVariableName(Parameter parameter)
  119. {
  120. string typeName = GetTypeDeclaration(parameter.DataType);
  121. return "CType(Report.GetVariableValue(\"" + parameter.FullName + "\"), " + typeName + ")";
  122. }
  123. public override string ReplaceTotalName(string name)
  124. {
  125. return "Report.GetTotalValue(\"" + name + "\")";
  126. }
  127. public override string GenerateInitializeMethod()
  128. {
  129. Hashtable events = new Hashtable();
  130. string reportString = StripEventHandlers(events);
  131. string result = " Private Sub InitializeComponent\r\n ";
  132. // form the reportString
  133. result += "Dim reportString As String = _\r\n ";
  134. int totalLength = 0;
  135. while (reportString.Length > 0)
  136. {
  137. string part = "";
  138. if (reportString.Length > 80)
  139. {
  140. part = reportString.Substring(0, 80);
  141. reportString = reportString.Substring(80);
  142. }
  143. else
  144. {
  145. part = reportString;
  146. reportString = "";
  147. }
  148. part = "\"" + part.Replace("\"", "\"\"").Replace("\u201c", "\"\"").Replace("\u201d", "\"\"") + "\"";
  149. part = part.Replace("\r\n", "\" + ChrW(13) + ChrW(10) + \"");
  150. part = part.Replace("\r", "\" + ChrW(13) + \"");
  151. part = part.Replace("\n", "\" + ChrW(10) + \"");
  152. result += part;
  153. if (reportString != "")
  154. {
  155. if (totalLength > 1024)
  156. {
  157. totalLength = 0;
  158. result += "\r\n reportString = reportString + ";
  159. }
  160. else
  161. result += " + _\r\n ";
  162. totalLength += part.Length;
  163. }
  164. else
  165. {
  166. result += "\r\n ";
  167. }
  168. }
  169. result += "LoadFromString(reportString)\r\n ";
  170. result += "InternalInit()\r\n ";
  171. // form objects' event handlers
  172. foreach (DictionaryEntry de in events)
  173. {
  174. result += "AddHandler " + de.Key.ToString() + ", AddressOf " +
  175. de.Value.ToString() + "\r\n ";
  176. }
  177. result += "\r\n End Sub\r\n\r\n";
  178. result += " Public Sub New()\r\n InitializeComponent()\r\n End Sub\r\n";
  179. return result;
  180. }
  181. public override string ReplaceClassName(string scriptText, string className)
  182. {
  183. // replace the first occurence of "ReportScript"
  184. string replace = "Class ReportScript";
  185. int index = scriptText.IndexOf(replace);
  186. scriptText = scriptText.Remove(index, replace.Length);
  187. scriptText = scriptText.Insert(index, "Class " + className + "\r\n Inherits Report");
  188. // replace other items
  189. return scriptText.Replace("Private Function CalcExpression", "Protected Overrides Function CalcExpression");
  190. }
  191. public override string GetMethodSignature(MethodInfo info, bool fullForm)
  192. {
  193. string result = info.Name + "(";
  194. string fontBegin = "<font color=\"Blue\">";
  195. string fontEnd = "</font>";
  196. System.Reflection.ParameterInfo[] pars = info.GetParameters();
  197. foreach (System.Reflection.ParameterInfo par in pars)
  198. {
  199. // special case - skip "thisReport" parameter
  200. if (par.Name == "thisReport")
  201. continue;
  202. string modifier = "ByVal";
  203. if (par.IsOptional)
  204. modifier = "Optional " + modifier;
  205. object[] attr = par.GetCustomAttributes(typeof(ParamArrayAttribute), false);
  206. if (attr.Length > 0)
  207. modifier += " ParamArray";
  208. result += fullForm ? fontBegin + modifier + fontEnd + " " + par.Name + " " + fontBegin + "As" + fontEnd + " " : "";
  209. result += (fullForm ? fontBegin : "") + GetEquivalentKeyword(par.ParameterType.Name) + (fullForm ? fontEnd : "");
  210. #if DOTNET_4
  211. if (par.IsOptional && fullForm)
  212. result += CodeUtils.GetOptionalParameter(par, CodeUtils.Language.Vb);
  213. #endif
  214. result += ", ";
  215. }
  216. if (result.EndsWith(", "))
  217. result = result.Substring(0, result.Length - 2);
  218. result += ")";
  219. if (fullForm)
  220. result += " " + fontBegin + "As " + info.ReturnType.Name + fontEnd;
  221. return result;
  222. }
  223. public override string GetMethodSignatureAndBody(MethodInfo info)
  224. {
  225. string result = info.Name + "(";
  226. result = " Private Function " + result;
  227. System.Reflection.ParameterInfo[] pars = info.GetParameters();
  228. foreach (System.Reflection.ParameterInfo par in pars)
  229. {
  230. // special case - skip "thisReport" parameter
  231. if (par.Name == "thisReport")
  232. continue;
  233. string parName = "_" + par.Name;
  234. string modifier = "ByVal";
  235. if (par.IsOptional)
  236. modifier = "Optional " + modifier;
  237. object[] attr = par.GetCustomAttributes(typeof(ParamArrayAttribute), false);
  238. if (attr.Length > 0)
  239. modifier += " ParamArray";
  240. result += modifier + " " + parName + " As ";
  241. result += GetTypeDeclaration(par.ParameterType);
  242. #if DOTNET_4
  243. if (par.IsOptional)
  244. result += CodeUtils.GetOptionalParameter(par, CodeUtils.Language.Vb);
  245. #endif
  246. result += ", ";
  247. }
  248. if (result.EndsWith(", "))
  249. result = result.Substring(0, result.Length - 2);
  250. result += ")";
  251. result += " As " + GetTypeDeclaration(info.ReturnType);
  252. result += "\r\n";
  253. result += " Return Global." + info.ReflectedType.Namespace + "." +
  254. info.ReflectedType.Name + "." + info.Name + "(";
  255. foreach (System.Reflection.ParameterInfo par in pars)
  256. {
  257. string parName = "_" + par.Name;
  258. // special case - handle "thisReport" parameter
  259. if (parName == "_thisReport")
  260. parName = "Report";
  261. result += parName + ", ";
  262. }
  263. if (result.EndsWith(", "))
  264. result = result.Substring(0, result.Length - 2);
  265. result += ")\r\n";
  266. result += " End Function\r\n";
  267. result += "\r\n";
  268. return result;
  269. }
  270. public override CodeDomProvider GetCodeProvider()
  271. {
  272. return new VBCodeProvider();
  273. }
  274. #endregion
  275. public VbCodeHelper(Report report) : base(report)
  276. {
  277. }
  278. }
  279. }