Extentions.cs 14 KB

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