ModeTypeGenerator.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace FlutterCodeGenerator
  5. {
  6. public class ModelTypeGenerator
  7. {
  8. public static Dictionary<Type, int> _tempTypes = new Dictionary<Type, int>();
  9. public static ModelType Create(Type type, string argName, bool clearTypes = false)
  10. {
  11. if (clearTypes)
  12. {
  13. _tempTypes.Clear();
  14. }
  15. if ((_tempTypes.ContainsKey(type) && _tempTypes[type] < 2) || !_tempTypes.ContainsKey(type))
  16. {
  17. var duplicatedTime = 0;
  18. if (_tempTypes.ContainsKey(type))
  19. {
  20. duplicatedTime = _tempTypes[type];
  21. }
  22. duplicatedTime++;
  23. if (type == typeof(int) || type == typeof(long) || type == typeof(short))
  24. {
  25. return new IntModeType(argName);
  26. }
  27. else if (type == typeof(int?) || type == typeof(long?) || type == typeof(short?))
  28. {
  29. return new IntNullableModeType(argName);
  30. }
  31. else if (type == typeof(float) || type == typeof(double))
  32. {
  33. return new DoubleModeType(argName);
  34. }
  35. else if (type == typeof(float?) || type == typeof(double?))
  36. {
  37. return new DoubleNullableModeType(argName);
  38. }
  39. else if (type == typeof(bool))
  40. {
  41. return new BoolModeType(argName);
  42. }
  43. else if (type == typeof(bool?))
  44. {
  45. return new BoolNullableModeType(argName);
  46. }
  47. else if (type == typeof(string) || type == typeof(char) || type.BaseType?.Name == "BaseParamsString" || type.BaseType?.Name == "BaseString")
  48. {
  49. return new StringModeType(argName);
  50. }
  51. else if (type == typeof(DateTime) || type == typeof(DateTime?))
  52. {
  53. return new DateTimeModeType(argName);
  54. }
  55. else if (type == typeof(byte))
  56. {
  57. return new ByteModelType(argName);
  58. }
  59. else if (type == typeof(byte?))
  60. {
  61. return new ByteNullableModelType(argName);
  62. }
  63. else if (type == typeof(void))
  64. {
  65. return new VoidModelType(argName);
  66. }
  67. else if (type.IsEnum)
  68. {
  69. var modelType = new EnumModeType(type, argName);
  70. if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  71. {
  72. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  73. }
  74. else
  75. {
  76. if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  77. {
  78. var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  79. modelType.Index = index;
  80. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  81. }
  82. else
  83. {
  84. modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  85. }
  86. }
  87. if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
  88. {
  89. ServiceMap.TemporaryList.Add(modelType);
  90. }
  91. return modelType;
  92. }
  93. else if (type.IsArray)
  94. {
  95. var modelType = new ArrayModelType(type, argName);
  96. return modelType;
  97. }
  98. else if (type.IsGenericType)
  99. {
  100. if (type.Name == "List`1" || type.Name=="IList`1"|| type.Name == "IEnumerable`1")
  101. {
  102. var modelType = new ListModelType(type, argName);
  103. return modelType;
  104. }
  105. else if (type.Name == "Dictionary`2")
  106. {
  107. var modelType = new DictionaryModelType(type, argName);
  108. return modelType;
  109. }
  110. else if (type.Name == "Nullable`1")
  111. {
  112. var modelType = new EnumNullableModelType(type.GetGenericArguments()[0], argName);
  113. return modelType;
  114. }
  115. else
  116. {
  117. _tempTypes[type] = duplicatedTime;
  118. var modelType = new UserDefinedGenericModelType(type, argName);
  119. if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  120. {
  121. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  122. }
  123. else
  124. {
  125. if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  126. {
  127. var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  128. modelType.Index = index;
  129. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  130. }
  131. else
  132. {
  133. modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  134. }
  135. }
  136. if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
  137. {
  138. ServiceMap.TemporaryList.Add(modelType);
  139. }
  140. return modelType;
  141. }
  142. }
  143. else if (type.BaseType != typeof(object))
  144. {
  145. _tempTypes[type] = duplicatedTime;
  146. var modelType = new UserDefinedDerivedModelType(type, argName);
  147. if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  148. {
  149. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  150. }
  151. else
  152. {
  153. if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  154. {
  155. var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  156. modelType.Index = index;
  157. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  158. }
  159. else
  160. {
  161. modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  162. }
  163. }
  164. if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
  165. {
  166. ServiceMap.TemporaryList.Add(modelType);
  167. }
  168. return modelType;
  169. }
  170. else
  171. {
  172. _tempTypes[type] = duplicatedTime;
  173. var modelType = new UserDefinedModeType(type, argName);
  174. if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  175. {
  176. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  177. }
  178. else
  179. {
  180. if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  181. {
  182. var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  183. modelType.Index = index;
  184. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  185. }
  186. else
  187. {
  188. modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  189. }
  190. }
  191. if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
  192. {
  193. ServiceMap.TemporaryList.Add(modelType);
  194. }
  195. return modelType;
  196. }
  197. }
  198. else
  199. {
  200. var modelType = new ExtraObjectModelType(type, "");
  201. return modelType;
  202. }
  203. }
  204. }
  205. }