Extentions.cs 15 KB

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