CodeGenerator.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace IntelligentControlForsx.CodeGenerator
  7. {
  8. public class CodeGenerator
  9. {
  10. public static string Generate(string clsName, string[] varArray)
  11. {
  12. StringBuilder sbVarDelare = new StringBuilder();
  13. for(int i=1;i<varArray.Length;i++)
  14. {
  15. string varDelareField = string.Format(VarDeclare, i, varArray[i]);
  16. sbVarDelare.AppendLine(varDelareField);
  17. }
  18. StringBuilder sbAssignment = new StringBuilder();
  19. for (int i = 1; i < varArray.Length; i++)
  20. {
  21. string varDelareField = string.Format(Assignment, i-1, varArray[i]);
  22. sbAssignment.AppendLine(varDelareField);
  23. }
  24. return string.Format(Outline, clsName, varArray[0],
  25. sbVarDelare.ToString(), sbAssignment.ToString());
  26. }
  27. #region 代码模版
  28. public const string Outline = @"
  29. using System;
  30. using System.ComponentModel;
  31. using System.Runtime.CompilerServices;
  32. namespace GdxnyWindTurbineContorls.CodeGenerator
  33. {{
  34. public class {0} : TableModel
  35. {{
  36. public {0}(string deviceName) : base(deviceName)
  37. {{
  38. this.deviceId = deviceName;
  39. }}
  40. public string {1}
  41. {{
  42. get {{ return deviceId;}}
  43. set {{
  44. if (value != this.deviceId)
  45. {{
  46. this.deviceId = value;
  47. NotifyPropertyChanged();
  48. }}
  49. }}
  50. }}
  51. {2}
  52. public override void bindingData(double[] vals)
  53. {{
  54. {3}
  55. }}
  56. }}
  57. }}
  58. ";
  59. public const string VarDeclare = @"
  60. private string var{0} =""0.00"";
  61. public string {1}
  62. {{
  63. get {{ return var{0}; }}
  64. set
  65. {{
  66. if (value != this.var{0})
  67. {{
  68. this.var{0} = value;
  69. NotifyPropertyChanged();
  70. }}
  71. }}
  72. }}
  73. ";
  74. public const string Assignment = "this.{1} = vals[{0}].ToString(\"f2\");";
  75. #endregion
  76. }
  77. }