Extentions.cs 14 KB

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