Extentions.cs 14 KB

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