Extentions.cs 14 KB

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