Extentions.cs 15 KB

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