当前位置: 首页 > 工具软件 > Object Shell > 使用案例 >

C# 将object类型转换成List

华心思
2023-12-01
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;
        }
 类似资料: