实体类与实体DTO类之间的转换

1.通过使用第三方序列化反序列化工具Newtonsoft.Json

通过Nuget包管理器添加Newtonsoft.Json



构造一个Trans的静态方法:

public static TOut Trans<TIn,TOut>(TIn tIn)
{
return JsonConvert.DeserializeObject<TOut>(JsonConvert.SerializeObject(tIn));
}

前端调用:

People people = new People
{
Id = 1,
Name = "ddd",
Class = 1 };
PeopleCopy peopleCopy = SerializeMapper.Trans<People, PeopleCopy>(people);

2.通过反射实现

构造一个Trans的静态方法:

public static TOut Trans<TIn,TOut>(TIn tIn)
{
TOut tOut = Activator.CreateInstance<TOut>();
foreach (var outfield in tOut.GetType().GetFields())
{
foreach (var infield in tIn.GetType().GetFields())
{
if (outfield.Name.Equals(infield.Name))
{
outfield.SetValue(tOut, infield.GetValue(tIn));
break;
}
}
}
foreach (var outProperty in tOut.GetType().GetProperties())
{
foreach (var inProperty in tIn.GetType().GetProperties())
{
if (outProperty.Name.Equals(inProperty.Name))
{
outProperty.SetValue(tOut,inProperty.GetValue(tIn));
break;
}
} }
return tOut;
}

前端调用:

People people = new People
{
Id = 1,
Name = "ddd",
Class = 1 };
PeopleCopy peopleCopy = ReflectionMapper.Trans<People, PeopleCopy>(people);

3.通过表达式目录树加字典缓存实现

静态字典:

public static Dictionary<string, Object> _Dictionary = new Dictionary<string, object>();

构造一个Trans的静态方法:

public static TOut Trans<TIn, TOut>(TIn tIn)
{
TOut tOut = Activator.CreateInstance<TOut>();
string key = string.Format("funckey_{0}_{1}", tIn.GetType().Name, tOut.GetType().Name);
if (!_Dictionary.ContainsKey(key))
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
List<MemberBinding> memberBindingsList = new List<MemberBinding>();
foreach (var item in typeof(TOut).GetProperties())
{
MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingsList.Add(memberBinding);
}
foreach (var item in typeof(TOut).GetFields())
{
MemberExpression filed = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
MemberBinding memberBinding = Expression.Bind(item, filed);
memberBindingsList.Add(memberBinding);
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingsList.ToArray());
Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
{
parameterExpression
});
Func<TIn, TOut> func = lambda.Compile();
_Dictionary[key] = func;
}
return ((Func<TIn, TOut>)_Dictionary[key]).Invoke(tIn);
}

前端调用:

People people = new People
{
Id = 1,
Name = "ddd",
Class = 1 };
PeopleCopy peopleCopy = ExpressionMapper.Trans<People, PeopleCopy>(people);

4. 通过表达式目录树加泛型缓存实现(静态构造函数)

构造一个泛型缓存类:

 public class ExpressionGenericMapper<TIn,TOut>
{
private static Func<TIn, TOut> _FUNC = null;
static ExpressionGenericMapper()
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn),"p");
List<MemberBinding> memberBindingsList = new List<MemberBinding>();
foreach (var item in typeof(TOut).GetProperties())
{
MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingsList.Add(memberBinding);
}
foreach (var item in typeof(TOut).GetFields())
{
MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingsList.Add(memberBinding);
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingsList.ToArray());
Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
{
parameterExpression
});
_FUNC = lambda.Compile();//拼装是一次性的
} public static TOut Trans(TIn tIn)
{
return _FUNC(tIn);
}
}

前端调用:

People people = new People
{
Id = 1,
Name = "ddd",
Class = 1 };
PeopleCopy peopleCopy = ExpressionGenericMapper.Trans<People, PeopleCopy>(people);

原文地址:https://blog.csdn.net/m0_37591671/article/details/80386122

实体类与实体DTO类之间的转换的更多相关文章

  1. OpenCV图片类cv::Mat和QImage之间进行转换(好多相关文章)

    在使用Qt和OpenCV混合编程时,我们有时需要在两种图片类cv::Mat和QImage之间进行转换,下面的代码参考了网上这个帖子: //##### cv::Mat ---> QImage ## ...

  2. 使用FastJson对实体类和Json还有JSONObject之间的转换

    1. 实体类或集合转JSON串 String jsonString = JSONObject.toJSONString(实体类); 2.JSON串转JSONObject JSONObject json ...

  3. C++四种类型之间的转换

    C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a. C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. const_cas ...

  4. 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?

    当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...

  5. ado.net 实体类_数据访问类

    实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  6. 9_13学习完整修改和查询&&实体类,数据访问类

    完整修改和查询:中间变量运用. 1.先查 2.执行操作 ---------------------------------------------------- namespace ADO.NET_小 ...

  7. .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类

    在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...

  8. ADO,NET 实体类 和 数据访问类

    啥也不说,看代码. --SQl中 --建立ren的数据库,插入一条信息 create database ren go use ren go create table xinxi ( code ) pr ...

  9. ADO.NET(完整修改和查询、实体类,数据访问类)

    一.完整修改和查询 在编写c#语句时需考虑到用户体验,例如在编写修改语句时,需要考虑到输入的内容在数据库中是否能够找到. 中间变量运用. 1.先查 2.执行操作 完整修改语句: bool has = ...

随机推荐

  1. checkbox:click事件触发文本框显示隐藏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Es6学习笔记(7)----数组的扩展

    参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ 数组的扩展 1.扩展运算符:可以将数组转化成逗号隔离的单个参数...[1,2,3] //控制台运 ...

  3. SQL中的SELECT_简单查询语句总结

    --以scott用户下的dept和emp表为例 --注意:如果scott用户不能使用,请使用system用户登录--解锁scott用户ALTER USER SCOTT ACCOUNT UNLOCK;- ...

  4. 杨辉三角python的最佳实现方式,牛的不能再牛了

    def triangles(): N = [1] while True: yield N N.append(0) N = [N[i-1] + N[i] for i in range(len(N))] ...

  5. ubuntu下安装mongo扩展

    安装openssl apt-get install openssl libssl-dev libssl0.9.8 libgtk2.0-dev 安装php-pear apt-get install ph ...

  6. Android(java)学习笔记178:多媒体之计算机图形表示方式

    1. 多媒体 很多媒体:文字(TextView,简单不讲),图片,声音,视频等等.   2. 图片 计算机如何表示图片的? (1)bmp 高质量保存    256色位图:图片中的每个像素点可以有256 ...

  7. TIOJ1208 第K大连续和

    第k大的题一般都有点麻烦 pbds库的tree,需要研究一下https://codeforces.com/blog/entry/11080find_by_order() and order_of_ke ...

  8. Lampiao(dirtycow)脏牛漏洞复现

    nmap扫描内网80端口发现目标主机 nmap -sP   -p 80 192.168.31.0/24 扫描发现目标主机开放22端口.并且  1898端口开放http服务 御剑扫描目录并访问之后发现存 ...

  9. qobject_cast

    void QLadderDiagramItem::GetMainForm(DoType sourceType){ for each (QWidget *w in qApp->topLevelWi ...

  10. clone对象或数组

    function clone(obj) { var o; if (typeof obj == "object") { if (obj === null) { o = null; } ...