TypeExtension.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. using SqlSugar;
  2. using System.Collections.Concurrent;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Globalization;
  6. using System.Numerics;
  7. using System.Reflection;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. namespace ServiceCenter.Extensions
  12. {
  13. /// <summary>
  14. /// 类型转换扩展
  15. /// </summary>
  16. public static class TypeExtension
  17. {
  18. /// <summary>
  19. /// 将字符串转换为short
  20. /// </summary>
  21. /// <param name="value">需要转换的字符串</param>
  22. /// <returns></returns>
  23. public static short ToShort(this string value)
  24. {
  25. return Convert.ToInt16(value);
  26. }
  27. /// <summary>
  28. /// 将int转换为short
  29. /// </summary>
  30. /// <param name="value">需要转换的字符串</param>
  31. /// <returns></returns>
  32. public static short ToShort(this int value)
  33. {
  34. return Convert.ToInt16(value);
  35. }
  36. /// <summary>
  37. /// 将decimal转换为short
  38. /// </summary>
  39. /// <param name="value">需要转换的字符串</param>
  40. /// <returns></returns>
  41. public static short ToShort(this decimal value)
  42. {
  43. return Convert.ToInt16(value);
  44. }
  45. /// <summary>
  46. /// 将字符串转换为int
  47. /// </summary>
  48. /// <param name="value">需要转换的字符串</param>
  49. /// <returns></returns>
  50. public static int ToInt(this string value)
  51. {
  52. return Convert.ToInt32(value);
  53. }
  54. /// <summary>
  55. /// 获取最后一位数字
  56. /// </summary>
  57. /// <param name="value">字符串</param>
  58. /// <returns></returns>
  59. public static int GetLastDigit(this string value)
  60. {
  61. return Convert.ToInt32(Regex.Match(value, @"\d+$").Value);
  62. }
  63. /// <summary>
  64. /// 判断值为奇数/偶数
  65. /// </summary>
  66. /// <param name="value">需要判断的值</param>
  67. /// <returns> true:是奇数 false:是偶数</returns>
  68. public static bool OddNumberOrEven(this short value)
  69. {
  70. return value % 2 != 0;
  71. }
  72. /// <summary>
  73. /// 获取short类型Code,只限设备组
  74. /// </summary>
  75. /// <param name="value"></param>
  76. /// <returns></returns>
  77. public static short GetShortCode(this string value)
  78. {
  79. return value.Replace("G", "").ToShort();
  80. }
  81. /// <summary>
  82. /// 扩展方法,获得枚举的Description
  83. /// </summary>
  84. /// <param name="value">枚举值</param>
  85. /// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
  86. /// <returns>枚举的Description</returns>
  87. public static string GetDescription(this Enum value, Boolean nameInstead = true)
  88. {
  89. Type type = value.GetType();
  90. string name = Enum.GetName(type, value);
  91. if (name == null)
  92. {
  93. return null;
  94. }
  95. FieldInfo field = type.GetField(name);
  96. DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
  97. if (attribute == null && nameInstead == true)
  98. {
  99. return name;
  100. }
  101. return attribute?.Description;
  102. }
  103. /// <summary>
  104. /// 获取属性的描述信息
  105. /// </summary>
  106. public static string GetDescription(this Type type, string proName)
  107. {
  108. PropertyInfo pro = type.GetProperty(proName);
  109. string des = proName;
  110. if (pro != null)
  111. {
  112. des = pro.GetDescription();
  113. }
  114. return des;
  115. }
  116. /// <summary>
  117. /// 获取属性的描述信息
  118. /// </summary>
  119. public static string GetDescription(this MemberInfo info)
  120. {
  121. var attrs = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
  122. string des = info.Name;
  123. foreach (DescriptionAttribute attr in attrs)
  124. {
  125. des = attr.Description;
  126. }
  127. return des;
  128. }
  129. /// <summary>
  130. /// 数据映射
  131. /// </summary>
  132. /// <typeparam name="D"></typeparam>
  133. /// <typeparam name="S"></typeparam>
  134. /// <param name="s"></param>
  135. /// <returns></returns>
  136. public static D Mapper<D, S>(S s)
  137. {
  138. var d = Activator.CreateInstance<D>();
  139. var sType = s?.GetType();
  140. var dType = typeof(D);
  141. foreach (var sP in sType.GetProperties())
  142. {
  143. foreach (var dP in dType.GetProperties())
  144. {
  145. if (dP.Name == sP.Name)
  146. {
  147. dP.SetValue(d, sP.GetValue(s));
  148. break;
  149. }
  150. }
  151. }
  152. return d;
  153. }
  154. /// <summary>
  155. /// 获取字典
  156. /// </summary>
  157. /// <typeparam name="T1"></typeparam>
  158. /// <typeparam name="T2"></typeparam>
  159. /// <typeparam name="T3"></typeparam>
  160. /// <param name="t3"></param>
  161. /// <returns></returns>
  162. public static Dictionary<string, object> EntityClassToDictionary<T>(T t)
  163. {
  164. var type = typeof(SugarColumn);
  165. Dictionary<string, object> d = new Dictionary<string, object>();
  166. var sType = t.GetType();
  167. foreach (var sP in sType.GetProperties())
  168. {
  169. if (sP.CustomAttributes.Any(v => v.AttributeType == type) && sP.Name != "VER" && sP.Name != "ID")
  170. {
  171. d.Add(sP.Name, sP.GetValue(t));
  172. }
  173. }
  174. return d;
  175. }
  176. /// <summary>
  177. /// 获取MD5字符串
  178. /// </summary>
  179. /// <param name="myString"></param>
  180. /// <returns></returns>
  181. public static string GetMD5(this string myString)
  182. {
  183. var md5 = MD5.Create();
  184. var fromData = Encoding.Unicode.GetBytes(myString);
  185. var targetData = md5.ComputeHash(fromData);
  186. string byte2String = null;
  187. for (var i = 0; i < targetData.Length; i++)
  188. {
  189. byte2String += targetData[i].ToString("x");
  190. }
  191. return byte2String;
  192. }
  193. /// <summary>
  194. /// DataTable转换成实体类
  195. /// </summary>
  196. /// <typeparam name="T"></typeparam>
  197. /// <param name="dt"></param>
  198. /// <returns></returns>
  199. public static List<object> TableToEntity(this DataTable dt, string typeName)
  200. {
  201. var list = new List<object>();
  202. try
  203. {
  204. foreach (DataRow row in dt.Rows)
  205. {
  206. var entity = Type.GetType(typeName);
  207. PropertyInfo[] pArray = entity.GetType().GetProperties();
  208. foreach (var p in pArray)
  209. {
  210. if (dt.Columns.Contains(p.Name))
  211. {
  212. if (!p.CanWrite) continue;
  213. var value = row[p.Name];
  214. if (value != DBNull.Value)
  215. {
  216. var targetType = p.PropertyType;
  217. var convertType = targetType;
  218. if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  219. {
  220. //可空类型
  221. var nullableConverter = new NullableConverter(targetType);
  222. convertType = nullableConverter.UnderlyingType;
  223. }
  224. if (!string.IsNullOrEmpty(convertType.FullName) && !string.IsNullOrEmpty(value.ToString()))
  225. {
  226. value = Convert.ChangeType(value, convertType);
  227. }
  228. switch (convertType.FullName)
  229. {
  230. case "System.Decimal":
  231. p.SetValue(entity, Convert.ToDecimal(value), null);
  232. break;
  233. case "System.String":
  234. p.SetValue(entity, Convert.ToString(value), null);
  235. break;
  236. case "System.Int32":
  237. p.SetValue(entity, Convert.ToInt32(value), null);
  238. break;
  239. case "System.Int64":
  240. p.SetValue(entity, Convert.ToInt64(value), null);
  241. break;
  242. case "System.Int16":
  243. p.SetValue(entity, Convert.ToInt16(value), null);
  244. break;
  245. case "System.Double":
  246. p.SetValue(entity, Convert.ToDouble(value), null);
  247. break;
  248. case "System.Single":
  249. p.SetValue(entity, Convert.ToSingle(value), null);
  250. break;
  251. case "System.DateTime":
  252. p.SetValue(entity, Convert.ToDateTime(value), null);
  253. break;
  254. default:
  255. p.SetValue(entity, value, null);
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. list.Add(entity);
  262. }
  263. }
  264. catch (Exception ex)
  265. {
  266. }
  267. return list;
  268. }
  269. /// <summary>
  270. /// 转换到目标类型
  271. /// </summary>
  272. /// <param name="value"></param>
  273. /// <typeparam name="T"></typeparam>
  274. /// <returns></returns>
  275. internal static T ConvertTo<T>(this object value) => (T)typeof(T).FromObject(value);
  276. public static string yyyy(this DateTime time)
  277. {
  278. return time.GetFormat(GetFormatterEnum.yyyy);
  279. }
  280. public static string yyyyMM(this DateTime time)
  281. {
  282. return time.GetFormat(GetFormatterEnum.yyyyMM);
  283. }
  284. public static string yyyyMMdd(this DateTime time)
  285. {
  286. return time.GetFormat(GetFormatterEnum.yyyyMMdd);
  287. }
  288. public static string yyyyMMddhh(this DateTime time)
  289. {
  290. return time.GetFormat(GetFormatterEnum.yyyyMMddhh);
  291. }
  292. public static string yyyyMMddhhmm(this DateTime time)
  293. {
  294. return time.GetFormat(GetFormatterEnum.yyyyMMddhhmm);
  295. }
  296. public static string yyyyMMddhhmmss(this DateTime time)
  297. {
  298. return time.GetFormat(GetFormatterEnum.yyyyMMddhhmmss);
  299. }
  300. public static string yyyyMMddhhmmssf(this DateTime time)
  301. {
  302. return time.GetFormat(GetFormatterEnum.yyyyMMddhhmmssfffffff);
  303. }
  304. /// <summary>
  305. /// 获取指定格式时间的字符串
  306. /// </summary>
  307. /// <param name="time">时间</param>
  308. /// <param name="formatterEnum">类型</param>
  309. /// <returns></returns>
  310. public static string GetFormat(this DateTime time, GetFormatterEnum formatterEnum)
  311. {
  312. switch (formatterEnum)
  313. {
  314. case GetFormatterEnum.Default:
  315. return time.ToString();
  316. case GetFormatterEnum.yyyy:
  317. return time.ToString("yyyy");
  318. case GetFormatterEnum.yyyyMM:
  319. return time.ToString("yyyy-MM");
  320. case GetFormatterEnum.yyyyMMdd:
  321. return time.ToString("yyyy-MM-dd");
  322. case GetFormatterEnum.yyyyMMddhh:
  323. return time.ToString("yyyy-MM-dd HH");
  324. case GetFormatterEnum.yyyyMMddhhmm:
  325. return time.ToString("yyyy-MM-dd HH:mm");
  326. case GetFormatterEnum.yyyyMMddhhmmss:
  327. return time.ToString("yyyy-MM-dd HH:mm:ss");
  328. case GetFormatterEnum.yyyyMMddhhmmssfffffff:
  329. return time.ToString("yyyy-MM-dd HH:mm:ss:fffffff");
  330. default:
  331. return time.ToString();
  332. }
  333. }
  334. private static ConcurrentDictionary<Type, Func<string, object>> _dicFromObject = new ConcurrentDictionary<Type, Func<string, object>>();
  335. public static object FromObject(this Type targetType, object value, Encoding encoding = null)
  336. {
  337. if (targetType == typeof(object)) return value;
  338. if (encoding == null) encoding = Encoding.UTF8;
  339. var valueIsNull = value == null;
  340. var valueType = valueIsNull ? typeof(string) : value.GetType();
  341. if (valueType == targetType) return value;
  342. if (valueType == typeof(byte[])) //byte[] -> guid
  343. {
  344. if (targetType == typeof(Guid))
  345. {
  346. var bytes = value as byte[];
  347. return Guid.TryParse(BitConverter.ToString(bytes, 0, Math.Min(bytes.Length, 36)).Replace("-", ""), out var tryguid) ? tryguid : Guid.Empty;
  348. }
  349. if (targetType == typeof(Guid?))
  350. {
  351. var bytes = value as byte[];
  352. return Guid.TryParse(BitConverter.ToString(bytes, 0, Math.Min(bytes.Length, 36)).Replace("-", ""), out var tryguid) ? (Guid?)tryguid : null;
  353. }
  354. }
  355. if (targetType == typeof(byte[])) //guid -> byte[]
  356. {
  357. if (valueIsNull) return null;
  358. if (valueType == typeof(Guid) || valueType == typeof(Guid?))
  359. {
  360. var bytes = new byte[16];
  361. var guidN = ((Guid)value).ToString("N");
  362. for (var a = 0; a < guidN.Length; a += 2)
  363. bytes[a / 2] = byte.Parse($"{guidN[a]}{guidN[a + 1]}", NumberStyles.HexNumber);
  364. return bytes;
  365. }
  366. return encoding.GetBytes(value.ToInvariantCultureToString());
  367. }
  368. else if (targetType.IsArray)
  369. {
  370. if (value is Array valueArr)
  371. {
  372. var targetElementType = targetType.GetElementType();
  373. var sourceArrLen = valueArr.Length;
  374. var target = Array.CreateInstance(targetElementType, sourceArrLen);
  375. for (var a = 0; a < sourceArrLen; a++) target.SetValue(targetElementType.FromObject(valueArr.GetValue(a), encoding), a);
  376. return target;
  377. }
  378. //if (value is IList valueList)
  379. //{
  380. // var targetElementType = targetType.GetElementType();
  381. // var sourceArrLen = valueList.Count;
  382. // var target = Array.CreateInstance(targetElementType, sourceArrLen);
  383. // for (var a = 0; a < sourceArrLen; a++) target.SetValue(targetElementType.FromObject(valueList[a], encoding), a);
  384. // return target;
  385. //}
  386. }
  387. var func = _dicFromObject.GetOrAdd(targetType, tt =>
  388. {
  389. if (tt == typeof(object)) return vs => vs;
  390. if (tt == typeof(string)) return vs => vs;
  391. if (tt == typeof(char[])) return vs => vs == null ? null : vs.ToCharArray();
  392. if (tt == typeof(char)) return vs => vs == null ? default(char) : vs.ToCharArray(0, 1).FirstOrDefault();
  393. if (tt == typeof(bool)) return vs =>
  394. {
  395. if (vs == null) return false;
  396. switch (vs.ToLower())
  397. {
  398. case "true":
  399. case "1":
  400. return true;
  401. }
  402. return false;
  403. };
  404. if (tt == typeof(bool?)) return vs =>
  405. {
  406. if (vs == null) return false;
  407. switch (vs.ToLower())
  408. {
  409. case "true":
  410. case "1":
  411. return true;
  412. case "false":
  413. case "0":
  414. return false;
  415. }
  416. return null;
  417. };
  418. if (tt == typeof(byte)) return vs => vs == null ? 0 : byte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  419. if (tt == typeof(byte?)) return vs => vs == null ? null : byte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (byte?)tryval : null;
  420. if (tt == typeof(decimal)) return vs => vs == null ? 0 : decimal.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  421. if (tt == typeof(decimal?)) return vs => vs == null ? null : decimal.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (decimal?)tryval : null;
  422. if (tt == typeof(double)) return vs => vs == null ? 0 : double.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  423. if (tt == typeof(double?)) return vs => vs == null ? null : double.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (double?)tryval : null;
  424. if (tt == typeof(float)) return vs => vs == null ? 0 : float.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  425. if (tt == typeof(float?)) return vs => vs == null ? null : float.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (float?)tryval : null;
  426. if (tt == typeof(int)) return vs => vs == null ? 0 : int.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  427. if (tt == typeof(int?)) return vs => vs == null ? null : int.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (int?)tryval : null;
  428. if (tt == typeof(long)) return vs => vs == null ? 0 : long.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  429. if (tt == typeof(long?)) return vs => vs == null ? null : long.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (long?)tryval : null;
  430. if (tt == typeof(sbyte)) return vs => vs == null ? 0 : sbyte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  431. if (tt == typeof(sbyte?)) return vs => vs == null ? null : sbyte.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (sbyte?)tryval : null;
  432. if (tt == typeof(short)) return vs => vs == null ? 0 : short.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  433. if (tt == typeof(short?)) return vs => vs == null ? null : short.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (short?)tryval : null;
  434. if (tt == typeof(uint)) return vs => vs == null ? 0 : uint.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  435. if (tt == typeof(uint?)) return vs => vs == null ? null : uint.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (uint?)tryval : null;
  436. if (tt == typeof(ulong)) return vs => vs == null ? 0 : ulong.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  437. if (tt == typeof(ulong?)) return vs => vs == null ? null : ulong.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (ulong?)tryval : null;
  438. if (tt == typeof(ushort)) return vs => vs == null ? 0 : ushort.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  439. if (tt == typeof(ushort?)) return vs => vs == null ? null : ushort.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (ushort?)tryval : null;
  440. if (tt == typeof(DateTime)) return vs => vs == null ? DateTime.MinValue : DateTime.TryParse(vs, out var tryval) ? tryval : DateTime.MinValue;
  441. if (tt == typeof(DateTime?)) return vs => vs == null ? null : DateTime.TryParse(vs, out var tryval) ? (DateTime?)tryval : null;
  442. if (tt == typeof(DateTimeOffset)) return vs => vs == null ? DateTimeOffset.MinValue : DateTimeOffset.TryParse(vs, out var tryval) ? tryval : DateTimeOffset.MinValue;
  443. if (tt == typeof(DateTimeOffset?)) return vs => vs == null ? null : DateTimeOffset.TryParse(vs, out var tryval) ? (DateTimeOffset?)tryval : null;
  444. if (tt == typeof(TimeSpan)) return vs => vs == null ? TimeSpan.Zero : TimeSpan.TryParse(vs, out var tryval) ? tryval : TimeSpan.Zero;
  445. if (tt == typeof(TimeSpan?)) return vs => vs == null ? null : TimeSpan.TryParse(vs, out var tryval) ? (TimeSpan?)tryval : null;
  446. if (tt == typeof(Guid)) return vs => vs == null ? Guid.Empty : Guid.TryParse(vs, out var tryval) ? tryval : Guid.Empty;
  447. if (tt == typeof(Guid?)) return vs => vs == null ? null : Guid.TryParse(vs, out var tryval) ? (Guid?)tryval : null;
  448. if (tt == typeof(BigInteger)) return vs => vs == null ? 0 : BigInteger.TryParse(vs, NumberStyles.Any, null, out var tryval) ? tryval : 0;
  449. if (tt == typeof(BigInteger?)) return vs => vs == null ? null : BigInteger.TryParse(vs, NumberStyles.Any, null, out var tryval) ? (BigInteger?)tryval : null;
  450. if (tt.NullableTypeOrThis().IsEnum)
  451. {
  452. var tttype = tt.NullableTypeOrThis();
  453. var ttdefval = tt.CreateInstanceGetDefaultValue();
  454. return vs =>
  455. {
  456. if (string.IsNullOrWhiteSpace(vs)) return ttdefval;
  457. return Enum.Parse(tttype, vs, true);
  458. };
  459. }
  460. var localTargetType = targetType;
  461. var localValueType = valueType;
  462. return vs =>
  463. {
  464. if (vs == null) return null;
  465. throw new NotSupportedException($"convert failed {localValueType.DisplayCsharp()} -> {localTargetType.DisplayCsharp()}");
  466. };
  467. });
  468. var valueStr = valueIsNull ? null : valueType == typeof(byte[]) ? encoding.GetString(value as byte[]) : value.ToInvariantCultureToString();
  469. return func(valueStr);
  470. }
  471. internal static string ToInvariantCultureToString(this object obj) => obj is string objstr ? objstr : string.Format(CultureInfo.InvariantCulture, @"{0}", obj);
  472. private static bool IsNullableType(this Type that) => that.IsArray == false && that?.FullName.StartsWith("System.Nullable`1[") == true;
  473. private static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
  474. internal static object CreateInstanceGetDefaultValue(this Type that)
  475. {
  476. if (that == null) return null;
  477. if (that == typeof(string)) return default(string);
  478. if (that == typeof(Guid)) return default(Guid);
  479. if (that == typeof(byte[])) return default(byte[]);
  480. if (that.IsArray) return Array.CreateInstance(that.GetElementType(), 0);
  481. if (that.IsInterface || that.IsAbstract) return null;
  482. var ctorParms = that.InternalGetTypeConstructor0OrFirst(false)?.GetParameters();
  483. if (ctorParms == null || ctorParms.Any() == false) return Activator.CreateInstance(that, true);
  484. return Activator.CreateInstance(that, ctorParms.Select(a => a.ParameterType.IsInterface ||
  485. a.ParameterType.IsAbstract ||
  486. a.ParameterType == typeof(string) ||
  487. a.ParameterType.IsArray ? null : Activator.CreateInstance(a.ParameterType, null)).ToArray());
  488. }
  489. private static ConcurrentDictionary<Type, ConstructorInfo> _dicInternalGetTypeConstructor0OrFirst = new ConcurrentDictionary<Type, ConstructorInfo>();
  490. private static ConstructorInfo InternalGetTypeConstructor0OrFirst(this Type that, bool isThrow = true)
  491. {
  492. var ret = _dicInternalGetTypeConstructor0OrFirst.GetOrAdd(that, tp =>
  493. tp.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null) ??
  494. tp.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault());
  495. if (ret == null && isThrow) throw new ArgumentException($"{that.FullName} has no method to access constructor");
  496. return ret;
  497. }
  498. private static string DisplayCsharp(this Type type, bool isNameSpace = true)
  499. {
  500. if (type == null) return null;
  501. if (type == typeof(void)) return "void";
  502. if (type.IsGenericParameter) return type.Name;
  503. if (type.IsArray) return $"{DisplayCsharp(type.GetElementType())}[]";
  504. var sb = new StringBuilder();
  505. var nestedType = type;
  506. while (nestedType.IsNested)
  507. {
  508. sb.Insert(0, ".").Insert(0, DisplayCsharp(nestedType.DeclaringType, false));
  509. nestedType = nestedType.DeclaringType;
  510. }
  511. if (isNameSpace && string.IsNullOrWhiteSpace(nestedType.Namespace) == false)
  512. sb.Insert(0, ".").Insert(0, nestedType.Namespace);
  513. if (type.IsGenericType == false)
  514. return sb.Append(type.Name).ToString();
  515. var genericParameters = type.GetGenericArguments();
  516. if (type.IsNested && type.DeclaringType.IsGenericType)
  517. {
  518. var dic = genericParameters.ToDictionary(a => a.Name);
  519. foreach (var nestedGenericParameter in type.DeclaringType.GetGenericArguments())
  520. if (dic.ContainsKey(nestedGenericParameter.Name))
  521. dic.Remove(nestedGenericParameter.Name);
  522. genericParameters = dic.Values.ToArray();
  523. }
  524. if (genericParameters.Any() == false)
  525. return sb.Append(type.Name).ToString();
  526. sb.Append(type.Name.Remove(type.Name.IndexOf('`'))).Append("<");
  527. var genericTypeIndex = 0;
  528. foreach (var genericType in genericParameters)
  529. {
  530. if (genericTypeIndex++ > 0) sb.Append(", ");
  531. sb.Append(DisplayCsharp(genericType, true));
  532. }
  533. return sb.Append(">").ToString();
  534. }
  535. }
  536. public enum GetFormatterEnum
  537. {
  538. /// <summary>
  539. /// 默认类型
  540. /// </summary>
  541. Default = 0,
  542. /// <summary>
  543. /// yyyy
  544. /// </summary>
  545. yyyy = 4,
  546. /// <summary>
  547. /// yyyy-MM
  548. /// </summary>
  549. yyyyMM = 5,
  550. /// <summary>
  551. /// yyyy-MM-dd
  552. /// </summary>
  553. yyyyMMdd = 6,
  554. /// <summary>
  555. /// yyyy-MM-dd hh
  556. /// </summary>
  557. yyyyMMddhh = 7,
  558. /// <summary>
  559. /// yyyy-MM-dd hh:mm
  560. /// </summary>
  561. yyyyMMddhhmm = 8,
  562. /// <summary>
  563. /// yyyy-MM-dd hh:mm:ss
  564. /// </summary>
  565. yyyyMMddhhmmss = 9,
  566. /// <summary>
  567. /// yyyy-MM-dd hh:mm:ss:fffffff
  568. /// </summary>
  569. yyyyMMddhhmmssfffffff = 10,
  570. }
  571. }