Extentions.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using WCS.Core.DataTrans;
  9. using WCS.Entity;
  10. namespace WCS.Core
  11. {
  12. public static class Extentions
  13. {
  14. //public static PlcDB Regist(this IPlcConn plc, ushort dbNo, ushort length)
  15. //{
  16. // var plc= new PlcDB(plc, dbNo, length);
  17. //}
  18. #region GetData
  19. public static byte[] GetData(this bool value)
  20. {
  21. return BitConverter.GetBytes(value);
  22. }
  23. public static byte[] GetData(this byte value)
  24. {
  25. return new byte[] { value };
  26. }
  27. public static byte[] GetData(this char value)
  28. {
  29. return BitConverter.GetBytes(value);
  30. }
  31. public static byte[] GetData(this short value)
  32. {
  33. return BitConverter.GetBytes(value);
  34. }
  35. public static byte[] GetData(this ushort value)
  36. {
  37. return BitConverter.GetBytes(value);
  38. }
  39. public static byte[] GetData(this int value)
  40. {
  41. return BitConverter.GetBytes(value);
  42. }
  43. public static byte[] GetData(this uint value)
  44. {
  45. return BitConverter.GetBytes(value);
  46. }
  47. public static byte[] GetData(this long value)
  48. {
  49. return BitConverter.GetBytes(value);
  50. }
  51. public static byte[] GetData(this ulong value)
  52. {
  53. return BitConverter.GetBytes(value);
  54. }
  55. internal static Encoding enCoding = Encoding.GetEncoding("gb2312");
  56. private static Dictionary<Type, MethodInfo> ToBytesMethods = new Dictionary<Type, MethodInfo>();
  57. private static byte[] GetPrimitiveData(object obj)
  58. {
  59. try
  60. {
  61. var t = obj.GetType();
  62. if (t == typeof(byte))
  63. return new byte[] { (byte)obj };
  64. MethodInfo mi;
  65. lock (ToBytesMethods)
  66. {
  67. if (!ToBytesMethods.TryGetValue(t, out mi))
  68. {
  69. var ms = typeof(BitConverter).GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
  70. ms = ms.Where(v => v.Name == "GetBytes").ToArray();
  71. mi = ms.FirstOrDefault(v => v.GetParameters()[0].ParameterType == t);
  72. ToBytesMethods.Add(t, mi);
  73. }
  74. }
  75. var res = mi.Invoke(null, new object[] { obj }) as byte[];
  76. //if (!BitConverter.IsLittleEndian)
  77. {
  78. res = res.Reverse().ToArray();
  79. }
  80. return res;
  81. }
  82. catch (Exception)
  83. {
  84. throw;
  85. }
  86. }
  87. public static byte[] GetData(this object obj, byte dataSize = 0)
  88. {
  89. var type = obj.GetType();
  90. System.IO.MemoryStream st = new System.IO.MemoryStream();
  91. if (type.IsArray)
  92. {
  93. foreach (var o in obj as Array)
  94. {
  95. var data = GetData(o);
  96. st.Write(data, 0, data.Length);
  97. }
  98. }
  99. else
  100. {
  101. if (type == typeof(string))
  102. {
  103. var data = enCoding.GetBytes(obj.ToString());
  104. st.WriteByte(dataSize);
  105. st.WriteByte((byte)data.Length);
  106. st.Write(data, 0, data.Length);
  107. }
  108. else
  109. {
  110. var data = GetPrimitiveData(obj);
  111. st.Write(data, 0, data.Length);
  112. }
  113. }
  114. return st.ToArray();
  115. }
  116. #endregion GetData
  117. /// <summary>
  118. /// 转换基元类型或者String
  119. /// </summary>
  120. /// <param name="data"></param>
  121. /// <param name="type">类型为基元类型或者String</param>
  122. /// <returns></returns>
  123. public static object GetObj(this byte[] data, Type type)
  124. {
  125. if (type == typeof(string))
  126. {
  127. var len = data.Skip(1).First();
  128. var bytes = data.Skip(2).Take(len).ToArray();
  129. return Configs.StringEncoding.GetString(bytes);
  130. }
  131. else
  132. {
  133. if (type.IsEnum)
  134. type = type.GetEnumUnderlyingType();
  135. data = data.Reverse().ToArray();
  136. if (type == typeof(bool))
  137. {
  138. return BitConverter.ToBoolean(data, 0);
  139. }
  140. else if (type == typeof(char))
  141. {
  142. return BitConverter.ToChar(data, 0);
  143. }
  144. else if (type == typeof(byte))
  145. {
  146. return data.First();
  147. }
  148. else if (type == typeof(short))
  149. {
  150. return BitConverter.ToInt16(data, 0);
  151. }
  152. else if (type == typeof(ushort))
  153. {
  154. return BitConverter.ToUInt16(data, 0);
  155. }
  156. else if (type == typeof(int))
  157. {
  158. return BitConverter.ToInt32(data, 0);
  159. }
  160. else if (type == typeof(uint))
  161. {
  162. return BitConverter.ToUInt32(data, 0);
  163. }
  164. else if (type == typeof(long))
  165. {
  166. return BitConverter.ToInt64(data, 0);
  167. }
  168. else if (type == typeof(ulong))
  169. {
  170. return BitConverter.ToUInt64(data, 0);
  171. }
  172. else
  173. {
  174. throw new Exception("类型不支持");
  175. }
  176. }
  177. }
  178. /// <summary>
  179. /// 转换基元类型或者String
  180. /// </summary>
  181. /// <typeparam name="T"></typeparam>
  182. /// <param name="data"></param>
  183. /// <returns></returns>
  184. public static T GetObj<T>(this byte[] data)
  185. {
  186. return (T)data.GetObj(typeof(T));
  187. }
  188. /// <summary>
  189. /// 转换基元类型或者String,或数组
  190. /// </summary>
  191. /// <param name="data"></param>
  192. /// <param name="type"></param>
  193. /// <param name="datasize">String长度</param>
  194. /// <returns></returns>
  195. public static object GetObj(this byte[] data, Type type, byte datasize)
  196. {
  197. if (type.IsArray)
  198. {
  199. var t = type.GetElementType();
  200. var lsttype = typeof(List<>).MakeGenericType(t);
  201. var lst = Activator.CreateInstance(lsttype) as System.Collections.IList;
  202. var i = 0;
  203. if (t == typeof(bool))
  204. datasize = 1;
  205. else
  206. datasize = (byte)Marshal.SizeOf(t);
  207. while (i < data.Length)
  208. {
  209. var obj = GetObj(data.Skip(i).Take(datasize).ToArray(), t, datasize);
  210. lst.Add(obj);
  211. i += datasize;
  212. }
  213. var array = Array.CreateInstance(t, lst.Count) as Array;
  214. lst.CopyTo(array, 0);
  215. return array;
  216. }
  217. else
  218. return data.GetObj(type);
  219. }
  220. /// <summary>
  221. /// 转换基元类型或者String,或数组
  222. /// </summary>
  223. /// <typeparam name="T"></typeparam>
  224. /// <param name="data"></param>
  225. /// <param name="datasize">String长度</param>
  226. /// <returns></returns>
  227. public static T GetObj<T>(this byte[] data, byte datasize = 0)
  228. {
  229. return (T)data.GetObj(typeof(T), datasize);
  230. }
  231. }
  232. public static class BitExtension
  233. {
  234. #region ushort
  235. /// <summary>
  236. /// 设置指定位置的位值的值
  237. /// </summary>
  238. /// <param name="value">ushort对象</param>
  239. /// <param name="position">指定位置</param>
  240. /// <param name="flag">值</param>
  241. /// <returns></returns>
  242. public static ushort SetBit(this ushort value, int position, bool flag)
  243. {
  244. return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
  245. }
  246. /// <summary>
  247. /// 批量设置指定位置的位值的值
  248. /// </summary>
  249. /// <param name="value">ushort对象</param>
  250. /// <param name="position">开始位置</param>
  251. /// <param name="length">长度</param>
  252. /// <param name="bits">值</param>
  253. /// <returns></returns>
  254. public static ushort SetBits(this ushort value, int position, int length, ushort bits)
  255. {
  256. if (length <= 0 || position >= 16) return value;
  257. var mask = (2 << (length - 1)) - 1;
  258. value &= (ushort)~(mask << position);
  259. value |= (ushort)((bits & mask) << position);
  260. return value;
  261. }
  262. /// <summary>
  263. /// 获取指定位置的值
  264. /// </summary>
  265. /// <param name="value">ushort对象</param>
  266. /// <param name="position">指定位置</param>
  267. /// <returns></returns>
  268. public static bool GetBit(this ushort value, int position)
  269. {
  270. return GetBits(value, position, 1) == 1;
  271. }
  272. /// <summary>
  273. /// 批量获取指定位置的值
  274. /// </summary>
  275. /// <param name="value">ushort对象</param>
  276. /// <param name="position">开始位值</param>
  277. /// <param name="length">长度</param>
  278. /// <returns></returns>
  279. public static ushort GetBits(this ushort value, int position, int length)
  280. {
  281. if (length <= 0 || position >= 16) return 0;
  282. var mask = (2 << (length - 1)) - 1;
  283. return (ushort)((value >> position) & mask);
  284. }
  285. #endregion ushort
  286. #region byte
  287. /// <summary>
  288. /// 设置指定位置的值
  289. /// </summary>
  290. /// <param name="value">byte对象</param>
  291. /// <param name="position">指定位置</param>
  292. /// <param name="flag">设置值</param>
  293. /// <returns></returns>
  294. public static byte SetBit(this byte value, int position, bool flag)
  295. {
  296. if (position >= 8) return value;
  297. var mask = (2 << (1 - 1)) - 1;
  298. value &= (byte)~(mask << position);
  299. value |= (byte)(((flag ? 1 : 0) & mask) << position);
  300. return value;
  301. }
  302. /// <summary>
  303. /// 获取指定位置的值
  304. /// </summary>
  305. /// <param name="value">byte对象</param>
  306. /// <param name="position">指定位置</param>
  307. /// <returns></returns>
  308. public static bool GetBit(this byte value, int position)
  309. {
  310. if (position >= 8) return false;
  311. var mask = (2 << (1 - 1)) - 1;
  312. return (byte)((value >> position) & mask) == 1;
  313. }
  314. #endregion byte
  315. #region uint
  316. /// <summary>
  317. /// 设置指定位置的位值的值
  318. /// </summary>
  319. /// <param name="value">uint对象</param>
  320. /// <param name="position">指定位置</param>
  321. /// <param name="flag">值</param>
  322. /// <returns></returns>
  323. public static uint SetBit(this uint value, int position, bool flag)
  324. {
  325. return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
  326. }
  327. /// <summary>
  328. /// 批量设置指定位置的位值的值
  329. /// </summary>
  330. /// <param name="value">uint对象</param>
  331. /// <param name="position">开始位置</param>
  332. /// <param name="length">长度</param>
  333. /// <param name="bits">值</param>
  334. /// <returns></returns>
  335. public static uint SetBits(this uint value, int position, int length, uint bits)
  336. {
  337. if (length <= 0 || position >= 32) return value;
  338. var mask = (2 << (length - 1)) - 1;
  339. value &= (uint)~(mask << position);
  340. value |= (uint)((bits & mask) << position);
  341. return value;
  342. }
  343. /// <summary>
  344. /// 获取指定位置的值
  345. /// </summary>
  346. /// <param name="value">uint对象</param>
  347. /// <param name="position">指定位置</param>
  348. /// <returns></returns>
  349. public static bool GetBit(this uint value, int position)
  350. {
  351. return GetBits(value, position, 1) == 1;
  352. }
  353. /// <summary>
  354. /// 批量获取指定位置的值
  355. /// </summary>
  356. /// <param name="value">uint对象</param>
  357. /// <param name="position">开始位值</param>
  358. /// <param name="length">长度</param>
  359. /// <returns></returns>
  360. public static uint GetBits(this uint value, int position, int length)
  361. {
  362. if (length <= 0 || position >= 32) return 0;
  363. var mask = (2 << (length - 1)) - 1;
  364. return (uint)((value >> position) & mask);
  365. }
  366. #endregion uint
  367. #region ulong
  368. /// <summary>
  369. /// 设置指定位置的位值的值
  370. /// </summary>
  371. /// <param name="value">ulong对象</param>
  372. /// <param name="position">指定位置</param>
  373. /// <param name="flag">值</param>
  374. /// <returns></returns>
  375. public static ulong SetBit(this ulong value, int position, bool flag)
  376. {
  377. return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);
  378. }
  379. /// <summary>
  380. /// 批量设置指定位置的位值的值
  381. /// </summary>
  382. /// <param name="value">ulong对象</param>
  383. /// <param name="position">开始位置</param>
  384. /// <param name="length">长度</param>
  385. /// <param name="bits">值</param>
  386. /// <returns></returns>
  387. public static ulong SetBits(this ulong value, int position, int length, ulong bits)
  388. {
  389. if (length <= 0 || position >= 64) return value;
  390. var mask = (ulong)(2 << (length - 1)) - 1;
  391. value &= ~(mask << position);
  392. value |= (bits & mask) << position;
  393. return value;
  394. }
  395. /// <summary>
  396. /// 获取指定位置的值
  397. /// </summary>
  398. /// <param name="value">ulong对象</param>
  399. /// <param name="position">指定位置</param>
  400. /// <returns></returns>
  401. public static bool GetBit(this ulong value, int position)
  402. {
  403. return GetBits(value, position, 1) == 1;
  404. }
  405. /// <summary>
  406. /// 批量获取指定位置的值
  407. /// </summary>
  408. /// <param name="value">ulong对象</param>
  409. /// <param name="position">开始位值</param>
  410. /// <param name="length">长度</param>
  411. /// <returns></returns>
  412. public static ulong GetBits(this ulong value, int position, int length)
  413. {
  414. if (length <= 0 || position >= 64) return 0;
  415. var mask = (ulong)(2 << (length - 1)) - 1;
  416. return (value >> position) & mask;
  417. }
  418. #endregion ulong
  419. }
  420. public static class Extensions
  421. {
  422. private static ConcurrentDictionary<WCS_DEVICEPROTOCOL, object> Datas = new ConcurrentDictionary<WCS_DEVICEPROTOCOL, object>();
  423. private static ConcurrentDictionary<object, object> ExObjs = new ConcurrentDictionary<object, object>();
  424. public static T Data<T>(this WCS_DEVICEPROTOCOL obj)
  425. {
  426. return (T)Data(obj);
  427. }
  428. public static object Data(this WCS_DEVICEPROTOCOL obj)
  429. {
  430. if (!Datas.ContainsKey(obj))
  431. {
  432. var type = typeof(Generator<,>);
  433. type = type.MakeGenericType(obj.DB.GetProtocolType(), Configs.ProtocolProxyBaseType);
  434. var m = type.GetMethod("Create", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
  435. Datas[obj] = m.Invoke(null, new object[] { new object[] { obj.DEVICE.CODE.ToString(), obj.DB, (ushort)obj.POSITION, obj } });
  436. }
  437. return Datas[obj];
  438. }
  439. //public static WCS_DEVICE Device(this IDATA obj)
  440. //{
  441. // return (obj as PLCData).Device;
  442. //}
  443. public static T GetEx<T>(object entity)
  444. {
  445. if (!ExObjs.ContainsKey(entity))
  446. {
  447. var obj = Activator.CreateInstance(typeof(T), entity);
  448. ExObjs[entity] = obj;
  449. }
  450. return (T)ExObjs[entity];
  451. }
  452. public static DataBlock Ex(this WCS_DATABLOCK source)
  453. {
  454. return GetEx<DataBlock>(source);
  455. }
  456. public static PlcAccessor Ex(this WCS_PLC source)
  457. {
  458. return GetEx<PlcAccessor>(source);
  459. }
  460. public static WCS_DEVICEPROTOCOL PROTOCOL(this IProtocol obj)
  461. {
  462. return (obj as ProtocolProxyBase).Protocol;
  463. }
  464. public static T Create<T>(this WCS_DEVICE source)
  465. {
  466. return (T)Activator.CreateInstance(typeof(T), source);
  467. }
  468. public static Device<T> Device<T>(this WCS_DEVICE source) where T : IProtocol
  469. {
  470. var res = (Device<T>)Activator.CreateInstance(typeof(Device<T>), source);
  471. return res;
  472. }
  473. public static Device<T, T2> Device<T, T2>(this WCS_DEVICE source) where T : IProtocol where T2 : IProtocol
  474. {
  475. return (Device<T, T2>)Activator.CreateInstance(typeof(Device<T, T2>), source);
  476. }
  477. public static Device<T, T2, T3> Device<T, T2, T3>(this WCS_DEVICE source) where T : IProtocol where T2 : IProtocol where T3 : IProtocol
  478. {
  479. return (Device<T, T2, T3>)Activator.CreateInstance(typeof(Device<T, T2, T3>), source);
  480. }
  481. public static DeviceGroup<T> DeviceGroup<T>(this WCS_DEVICE source) where T : IProtocol
  482. {
  483. return (DeviceGroup<T>)Activator.CreateInstance(typeof(DeviceGroup<T>), source);
  484. }
  485. public static DeviceGroup<T, T2> DeviceGroup<T, T2>(this WCS_DEVICE source) where T : IProtocol where T2 : IProtocol
  486. {
  487. return (DeviceGroup<T, T2>)Activator.CreateInstance(typeof(DeviceGroup<T, T2>), source);
  488. }
  489. public static DeviceGroup<T, T2, T3> DeviceGroup<T, T2, T3>(this WCS_DEVICE source) where T : IProtocol where T2 : IProtocol where T3 : IProtocol
  490. {
  491. return (DeviceGroup<T, T2, T3>)Activator.CreateInstance(typeof(DeviceGroup<T, T2, T3>), source);
  492. }
  493. public static DateTime GetUpdateTime(this IProtocol source)
  494. {
  495. dynamic obj = source;
  496. return obj.UpdateTime;
  497. }
  498. }
  499. }