| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 | using System;using System.Collections.Concurrent;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Runtime.InteropServices;using System.Text;using WCS.Core.DataTrans;using WCS.Entity;namespace WCS.Core{    public static class Extentions    {        //public static PlcDB Regist(this IPlcConn plc, ushort dbNo, ushort length)        //{        //    var plc= new PlcDB(plc, dbNo, length);        //}        #region GetData        public static byte[] GetData(this bool value)        {            return BitConverter.GetBytes(value);        }        public static byte[] GetData(this byte value)        {            return new byte[] { value };        }        public static byte[] GetData(this char value)        {            return BitConverter.GetBytes(value);        }        public static byte[] GetData(this short value)        {            return BitConverter.GetBytes(value);        }        public static byte[] GetData(this ushort value)        {            return BitConverter.GetBytes(value);        }        public static byte[] GetData(this int value)        {            return BitConverter.GetBytes(value);        }        public static byte[] GetData(this uint value)        {            return BitConverter.GetBytes(value);        }        public static byte[] GetData(this long value)        {            return BitConverter.GetBytes(value);        }        public static byte[] GetData(this ulong value)        {            return BitConverter.GetBytes(value);        }        internal static Encoding enCoding = Encoding.GetEncoding("gb2312");        private static Dictionary<Type, MethodInfo> ToBytesMethods = new Dictionary<Type, MethodInfo>();        private static byte[] GetPrimitiveData(object obj)        {            try            {                var t = obj.GetType();                if (t == typeof(byte))                    return new byte[] { (byte)obj };                MethodInfo mi;                lock (ToBytesMethods)                {                    if (!ToBytesMethods.TryGetValue(t, out mi))                    {                        var ms = typeof(BitConverter).GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);                        ms = ms.Where(v => v.Name == "GetBytes").ToArray();                        mi = ms.FirstOrDefault(v => v.GetParameters()[0].ParameterType == t);                        ToBytesMethods.Add(t, mi);                    }                }                var res = mi.Invoke(null, new object[] { obj }) as byte[];                //if (!BitConverter.IsLittleEndian)                {                    res = res.Reverse().ToArray();                }                return res;            }            catch (Exception)            {                throw;            }        }        public static byte[] GetData(this object obj, byte dataSize = 0)        {            var type = obj.GetType();            System.IO.MemoryStream st = new System.IO.MemoryStream();            if (type.IsArray)            {                foreach (var o in obj as Array)                {                    var data = GetData(o);                    st.Write(data, 0, data.Length);                }            }            else            {                if (type == typeof(string))                {                    var data = enCoding.GetBytes(obj.ToString());                    st.WriteByte(dataSize);                    st.WriteByte((byte)data.Length);                    st.Write(data, 0, data.Length);                }                else                {                    var data = GetPrimitiveData(obj);                    st.Write(data, 0, data.Length);                }            }            return st.ToArray();        }        #endregion GetData        /// <summary>        /// 转换基元类型或者String        /// </summary>        /// <param name="data"></param>        /// <param name="type">类型为基元类型或者String</param>        /// <returns></returns>        public static object GetObj(this byte[] data, Type type)        {            if (type == typeof(string))            {                var len = data.Skip(1).First();                var bytes = data.Skip(2).Take(len).ToArray();                return Configs.StringEncoding.GetString(bytes);            }            else            {                if (type.IsEnum)                    type = type.GetEnumUnderlyingType();                data = data.Reverse().ToArray();                if (type == typeof(bool))                {                    return BitConverter.ToBoolean(data, 0);                }                else if (type == typeof(char))                {                    return BitConverter.ToChar(data, 0);                }                else if (type == typeof(byte))                {                    return data.First();                }                else if (type == typeof(short))                {                    return BitConverter.ToInt16(data, 0);                }                else if (type == typeof(ushort))                {                    return BitConverter.ToUInt16(data, 0);                }                else if (type == typeof(int))                {                    return BitConverter.ToInt32(data, 0);                }                else if (type == typeof(uint))                {                    return BitConverter.ToUInt32(data, 0);                }                else if (type == typeof(long))                {                    return BitConverter.ToInt64(data, 0);                }                else if (type == typeof(ulong))                {                    return BitConverter.ToUInt64(data, 0);                }                else                {                    throw new Exception("类型不支持");                }            }        }        /// <summary>        /// 转换基元类型或者String        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="data"></param>        /// <returns></returns>        public static T GetObj<T>(this byte[] data)        {            return (T)data.GetObj(typeof(T));        }        /// <summary>        /// 转换基元类型或者String,或数组        /// </summary>        /// <param name="data"></param>        /// <param name="type"></param>        /// <param name="datasize">String长度</param>        /// <returns></returns>        public static object GetObj(this byte[] data, Type type, byte datasize)        {            if (type.IsArray)            {                var t = type.GetElementType();                var lsttype = typeof(List<>).MakeGenericType(t);                var lst = Activator.CreateInstance(lsttype) as System.Collections.IList;                var i = 0;                if (t == typeof(bool))                    datasize = 1;                else                    datasize = (byte)Marshal.SizeOf(t);                while (i < data.Length)                {                    var obj = GetObj(data.Skip(i).Take(datasize).ToArray(), t, datasize);                    lst.Add(obj);                    i += datasize;                }                var array = Array.CreateInstance(t, lst.Count) as Array;                lst.CopyTo(array, 0);                return array;            }            else                return data.GetObj(type);        }        /// <summary>        /// 转换基元类型或者String,或数组        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="data"></param>        /// <param name="datasize">String长度</param>        /// <returns></returns>        public static T GetObj<T>(this byte[] data, byte datasize = 0)        {            return (T)data.GetObj(typeof(T), datasize);        }    }    public static class BitExtension    {        #region ushort        /// <summary>        /// 设置指定位置的位值的值        /// </summary>        /// <param name="value">ushort对象</param>        /// <param name="position">指定位置</param>        /// <param name="flag">值</param>        /// <returns></returns>        public static ushort SetBit(this ushort value, int position, bool flag)        {            return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);        }        /// <summary>        /// 批量设置指定位置的位值的值        /// </summary>        /// <param name="value">ushort对象</param>        /// <param name="position">开始位置</param>        /// <param name="length">长度</param>        /// <param name="bits">值</param>        /// <returns></returns>        public static ushort SetBits(this ushort value, int position, int length, ushort bits)        {            if (length <= 0 || position >= 16) return value;            var mask = (2 << (length - 1)) - 1;            value &= (ushort)~(mask << position);            value |= (ushort)((bits & mask) << position);            return value;        }        /// <summary>        /// 获取指定位置的值        /// </summary>        /// <param name="value">ushort对象</param>        /// <param name="position">指定位置</param>        /// <returns></returns>        public static bool GetBit(this ushort value, int position)        {            return GetBits(value, position, 1) == 1;        }        /// <summary>        /// 批量获取指定位置的值        /// </summary>        /// <param name="value">ushort对象</param>        /// <param name="position">开始位值</param>        /// <param name="length">长度</param>        /// <returns></returns>        public static ushort GetBits(this ushort value, int position, int length)        {            if (length <= 0 || position >= 16) return 0;            var mask = (2 << (length - 1)) - 1;            return (ushort)((value >> position) & mask);        }        #endregion ushort        #region byte        /// <summary>        /// 设置指定位置的值        /// </summary>        /// <param name="value">byte对象</param>        /// <param name="position">指定位置</param>        /// <param name="flag">设置值</param>        /// <returns></returns>        public static byte SetBit(this byte value, int position, bool flag)        {            if (position >= 8) return value;            var mask = (2 << (1 - 1)) - 1;            value &= (byte)~(mask << position);            value |= (byte)(((flag ? 1 : 0) & mask) << position);            return value;        }        /// <summary>        /// 获取指定位置的值        /// </summary>        /// <param name="value">byte对象</param>        /// <param name="position">指定位置</param>        /// <returns></returns>        public static bool GetBit(this byte value, int position)        {            if (position >= 8) return false;            var mask = (2 << (1 - 1)) - 1;            return (byte)((value >> position) & mask) == 1;        }        #endregion byte        #region uint        /// <summary>        /// 设置指定位置的位值的值        /// </summary>        /// <param name="value">uint对象</param>        /// <param name="position">指定位置</param>        /// <param name="flag">值</param>        /// <returns></returns>        public static uint SetBit(this uint value, int position, bool flag)        {            return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);        }        /// <summary>        /// 批量设置指定位置的位值的值        /// </summary>        /// <param name="value">uint对象</param>        /// <param name="position">开始位置</param>        /// <param name="length">长度</param>        /// <param name="bits">值</param>        /// <returns></returns>        public static uint SetBits(this uint value, int position, int length, uint bits)        {            if (length <= 0 || position >= 32) return value;            var mask = (2 << (length - 1)) - 1;            value &= (uint)~(mask << position);            value |= (uint)((bits & mask) << position);            return value;        }        /// <summary>        /// 获取指定位置的值        /// </summary>        /// <param name="value">uint对象</param>        /// <param name="position">指定位置</param>        /// <returns></returns>        public static bool GetBit(this uint value, int position)        {            return GetBits(value, position, 1) == 1;        }        /// <summary>        /// 批量获取指定位置的值        /// </summary>        /// <param name="value">uint对象</param>        /// <param name="position">开始位值</param>        /// <param name="length">长度</param>        /// <returns></returns>        public static uint GetBits(this uint value, int position, int length)        {            if (length <= 0 || position >= 32) return 0;            var mask = (2 << (length - 1)) - 1;            return (uint)((value >> position) & mask);        }        #endregion uint        #region ulong        /// <summary>        /// 设置指定位置的位值的值        /// </summary>        /// <param name="value">ulong对象</param>        /// <param name="position">指定位置</param>        /// <param name="flag">值</param>        /// <returns></returns>        public static ulong SetBit(this ulong value, int position, bool flag)        {            return SetBits(value, position, 1, flag ? (byte)1 : (byte)0);        }        /// <summary>        /// 批量设置指定位置的位值的值        /// </summary>        /// <param name="value">ulong对象</param>        /// <param name="position">开始位置</param>        /// <param name="length">长度</param>        /// <param name="bits">值</param>        /// <returns></returns>        public static ulong SetBits(this ulong value, int position, int length, ulong bits)        {            if (length <= 0 || position >= 64) return value;            var mask = (ulong)(2 << (length - 1)) - 1;            value &= ~(mask << position);            value |= (bits & mask) << position;            return value;        }        /// <summary>        /// 获取指定位置的值        /// </summary>        /// <param name="value">ulong对象</param>        /// <param name="position">指定位置</param>        /// <returns></returns>        public static bool GetBit(this ulong value, int position)        {            return GetBits(value, position, 1) == 1;        }        /// <summary>        /// 批量获取指定位置的值        /// </summary>        /// <param name="value">ulong对象</param>        /// <param name="position">开始位值</param>        /// <param name="length">长度</param>        /// <returns></returns>        public static ulong GetBits(this ulong value, int position, int length)        {            if (length <= 0 || position >= 64) return 0;            var mask = (ulong)(2 << (length - 1)) - 1;            return (value >> position) & mask;        }        #endregion ulong    }    public static class Extensions    {        private static ConcurrentDictionary<WCS_DEVICEPROTOCOL, object> Datas = new ConcurrentDictionary<WCS_DEVICEPROTOCOL, object>();        private static ConcurrentDictionary<object, object> ExObjs = new ConcurrentDictionary<object, object>();        public static T Data<T>(this WCS_DEVICEPROTOCOL obj)        {            return (T)Data(obj);        }        public static object Data(this WCS_DEVICEPROTOCOL obj)        {            if (!Datas.ContainsKey(obj))            {                var type = typeof(Generator<,>);                type = type.MakeGenericType(obj.DB.GetProtocolType(), Configs.ProtocolProxyBaseType);                var m = type.GetMethod("Create", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);                Datas[obj] = m.Invoke(null, new object[] { new object[] { obj.DEVICE.CODE.ToString(), obj.DB, (ushort)obj.POSITION, obj } });            }            return Datas[obj];        }        //public static WCS_DEVICE Device(this IDATA obj)        //{        //    return (obj as PLCData).Device;        //}        public static T GetEx<T>(object entity)        {            if (!ExObjs.ContainsKey(entity))            {                var obj = Activator.CreateInstance(typeof(T), entity);                ExObjs[entity] = obj;            }            return (T)ExObjs[entity];        }        public static DataBlock Ex(this WCS_DATABLOCK source)        {            return GetEx<DataBlock>(source);        }        public static PlcAccessor Ex(this WCS_PLC source)        {            return GetEx<PlcAccessor>(source);        }        public static WCS_DEVICEPROTOCOL PROTOCOL(this IProtocol obj)        {            return (obj as ProtocolProxyBase).Protocol;        }        public static T Create<T>(this WCS_DEVICE source)        {            return (T)Activator.CreateInstance(typeof(T), source);        }        public static Device<T> Device<T>(this WCS_DEVICE source) where T : IProtocol        {            var res = (Device<T>)Activator.CreateInstance(typeof(Device<T>), source);            return res;        }        public static Device<T, T2> Device<T, T2>(this WCS_DEVICE source) where T : IProtocol where T2 : IProtocol        {            return (Device<T, T2>)Activator.CreateInstance(typeof(Device<T, T2>), source);        }        public static Device<T, T2, T3> Device<T, T2, T3>(this WCS_DEVICE source) where T : IProtocol where T2 : IProtocol where T3 : IProtocol        {            return (Device<T, T2, T3>)Activator.CreateInstance(typeof(Device<T, T2, T3>), source);        }        public static DeviceGroup<T> DeviceGroup<T>(this WCS_DEVICE source) where T : IProtocol        {            return (DeviceGroup<T>)Activator.CreateInstance(typeof(DeviceGroup<T>), source);        }        public static DeviceGroup<T, T2> DeviceGroup<T, T2>(this WCS_DEVICE source) where T : IProtocol where T2 : IProtocol        {            return (DeviceGroup<T, T2>)Activator.CreateInstance(typeof(DeviceGroup<T, T2>), source);        }        public static DeviceGroup<T, T2, T3> DeviceGroup<T, T2, T3>(this WCS_DEVICE source) where T : IProtocol where T2 : IProtocol where T3 : IProtocol        {            return (DeviceGroup<T, T2, T3>)Activator.CreateInstance(typeof(DeviceGroup<T, T2, T3>), source);        }        public static DateTime GetUpdateTime(this IProtocol source)        {            dynamic obj = source;            return obj.UpdateTime;        }    }}
 |