Extentions.cs 16 KB

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