反射 与 特性 与动态数据解析 针对与Byte类型

2022-05-31 10:29:54 浏览数 (2)

下面是工作记录,用于与硬件对接,都是16进制数据处理

[类与数据] [数据与类] 的来回转换

要与添加有特性的类结合使用

可支持多字节与单字节解析 

这属于工作记录,不懂可以联系我哦

【特性定义】

代码语言:javascript复制
    /// <summary>
    /// 记录截取位
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
    public class TruncateLocationAttribute : Attribute
    {
        /// <summary>
        /// 记录截取位 
        /// </summary>
        /// <param name="StartLocation">开始位</param>
        /// <param name="StopLocation">结束位</param>
        public TruncateLocationAttribute(int StartLocation, int StopLocation)
        {
            this.StartLocation = StartLocation;
            this.StopLocation = StopLocation;
        }
        /// <summary>
        /// 开始位
        /// </summary>
        public int StartLocation { get; private set; }
        /// <summary>
        /// 结束位
        /// </summary>
        public int StopLocation { get; private set; }
        public TruncateLocationAttribute(int Location)
        {
            this.Location = Location;
        }
        /// <summary>
        /// 位
        /// </summary>
        public int Location { get; private set; }
    }

【类定义】

代码语言:javascript复制
        /// <summary>
        /// 设备数据结构体
        /// </summary>
        public class DevDataStructuralBody
        {
            /// <summary>
            /// 存储状态(0~3) 
            /// </summary>
            [TruncateLocation(0, 3)]
            public byte[] saveState { get; set; }
            /// <summary>
            /// 本机IP地址(4~7)
            /// </summary>
            [TruncateLocation(4, 7)]
            public byte[] IP_addr { get; set; }
            /// <summary>
            /// 设备类型(8)
            /// </summary>
            [TruncateLocation(8)]
            public byte TYPE { get; set; }
            /// <summary>
            /// CAN波特率(9)
            /// </summary>
            [TruncateLocation(9)]
            public byte CANBaud { get; set; }
           
            /// <summary>
            /// 通过属性名获取属性值
            /// </summary>
            /// <param name="propertyName">属性名称</param>
            /// <returns></returns>
            public object GetValue(string propertyName)
            {
                return this.GetType().GetProperty(propertyName).GetValue(this, null);
            }
        }

【方法】

代码语言:javascript复制
        /// <summary>
        /// 通过属性名获取特性值
        /// </summary>
        /// <param name="AttributeName">属性名</param>
        /// <returns></returns>
        object GetAttributeValue(string AttributeName)
        {
            var FieldIfno = typeof(DevDataStructuralBody).GetProperty(AttributeName).GetCustomAttribute<TruncateLocationAttribute>();
            if (FieldIfno.Location != 0)
            {
                return FieldIfno.Location.ToString();
            }
            else
            {
                return new Tuple<int, int>(FieldIfno.StartLocation, FieldIfno.StopLocation);
            }
        }

        /// <summary>
        ///  获取类中的字段
        /// </summary>
        /// <returns>所有字段名称</returns>
        List<string> GetFields<T>(T t)
        {
            List<string> ListStr = new List<string>();
            foreach (var item in t.GetType().GetProperties())
            {
                ListStr.Add(item.Name.ToString());
            }
            return ListStr;
        }



         /// <summary>
        /// 为指定对象分配参数
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="dic">字段/值</param>
        /// <returns></returns>
        public static T Assign<T>(Dictionary<string, object> dic) where T : new()
        {
            Type myType = typeof(T);
            T entity = new T();
            var fields = myType.GetProperties();
            object val;
            object obj;
            foreach (var field in fields)
            {
                if (!dic.Keys.Contains(field.Name))
                    continue;
                val = dic[field.Name.ToString()];
                obj = Convert.ChangeType(val, field.PropertyType);
                field.SetValue(entity, obj, null);
            }
            return entity;
        }


        /// <summary>
        /// 数据转结构体
        /// </summary>
        /// <param name="DeleteHead"></param>
        /// <returns></returns>
        Dictionary<string, object> DataTurnStructuralBody(byte[] DeleteHead)
        {
            Dictionary<string, object> DataList = new Dictionary<string, object>();
            List<string> AttributeNames = GetFields<CanComData.DevDataStructuralBody>(new DevDataStructuralBody());
            foreach (var item in AttributeNames)
            {
                var Data = GetAttributeValue(item);
                //只占一个字节
                string One = Data as string;
                if (One != null)
                {
                    DataList.Add(item, DeleteHead[int.Parse(One)]);
                }
                //占多个字节
                Tuple<int, int> Two = Data as Tuple<int, int>;
                if (Two != null)
                {
                    int Num = (Two.Item2 - Two.Item1)   1;
                    byte[] bytes = new byte[Num];
                    for (int i = 0; i < Num; i  )
                    {
                        bytes[i] = DeleteHead[Two.Item1   i];
                    }
                    DataList.Add(item, bytes);
                }
            }

            return DataList;
        }







        /// <summary>
        /// 结构体转数据
        /// </summary>
        /// <param name="DeleteHead"></param>
        /// <returns></returns>
        byte[] StructuralBodyTurnData(DevDataStructuralBody Boty)
        {
            byte[] Datas = new byte[100];
            List<string> AttributeNames = GetFields<DevDataStructuralBody>(Boty);
            foreach (var item in AttributeNames)
            {
               var Value= Boty.GetValue(item);
                var Attribute = GetAttributeValue(item);
                //只占一个字节
                string One = Attribute as string;
                if (One != null)
                {
                    Datas[int.Parse(One)] = (byte)Value;
                }
                //占多个字节
                Tuple<int, int> Two = Attribute as Tuple<int, int>;
                if (Two != null)
                {
                    int Num = (Two.Item2 - Two.Item1)   1;
                    byte[] bytei = (byte[])Value;
                    for (int i = 0; i < Num; i  )
                    {
                        Datas[Two.Item1   i] = bytei[i];
                    }
                }
            }
            return Datas;
        }

“关注[顺网]微信公众号,了解更多更有趣的实时信息”

本文作者:[博主]大顺

本文链接:https://shunnet.top/Zv6rMn

版权声明:转载注明出处,谢谢

0 人点赞