C# 将object类型转换成List_c# object转list-CSDN博客

public List<CariHesapEkstre> ConvertToDesiredType ( object list )
    {
        return ( (IEnumerable<dynamic>)list ).Select(item => new CariHesapEkstre
        {
            MutabakatDetayId = item.MutabakatDetayId,
            MutabakatVar = item.MutabakatVar,
        }).ToList();
    }

方法二,直接映射

        public static List<DispatchEntity> ConvertToDesiredType(object list)
        {
            List<DispatchEntity> returnList = new List<DispatchEntity>();

            foreach (var item in (IEnumerable<dynamic>)list)
            {
                DispatchEntity obj =JsonConvert.DeserializeObject<DispatchEntity>(item.ToString().Replace("{{","{").Replace("}}","}")); 
                DispatchEntity extract = new DispatchEntity();
                foreach (PropertyInfo prop in obj.GetType().GetProperties())
                {
                    foreach (PropertyInfo prop2 in extract.GetType().GetProperties())
                    {
                        if (prop2.Name == prop.Name)
                        {
                            prop2.SetValue(extract, prop.GetValue(obj));
                        }
                    }
                }

                returnList.Add(extract);
            }

            return returnList;
        }

原网址: 访问
创建于: 2024-06-21 15:30:29
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论