Extentions.cs 15 KB

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