前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >反射 与 特性 与动态数据解析 针对与Byte类型

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

作者头像
Shunnet
发布2022-05-31 10:29:54
3250
发布2022-05-31 10:29:54
举报

下面是工作记录,用于与硬件对接,都是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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-04-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档