一、使用

  class Program
{
static void Main(string[] args)
{ User u1 = new User(); u1.UserName = "aaaaaaa";
u1.Salary = 89.63m;
u1.BirthDay = DateTime.Now;
u1.Age = ; User u2 = new ObjectMapping<User, User>().Mapping(u1); Console.WriteLine("U2.UserName="+u2.UserName);
Console.WriteLine("U2.Age=" + u2.Age);
Console.WriteLine("U2.Salary=" + u2.Salary);
Console.WriteLine("U2.BirthDay=" + u2.BirthDay); Console.Read(); }
}

二、类

/// <summary>
/// 两个对象之间的映射
/// 此类未经优化,只是一个核心实现
/// </summary>
public class ObjectMapping<S, T> where S :class, new() where T:class, new()
{
public T Mapping(S source)
{
if (source == null)
{
return null;
} T t = Activator.CreateInstance<T>(); Type tType = t.GetType();
Type sType = source.GetType(); PropertyInfo[] proertyInfos = tType.GetProperties(); if (!proertyInfos.Any())
{
return null;
} foreach (var pro in proertyInfos)
{
var sMethodInfo = sType.GetMethod("get_" + pro.Name);
var tMethodInfo = tType.GetMethod("set_" + pro.Name); if (sMethodInfo == null || tMethodInfo == null)
{
continue;
} var sFunc = GetFuncByMethodInfo(sMethodInfo, source);
var taction = GetActionByMethodInfo(tMethodInfo, t); taction.DynamicInvoke(sFunc.DynamicInvoke());
} return t;
} /// <summary>
/// 获取 一个方法的 FUNC
/// </summary>
/// <param name="methodInfo"></param>
/// <param name="obj"></param>
/// <returns></returns>
public Delegate GetFuncByMethodInfo(MethodInfo methodInfo,Object obj)
{
var parameters = new List<Type> (); parameters.AddRange(methodInfo.GetParameters().Select(p => p.ParameterType)); parameters.Add(methodInfo.ReturnType); var funcType= Expression.GetFuncType(parameters.ToArray<Type>()); return Delegate.CreateDelegate(funcType, obj, methodInfo);
} /// <summary>
/// 获取一个方法的 ACTION
/// </summary>
/// <param name="methodInfo"></param>
/// <param name="obj"></param>
/// <returns></returns>
public Delegate GetActionByMethodInfo(MethodInfo methodInfo, Object obj)
{
var parameters = new List<Type>(); parameters.AddRange(methodInfo.GetParameters().Select(p => p.ParameterType)); var actionType = Expression.GetActionType(parameters.ToArray<Type>()); return Delegate.CreateDelegate(actionType, obj, methodInfo);
} }

相关知识:

Delegate.CreateDelegate 方法

创建指定类型的委托。

命名空间:   System
程序集:  mscorlib(位于 mscorlib.dll)

  名称 说明
CreateDelegate(Type, MethodInfo)

创建指定类型的委托以表示指定的静态方法。

CreateDelegate(Type, MethodInfo, Boolean)

使用针对绑定失败的指定行为,创建用于表示指定静态方法的指定类型的委托。

CreateDelegate(Type, Object, MethodInfo)

使用指定的第一个参数创建指定类型的委托,该委托表示指定的静态方法或实例方法。

CreateDelegate(Type, Object, MethodInfo, Boolean)

使用指定的第一个参数和针对绑定失败的指定行为,创建表示指定的静态方法或实例方法的指定类型的委托。

CreateDelegate(Type, Object, String)

创建指定类型的委托,该委托表示要对指定的类实例调用的指定实例方法。

CreateDelegate(Type, Object, String, Boolean)

创建指定类型的委托,该委托表示要按指定的大小写敏感度对指定类实例调用的指定实例方法。

CreateDelegate(Type, Object, String, Boolean, Boolean)

使用用于指定是否区分大小写的值和针对绑定失败的指定行为,创建指定类型的委托,该委托表示要对指定类实例调用的指定实例方法。

CreateDelegate(Type, Type, String)

创建指定类型的委托,该委托表示指定类的指定静态方法。

CreateDelegate(Type, Type, String, Boolean)

使用用于指定是否区分大小写的值创建指定类型的委托,该委托表示指定类的指定静态方法。

CreateDelegate(Type, Type, String, Boolean, Boolean)

使用用于指定是否区分大小写的值和针对绑定失败的指定行为,创建指定类型的委托,该委托表示指定类的指定静态方法。

