Extentions.cs 19 KB

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