开发中经常碰到这样的场景,从数据库查询出来全部的字段,但是有些字段是不想给 客户端看到,这时就需要将属性从DAO复制到传给客户端的DTO对象,如果采用get/set, 那显得很麻烦.可使用反射实现. Spring框架的 org.springframework.beans.BeanUtils 类帮我们实现了这个功能. 例子 @Data class Source{ private String name; private int age; public Source(String name, in
实体类转换成XML方法: 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化 public static string XmlSerialize<T>(T obj) { using (System.IO.StringWriter sw = new StringWriter()) { Type t = obj.GetType(); System.Xml.Serialization.XmlSerializer serializer = new Syst
最近在使用JSON.toJSONString过程中出现实体类的属性与转换之前的顺序不一致 public static void main(String[] args) { Person person = new Person(); person.setName("Tom"); person.setAge(20); person.setGender("Male"); person.setHeight(180.5); person.setWeight(80.5); Sy
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; namespace Common { public class ModelHandler<T> where T : new() { #region DataTable转换成实体类 /// <summary> /// 填充对象列