Extentions.cs 15 KB

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