java-List<实体>怎么转换成List<Object>

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java-List<实体>怎么转换成List<Object>相关的知识,希望对你有一定的参考价值。

参考技术A List<String> string = new ArrayList<String>();
ist<Object> objectList = new ArrayList<Object>(string);

本回答被提问者采纳

C# 将DataTable数据源转换成实体类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Reflection;
 5 
 6 /// <summary>
 7 /// 将DataTable数据源转换成实体类
 8 /// </summary>
 9 /// <typeparam name="T">实体</typeparam>
10 public static class ToModel<T> where T : new()
11 {
12     /// <summary>
13     /// 将DataTable数据源转换成实体类
14     /// </summary>
15     public static List<T> ConvertToModel(DataTable dt)
16     {
17         List<T> ts = new List<T>();// 定义集合
18         foreach (DataRow dr in dt.Rows)
19         {
20             T t = new T();
21             PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
22             foreach (PropertyInfo pi in propertys)
23             {
24                 if (dt.Columns.Contains(pi.Name))
25                 {
26                     if (!pi.CanWrite) continue;
27                     var value = dr[pi.Name];
28                     if (value != DBNull.Value)
29                     {
30                         switch (pi.PropertyType.FullName)
31                         {
32                             case "System.Decimal":
33                                 pi.SetValue(t, decimal.Parse(value.ToString()), null);
34                                 break;
35                             case "System.String":
36                                 pi.SetValue(t, value.ToString(), null);
37                                 break;
38                             case "System.Int32":
39                                 pi.SetValue(t, int.Parse(value.ToString()), null);
40                                 break;
41                             default:
42                                 pi.SetValue(t, value, null);
43                                 break;
44                         }
45                     }
46                 }
47             }
48             ts.Add(t);
49         }
50         return ts;
51     }
52 }

 

以上是关于java-List<实体>怎么转换成List<Object>的主要内容,如果未能解决你的问题,请参考以下文章

.net WebService接口参数为实体类数组,java调用接口怎么把参数转换成string类型的xml内容

将 java-List<T> 转换为可用于 iomicrometer 的 MultiGauge 的流

C# DataTable转换成实体列表 与 实体列表转换成DataTable

java 如何将List<Object[]>转换成List<实体>?

javascript 怎么将html字符实体 转换成 正常显示的字符

DataTable转换成实体