ModeTypeGenerator.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  114. {
  115. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  116. }
  117. else
  118. {
  119. if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  120. {
  121. var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  122. modelType.Index = index;
  123. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  124. }
  125. else
  126. {
  127. modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  128. }
  129. }
  130. if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
  131. {
  132. ServiceMap.TemporaryList.Add(modelType);
  133. }
  134. return modelType;
  135. }
  136. else
  137. {
  138. _tempTypes[type] = duplicatedTime;
  139. var modelType = new UserDefinedGenericModelType(type, argName);
  140. if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  141. {
  142. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  143. }
  144. else
  145. {
  146. if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  147. {
  148. var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  149. modelType.Index = index;
  150. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  151. }
  152. else
  153. {
  154. modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  155. }
  156. }
  157. if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
  158. {
  159. ServiceMap.TemporaryList.Add(modelType);
  160. }
  161. return modelType;
  162. }
  163. }
  164. else if(type==typeof(object))
  165. {
  166. throw new Exception("Object is not supported, it should be specific type.");
  167. }
  168. else if (type.BaseType != typeof(object))
  169. {
  170. _tempTypes[type] = duplicatedTime;
  171. var modelType = new UserDefinedDerivedModelType(type, argName);
  172. if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  173. {
  174. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  175. }
  176. else
  177. {
  178. if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  179. {
  180. var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  181. modelType.Index = index;
  182. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  183. }
  184. else
  185. {
  186. modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  187. }
  188. }
  189. if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
  190. {
  191. ServiceMap.TemporaryList.Add(modelType);
  192. }
  193. return modelType;
  194. }
  195. else
  196. {
  197. _tempTypes[type] = duplicatedTime;
  198. var modelType = new UserDefinedModeType(type, argName);
  199. if (!CodeGenerator.ConflictModelTypeList.ContainsKey(modelType.ParameterType.Name))
  200. {
  201. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name] = new Dictionary<ModelType, int>() { { modelType, 1 } };
  202. }
  203. else
  204. {
  205. if (!CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Any(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  206. {
  207. var index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].Count() + 1;
  208. modelType.Index = index;
  209. CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name][modelType] = index;
  210. }
  211. else
  212. {
  213. modelType.Index = CodeGenerator.ConflictModelTypeList[modelType.ParameterType.Name].FirstOrDefault(x => x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  214. }
  215. }
  216. if (!ServiceMap.TemporaryList.Any(x => x.ParameterType == type))
  217. {
  218. ServiceMap.TemporaryList.Add(modelType);
  219. }
  220. return modelType;
  221. }
  222. }
  223. else
  224. {
  225. var modelType = new ExtraObjectModelType(type, "");
  226. return modelType;
  227. }
  228. }
  229. }
  230. }