Extentions.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using System.Collections.Concurrent;
  2. using System.ComponentModel;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. namespace WCS.Core;
  6. public static class Extentions
  7. {
  8. private static readonly ConcurrentDictionary<object, object> ExObjs = new();
  9. private static readonly ConcurrentDictionary<World, ConcurrentDictionary<object, object>> ExObjsOfWorld = new();
  10. public static T GetEx<T>(object entity)
  11. {
  12. lock (ExObjs)
  13. {
  14. if (!ExObjs.ContainsKey(entity))
  15. {
  16. var obj = Activator.CreateInstance(typeof(T), entity);
  17. ExObjs[entity] = obj;
  18. }
  19. return (T)ExObjs[entity];
  20. }
  21. }
  22. public static PLC Ex(this PLCInfo source, World world)
  23. {
  24. return GetExOfWorld<PLC>(source, world);
  25. }
  26. private static T GetExOfWorld<T>(this object source, World world)
  27. {
  28. if (!ExObjsOfWorld.TryGetValue(world, out var objs))
  29. {
  30. ExObjsOfWorld.TryAdd(world, new ConcurrentDictionary<object, object>());
  31. objs = ExObjsOfWorld[world];
  32. }
  33. if (!objs.TryGetValue(source, out var obj))
  34. lock (objs)
  35. {
  36. if (!objs.ContainsKey(source))
  37. {
  38. obj = Activator.CreateInstance(typeof(T), source, world);
  39. objs.TryAdd(source, obj);
  40. }
  41. obj = objs[source];
  42. }
  43. return (T)obj;
  44. }
  45. public static DataBlock Ex(this DBInfo source, World world)
  46. {
  47. return GetExOfWorld<DataBlock>(source, world);
  48. }
  49. public static DataBlock[] GetDataBlocks(this World source)
  50. {
  51. ExObjsOfWorld.TryGetValue(source, out var dic);
  52. if (dic == null)
  53. return new DataBlock[0];
  54. var res = dic.Values.OfType<DataBlock>().ToArray();
  55. return res;
  56. }
  57. public static WorldEx Ex(this World source)
  58. {
  59. return GetEx<WorldEx>(source);
  60. }
  61. public static void AddSafe<T>(this List<T> source, T item)
  62. {
  63. lock (source)
  64. {
  65. source.Add(item);
  66. }
  67. }
  68. public static void AddSafe<T>(this List<T> source, IEnumerable<T> item)
  69. {
  70. lock (source)
  71. {
  72. source.AddRange(item);
  73. }
  74. }
  75. public static string Description<T>(this T source) where T : struct, Enum
  76. {
  77. var name = Enum.GetName(source);
  78. var f = source.GetType().GetField(name);
  79. var attr = f.GetCustomAttribute<DescriptionAttribute>();
  80. if (attr == null)
  81. return source.ToString();
  82. return attr.Description;
  83. }
  84. public static object Copy(this object source, Type t)
  85. {
  86. var obj = Activator.CreateInstance(t);
  87. foreach (var p in t.GetProperties())
  88. {
  89. var p2 = source.GetType().GetProperty(p.Name);
  90. var value = p2.GetValue(source);
  91. p.SetValue(obj, value);
  92. }
  93. return obj;
  94. }
  95. public static object Copy(this object source, Type t, DateTime farme)
  96. {
  97. var obj = Activator.CreateInstance(t);
  98. foreach (var p in t.GetProperties())
  99. {
  100. var p2 = source.GetType().GetProperty(p.Name);
  101. if (p2 == null) continue;
  102. var value = p2.GetValue(source);
  103. //判断一下P2的类型是否为字符串
  104. if (p2.PropertyType == typeof(string))
  105. {
  106. var sValue = (string)value;
  107. value = sValue.Trim('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v').Trim();
  108. }
  109. p.SetValue(obj, value);
  110. }
  111. t.GetProperty("Frame").SetValue(obj, farme);
  112. return obj;
  113. }
  114. public static T Copy<T>(this object source)
  115. {
  116. return (T)source.Copy(typeof(T), DateTime.Now);
  117. }
  118. }
  119. public static class BitExtension
  120. {
  121. #region ushort
  122. /// <summary>
  123. /// 设置指定位置的位值的值
  124. /// </summary>
  125. /// <param name="value">ushort对象</param>
  126. /// <param name="position">指定位置</param>
  127. /// <param name="flag">值</param>
  128. /// <returns></returns>
  129. public static ushort SetBit(this ushort value, int position, bool flag)
  130. {
  131. return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
  132. }
  133. /// <summary>
  134. /// 批量设置指定位置的位值的值
  135. /// </summary>
  136. /// <param name="value">ushort对象</param>
  137. /// <param name="position">开始位置</param>
  138. /// <param name="length">长度</param>
  139. /// <param name="bits">值</param>
  140. /// <returns></returns>
  141. public static ushort SetBits(this ushort value, int position, int length, ushort bits)
  142. {
  143. if (length <= 0 || position >= 16) return value;
  144. var mask = (2 << (length - 1)) - 1;
  145. value &= (ushort)~(mask << position);
  146. value |= (ushort)((bits & mask) << position);
  147. return value;
  148. }
  149. /// <summary>
  150. /// 获取指定位置的值
  151. /// </summary>
  152. /// <param name="value">ushort对象</param>
  153. /// <param name="position">指定位置</param>
  154. /// <returns></returns>
  155. public static bool GetBit(this ushort value, int position)
  156. {
  157. return GetBits(value, position, 1) == 1;
  158. }
  159. /// <summary>
  160. /// 批量获取指定位置的值
  161. /// </summary>
  162. /// <param name="value">ushort对象</param>
  163. /// <param name="position">开始位值</param>
  164. /// <param name="length">长度</param>
  165. /// <returns></returns>
  166. public static ushort GetBits(this ushort value, int position, int length)
  167. {
  168. if (length <= 0 || position >= 16) return 0;
  169. var mask = (2 << (length - 1)) - 1;
  170. return (ushort)((value >> position) & mask);
  171. }
  172. #endregion ushort
  173. #region byte
  174. /// <summary>
  175. /// 设置指定位置的值
  176. /// </summary>
  177. /// <param name="value">byte对象</param>
  178. /// <param name="position">指定位置</param>
  179. /// <param name="flag">设置值</param>
  180. /// <returns></returns>
  181. public static byte SetBit(this byte value, int position, bool flag)
  182. {
  183. if (position >= 8) return value;
  184. var mask = (2 << (1 - 1)) - 1;
  185. value &= (byte)~(mask << position);
  186. value |= (byte)(((flag ? 1 : 0) & mask) << position);
  187. return value;
  188. }
  189. /// <summary>
  190. /// 获取指定位置的值
  191. /// </summary>
  192. /// <param name="value">byte对象</param>
  193. /// <param name="position">指定位置</param>
  194. /// <returns></returns>
  195. public static bool GetBit(this byte value, int position)
  196. {
  197. if (position >= 8) return false;
  198. var mask = (2 << (1 - 1)) - 1;
  199. return (byte)((value >> position) & mask) == 1;
  200. }
  201. #endregion byte
  202. #region uint
  203. /// <summary>
  204. /// 设置指定位置的位值的值
  205. /// </summary>
  206. /// <param name="value">uint对象</param>
  207. /// <param name="position">指定位置</param>
  208. /// <param name="flag">值</param>
  209. /// <returns></returns>
  210. public static uint SetBit(this uint value, int position, bool flag)
  211. {
  212. return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
  213. }
  214. /// <summary>
  215. /// 批量设置指定位置的位值的值
  216. /// </summary>
  217. /// <param name="value">uint对象</param>
  218. /// <param name="position">开始位置</param>
  219. /// <param name="length">长度</param>
  220. /// <param name="bits">值</param>
  221. /// <returns></returns>
  222. public static uint SetBits(this uint value, int position, int length, uint bits)
  223. {
  224. if (length <= 0 || position >= 32) return value;
  225. var mask = (2 << (length - 1)) - 1;
  226. value &= (uint)~(mask << position);
  227. value |= (uint)((bits & mask) << position);
  228. return value;
  229. }
  230. /// <summary>
  231. /// 获取指定位置的值
  232. /// </summary>
  233. /// <param name="value">uint对象</param>
  234. /// <param name="position">指定位置</param>
  235. /// <returns></returns>
  236. public static bool GetBit(this uint value, int position)
  237. {
  238. return GetBits(value, position, 1) == 1;
  239. }
  240. /// <summary>
  241. /// 批量获取指定位置的值
  242. /// </summary>
  243. /// <param name="value">uint对象</param>
  244. /// <param name="position">开始位值</param>
  245. /// <param name="length">长度</param>
  246. /// <returns></returns>
  247. public static uint GetBits(this uint value, int position, int length)
  248. {
  249. if (length <= 0 || position >= 32) return 0;
  250. var mask = (2 << (length - 1)) - 1;
  251. return (uint)((value >> position) & mask);
  252. }
  253. #endregion uint
  254. #region ulong
  255. /// <summary>
  256. /// 设置指定位置的位值的值
  257. /// </summary>
  258. /// <param name="value">ulong对象</param>
  259. /// <param name="position">指定位置</param>
  260. /// <param name="flag">值</param>
  261. /// <returns></returns>
  262. public static ulong SetBit(this ulong value, int position, bool flag)
  263. {
  264. return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
  265. }
  266. /// <summary>
  267. /// 批量设置指定位置的位值的值
  268. /// </summary>
  269. /// <param name="value">ulong对象</param>
  270. /// <param name="position">开始位置</param>
  271. /// <param name="length">长度</param>
  272. /// <param name="bits">值</param>
  273. /// <returns></returns>
  274. public static ulong SetBits(this ulong value, int position, int length, ulong bits)
  275. {
  276. if (length <= 0 || position >= 64) return value;
  277. var mask = (ulong)(2 << (length - 1)) - 1;
  278. value &= ~(mask << position);
  279. value |= (bits & mask) << position;
  280. return value;
  281. }
  282. /// <summary>
  283. /// 获取指定位置的值
  284. /// </summary>
  285. /// <param name="value">ulong对象</param>
  286. /// <param name="position">指定位置</param>
  287. /// <returns></returns>
  288. public static bool GetBit(this ulong value, int position)
  289. {
  290. return GetBits(value, position, 1) == 1;
  291. }
  292. /// <summary>
  293. /// 批量获取指定位置的值
  294. /// </summary>
  295. /// <param name="value">ulong对象</param>
  296. /// <param name="position">开始位值</param>
  297. /// <param name="length">长度</param>
  298. /// <returns></returns>
  299. public static ulong GetBits(this ulong value, int position, int length)
  300. {
  301. if (length <= 0 || position >= 64) return 0;
  302. var mask = (ulong)(2 << (length - 1)) - 1;
  303. return (value >> position) & mask;
  304. }
  305. #endregion ulong
  306. }
  307. public static class ExpressionExtensions
  308. {
  309. public static string ExpToString(this LambdaExpression source)
  310. {
  311. var str = "";
  312. try
  313. {
  314. str = LambdaToString(source);
  315. }
  316. catch
  317. {
  318. str = source.ToString();
  319. }
  320. var arr = str.Split("Convert(");
  321. arr = arr.Select(v =>
  322. {
  323. if (!v.Contains(','))
  324. return v;
  325. var index = v.IndexOf(',');
  326. var index2 = v.IndexOf(')', index);
  327. var sub = v.Substring(index, index2 - index + 1);
  328. var str = v.Replace(sub, "");
  329. return str;
  330. }).ToArray();
  331. var res = string.Join("", arr);
  332. return res;
  333. }
  334. public static string LambdaToString(LambdaExpression expression)
  335. {
  336. var replacements = new Dictionary<string, string>();
  337. WalkExpression(replacements, expression);
  338. var body = expression.ToString();
  339. foreach (var parm in expression.Parameters)
  340. {
  341. var parmName = parm.Name;
  342. var parmTypeName = parm.Type.Name;
  343. body = body.Replace(parmName + " =>", "(" + parmTypeName + " v) =>");
  344. }
  345. foreach (var replacement in replacements) body = body.Replace(replacement.Key, replacement.Value);
  346. return body;
  347. }
  348. private static void WalkExpression(Dictionary<string, string> replacements, Expression expression)
  349. {
  350. switch (expression.NodeType)
  351. {
  352. case ExpressionType.MemberAccess:
  353. var replacementExpression = expression.ToString();
  354. if (replacementExpression.Contains("value("))
  355. {
  356. var replacementValue = Expression.Lambda(expression).Compile().DynamicInvoke().ToString();
  357. if (!replacements.ContainsKey(replacementExpression))
  358. replacements.Add(replacementExpression, replacementValue);
  359. }
  360. break;
  361. case ExpressionType.GreaterThan:
  362. case ExpressionType.GreaterThanOrEqual:
  363. case ExpressionType.LessThan:
  364. case ExpressionType.LessThanOrEqual:
  365. case ExpressionType.OrElse:
  366. case ExpressionType.AndAlso:
  367. case ExpressionType.Equal:
  368. case ExpressionType.NotEqual:
  369. var bexp = expression as BinaryExpression;
  370. WalkExpression(replacements, bexp.Left);
  371. WalkExpression(replacements, bexp.Right);
  372. break;
  373. case ExpressionType.Call:
  374. var mcexp = expression as MethodCallExpression;
  375. foreach (var argument in mcexp.Arguments) WalkExpression(replacements, argument);
  376. break;
  377. case ExpressionType.Lambda:
  378. var lexp = expression as LambdaExpression;
  379. WalkExpression(replacements, lexp.Body);
  380. break;
  381. case ExpressionType.Constant:
  382. //do nothing
  383. break;
  384. case ExpressionType.Convert:
  385. var exp = expression as UnaryExpression;
  386. WalkExpression(replacements, exp.Operand);
  387. break;
  388. }
  389. }
  390. private static bool When<T>(this T source, Expression<Func<T, bool>> exp) where T : class
  391. {
  392. var str = exp.ExpToString();
  393. try
  394. {
  395. var res = exp.Compile().Invoke(source);
  396. str += res ? " 成立" : " 不成立";
  397. return res;
  398. }
  399. catch (Exception ex)
  400. {
  401. str += ex.GetBaseException().Message;
  402. throw;
  403. }
  404. finally
  405. {
  406. Console.WriteLine(str);
  407. Console.WriteLine("------------------------------------");
  408. }
  409. }
  410. }