Expression.TryGetFuncType 方法 (Type[], Type)

 

创建一个 Type 对象,它表示具有特定类型参数的泛型 System.Func 委托类型。 最后一个类型参数指定已创建委托的返回类型。

命名空间:   System.Linq.Expressions
程序集:  System.Core(位于 System.Core.dll)

.Net 两个对像之间的映射 ( 二 )的更多相关文章

  1. .Net 两个对像之间的映射

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. .Net 两个对像之间的映射 (一 )

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. windows server/windows同一系统下建立两个目录之间的映射关系

    应用场景,如下: 当两个不同的项目共享同一个资源目录.同一个数据库时,由于两项目根目录不一样,再加上部分项目可能有入口重写规则限制了用户的访问权限. 因此,我们可以利用window 服务器给我们提供的 ...

  4. 一步一步学EF系列1【Fluent API的方式来处理实体与数据表之间的映射关系】

    EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...

  5. ABP文档 - 对象与对象之间的映射

    文档目录 本节内容: 简介 IObjectMapper 接口 集成 AutoMapper 安装 创建映射 自动映射的特性 自定义映射 扩展方法 MapTo 单元测试 预定义的映射 Localizabl ...

  6. 修改Linux主机名与IP之间的映射关系

    linux主机版本: Distributor ID: UbuntuDescription: Ubuntu 14.10Release: 14.10 一.修改linux主机名 1.使用hostname命令 ...

  7. 两台linux服务器之间实现挂载

    https://blog.csdn.net/lpp_dd/article/details/78743862 两台linux服务器之间实现挂载: 服务端: 1.首先需要在主机上设置允许挂载的目录 (1) ...

  8. 一步一步学EF系列二【Fluent API的方式来处理实体与数据表之间的映射关系】

    EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...

  9. 两台Linux主机之间文件的复制

    使用scp命令可以实现两台Linux主机之间的文件复制,基本格式是: scp [可选参数] file_source file_target 1. 复制文件 命令格式: scp local_file r ...

随机推荐

  1. 沉淀再出发:java中的HashMap、ConcurrentHashMap和Hashtable的认识

    沉淀再出发:java中的HashMap.ConcurrentHashMap和Hashtable的认识 一.前言 很多知识在学习或者使用了之后总是会忘记的,但是如果把这些只是背后的原理理解了,并且记忆下 ...

  2. WinRAR(5.21)-0day漏洞-始末分析

    0x00 前言 上月底,WinRAR 5.21被曝出代码执行漏洞,Vulnerability Lab将此漏洞评为高危级,危险系数定为9(满分为10),与此同时安全研究人员Mohammad Reza E ...

  3. SOJ 4590 简单模拟

    Description Gandtom把家搬到了一个交通便利的地方.今天来通知他的朋友Sidney,但是Sidney好像不在家,出门了,敲门没有人开门.  于是Gandtom把家里的地址写了下来.他担 ...

  4. Debian下Cannot set LC_CTYPE to default locale: No such file or directory解决方法

    把语言环境变量改为英文 将Ubuntu系统语言环境改为英文的en_US.UTF-8 查看当前系统语言环境 locale 编辑配置文件,将zh_US.UTF-8改为en_US.UTF-8,zh改为en ...

  5. BZOJ5072:[Lydsy1710月赛]小A的树(树形DP)

    Description BZOJ只是扔了个下载链接 Solution 设$f[x][i]$表示$x$点选中$i$个黑点的最小连通块. 设$g[x][i]$表示$x$点选中$i$个黑点的最大连通块. 转 ...

  6. [Python 多线程] asyncio (十六)

    asyncio 该模块是3.4版本加入的新功能. 先来看一个例子: def a(): for x in range(3): print('a.x', x) def b(): for x in 'abc ...

  7. multimap详讲

    multimap和map的区别: 首先认识一下multimap和map的区别: 1>        multimap不提供operator[ ]运算符.因为这个运算符的语义在同一个键可以保存多个 ...

  8. Vue 子组件调用父组件 $emit

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...

  9. 信息: Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level

    四月 , :: 下午 org.apache.coyote.http11.AbstractHttp11Processor process 信息: Error parsing HTTP request h ...

  10. web前端开发从0到1—html结构与常用标签

    一:html文档标签结构 <html></html><!--文档片头信息,表示文档内容是用什么标签写的.--> <head></head>& ...