StringConvert.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*********************************************************************************
  2. *Author: OnClick
  3. *Version: 0.0.1
  4. *UnityVersion: 2018.3.11f1
  5. *Date: 2019-05-03
  6. *Description: IFramework
  7. *History: 2018.11--
  8. *********************************************************************************/
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. namespace IFramework.Serialization
  13. {
  14. #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释
  15. public static partial class StringConvert
  16. {
  17. private static Dictionary<Type, Type> _map = new Dictionary<Type, Type>()
  18. {
  19. { typeof(Byte),typeof(ByteStringConverter)},
  20. { typeof(Boolean),typeof(BoolStringConverter)},
  21. { typeof(Char),typeof(CharStringConverter)},
  22. { typeof(DateTime),typeof(DateTimeStringConverter)},
  23. { typeof(Decimal),typeof(DecimalStringConverter)},
  24. { typeof(Double),typeof(DoubleStringConverter)},
  25. { typeof(Single),typeof(FloatStringConverter)},
  26. { typeof(Int32),typeof(IntStringConverter)},
  27. { typeof(Int64),typeof(LongStringConverter)},
  28. { typeof(SByte),typeof(SByteStringConverter)},
  29. { typeof(Int16),typeof(ShortStringConverter)},
  30. { typeof(String),typeof(StringStringConverter)},
  31. { typeof(TimeSpan),typeof(TimeSpanStringConverter)},
  32. { typeof(UInt16),typeof(UInt16StringConverter)},
  33. { typeof(UInt32),typeof(UInt32StringConverter)},
  34. { typeof(UInt64),typeof(UInt64StringConverter)},
  35. };
  36. private static Dictionary<Type, StringConverter> _ins = new Dictionary<Type, StringConverter>();
  37. private static Dictionary<Type, Type> _fgenmap = new Dictionary<Type, Type>()
  38. {
  39. {(typeof(List<>)),typeof(ListFormatter<>) } ,
  40. {(typeof(Stack<>)),typeof(StackFormatter<>) } ,
  41. {(typeof(Queue<>)),typeof(QueueFormatter<>) } ,
  42. {(typeof(LinkedList<>)),typeof(LinkedListFormatter<>) } ,
  43. {(typeof(Dictionary<,>)),typeof(DictionaryFormatter<,>) } ,
  44. };
  45. private static Dictionary<Type, StringFormatter> _fins = new Dictionary<Type, StringFormatter>();
  46. public const char dot = ',';
  47. public const char leftBound = '{';
  48. public const char rightBound = '}';
  49. public const char midLeftBound = '[';
  50. public const char midRightBound = ']';
  51. public const char colon = ':';
  52. public static string ConvertToString<T>(T self)
  53. {
  54. return ConvertToString(self, typeof(T));
  55. }
  56. public static string ConvertToString(object self, Type type)
  57. {
  58. if (self == null) return string.Empty;
  59. StringBuilder builder = new StringBuilder();
  60. var s = GetFormatter(type);
  61. s.ConvertToString(self, builder);
  62. return builder.ToString();
  63. }
  64. public static bool TryConvert<T>(string self, out T t)
  65. {
  66. object t1 = null;
  67. if (TryConvert(self, typeof(T), ref t1))
  68. {
  69. t = (T)t1;
  70. return true;
  71. }
  72. t = default(T);
  73. return false;
  74. }
  75. public static bool TryConvert(string self, Type type, ref object obj)
  76. {
  77. if (string.IsNullOrEmpty(self)) return false;
  78. self = self.Replace(" ", "").Replace("\r\n", "\n").Replace("\n", "");
  79. return GetFormatter(type).TryConvertObject(self, out obj);
  80. }
  81. public static StringConverter GetConverter(Type type)
  82. {
  83. StringConverter c;
  84. if (!_ins.TryGetValue(type, out c))
  85. {
  86. Type t;
  87. if (!_map.TryGetValue(type, out t))
  88. {
  89. if (type.IsEnum)
  90. c = Activator.CreateInstance(typeof(EnumStringConverter<>).MakeGenericType(type)) as StringConverter;
  91. }
  92. else
  93. {
  94. c = CreateConverter(t);
  95. }
  96. if (c != null)
  97. {
  98. _ins.Add(type, c);
  99. }
  100. else
  101. {
  102. throw new Exception($"None StringConverter with Type {type}");
  103. }
  104. }
  105. return c;
  106. }
  107. private static StringConverter CreateConverter(Type type)
  108. {
  109. return Activator.CreateInstance(type) as StringConverter;
  110. }
  111. private static StringFormatter CreateFormatter(Type type)
  112. {
  113. if (type.IsEnum || _map.ContainsKey(type))
  114. return Activator.CreateInstance(typeof(BaseTypeFormatter<>).MakeGenericType(type)) as StringFormatter;
  115. if (type.IsArray)
  116. return Activator.CreateInstance(typeof(ArrayFormatter<>).MakeGenericType(type.GetElementType())) as StringFormatter;
  117. if (type.IsGenericType)
  118. {
  119. foreach (var item in _fgenmap.Keys)
  120. {
  121. if (type.IsSubclassOfGeneric(item))
  122. {
  123. return Activator.CreateInstance(_fgenmap[item].MakeGenericType(type.GetGenericArguments())) as StringFormatter;
  124. }
  125. }
  126. }
  127. return Activator.CreateInstance(typeof(ObjectFormatter<>).MakeGenericType(type)) as StringFormatter;
  128. }
  129. public static StringFormatter GetFormatter(Type type)
  130. {
  131. StringFormatter c;
  132. if (!_fins.TryGetValue(type, out c))
  133. {
  134. c = CreateFormatter(type);
  135. if (c != null)
  136. {
  137. _fins.Add(type, c);
  138. }
  139. else
  140. {
  141. throw new Exception($"None StringFormatter with Type {type}");
  142. }
  143. }
  144. return c;
  145. }
  146. public static void SubscribeConverter<T>(StringConverter converter)
  147. {
  148. if (!_map.ContainsKey(typeof(T)))
  149. {
  150. _ins.Add(typeof(T), converter);
  151. }
  152. else
  153. {
  154. _ins[typeof(T)] = converter;
  155. }
  156. }
  157. public static void SubscribeFormatter<T>(StringFormatter converter)
  158. {
  159. if (!_map.ContainsKey(typeof(T)))
  160. {
  161. _fins.Add(typeof(T), converter);
  162. }
  163. else
  164. {
  165. _fins[typeof(T)] = converter;
  166. }
  167. }
  168. public static void SubscribeGenericFormatter<T>(Type converter)
  169. {
  170. if (!_map.ContainsKey(typeof(T)))
  171. {
  172. _fgenmap.Add(typeof(T), converter);
  173. }
  174. else
  175. {
  176. _fgenmap[typeof(T)] = converter;
  177. }
  178. }
  179. }
  180. #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释
  181. }