Extentions.cs 16 KB

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