推倒障碍成浮桥 2022-05-09 17:19 采纳率: 16.7%
浏览 177

如何在C#中,将 字符串转换成实体对象,对象中包含List<T> 属性

实体类,例:
    //自定义特性
     public class StringRangeAttribute : BaseAttribute
    {
        public int Length { get; set; }    //约束属性长度
        public int Index { get; set; }
    }
    //实体类
    public class ProcessInquiryReceive:BaseOut
    {
        [StringRange(Length = 13, Index = 5)]
        public string procdata_id { get; set; }
        [StringRange(Length = 3, Index = 6)]
        public string arycnt1 { get; set; }
        [StringRange(Index = 7)]
        public List<ProcessInquiry> ary1 { get; set; }
    }
    [Serializable]
    public class ProcessInquiry
    {
        /// <summary>
        /// Process data item Length=10
        /// </summary>
        [StringRange(Length = 10)]
        public string procdata { get; set; }
        /// <summary>
        /// Item SEQ No Length=4
        /// </summary>
        [StringRange(Length = 4)]
        public string item_seq_no { get; set; }
    }

我的解答思路和尝试过的方法

字符串:【1 50 4 61 3 5 123 789 130651 11112222222222222233333333333333】

        public static void GetStrToEntity<T>(this T t,string Str)
        {
            try
            {
                //获取类型
                Type type = t.GetType();
                //获取对象全部属性
                PropertyInfo[] properties = type.GetProperties();
                //进行属性排序
                var PropertyByIndex = properties.OrderBy(p => (p.GetCustomAttributes(typeof(StringRangeAttribute), false).FirstOrDefault() as StringRangeAttribute).Index).ToArray();
                foreach (var item in PropertyByIndex)
                {
                    //获取该项 是否是泛型
                    var IsGenericType = item.PropertyType.IsGenericType;
                    //获取该项 实现或继承接口对象
                    var Lis = item.PropertyType.GetInterface("IEnumerable", false);
                    if (IsGenericType && Lis != null)
                    {
                        //将泛型集合中全部属性,转换成单个object对象
                        var ListVal = item.GetValue(t, null) as IEnumerable<object>;
                        if (ListVal == null)
                            continue;
                        //循环
                        foreach (var prop in ListVal)
                        {
                            prop.GetStrToEntity(Str);
                        }
                    }
                    else
                    {
                        //获取属性约束长度
                        int Len = (item.GetCustomAttributes(typeof(StringRangeAttribute), false).FirstOrDefault() as StringRangeAttribute).Length;
                            //GetCustomAttributes(typeof(StringRangeAttribute), false).FirstOrDefault() as StringRangeAttribute).Index
                        //设置长度
                        item.SetValue(t, Str.Substring(0,Len), null);
                        Str = Str.Substring(Len, Str.Length - Len);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger..Error("GetStrToEntity  Error " + ex.Message);
            }
        }
//调用
 ProcessInquiryReceive processInquiryReceive = new ProcessInquiryReceive()
            {
                trx_id = "1   5",
                type_id = "0",
                retcode1 = "   4 6",
                sqlerrcode = "1 3 5 ",
                procdata_id = "123   789  13",
                arycnt1 = "065",
                ary1 = new List<ProcessInquiry>
                {
                    new ProcessInquiry(){ procdata="",item_seq_no=""},
                    new ProcessInquiry(){ procdata="",item_seq_no=""},
                    new ProcessInquiry(){procdata="",item_seq_no=""}
                }
            };
processInquiryReceive .GetStringToEntity(new list<string>());

这种调用方式,必须提前创建好指定数量的 List,但是数量是不固定的,有其他方法可以解决吗?

  • 写回答

2条回答 默认 最新

  • Null_Reference 2022-05-09 17:28
    关注

    把字符串固定 如果没有,用一个固定的值占位也可以。不然程序没法判断

    评论

报告相同问题?

问题事件

  • 修改了问题 5月9日
  • 创建了问题 5月9日

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?