.Net 两个对像之间的映射 ( 二 )
一、使用
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 两个对像之间的映射 ( 二 )的更多相关文章
- .Net 两个对像之间的映射
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- .Net 两个对像之间的映射 (一 )
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- windows server/windows同一系统下建立两个目录之间的映射关系
应用场景,如下: 当两个不同的项目共享同一个资源目录.同一个数据库时,由于两项目根目录不一样,再加上部分项目可能有入口重写规则限制了用户的访问权限. 因此,我们可以利用window 服务器给我们提供的 ...
- 一步一步学EF系列1【Fluent API的方式来处理实体与数据表之间的映射关系】
EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...
- ABP文档 - 对象与对象之间的映射
文档目录 本节内容: 简介 IObjectMapper 接口 集成 AutoMapper 安装 创建映射 自动映射的特性 自定义映射 扩展方法 MapTo 单元测试 预定义的映射 Localizabl ...
- 修改Linux主机名与IP之间的映射关系
linux主机版本: Distributor ID: UbuntuDescription: Ubuntu 14.10Release: 14.10 一.修改linux主机名 1.使用hostname命令 ...
- 两台linux服务器之间实现挂载
https://blog.csdn.net/lpp_dd/article/details/78743862 两台linux服务器之间实现挂载: 服务端: 1.首先需要在主机上设置允许挂载的目录 (1) ...
- 一步一步学EF系列二【Fluent API的方式来处理实体与数据表之间的映射关系】
EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...
- 两台Linux主机之间文件的复制
使用scp命令可以实现两台Linux主机之间的文件复制,基本格式是: scp [可选参数] file_source file_target 1. 复制文件 命令格式: scp local_file r ...
随机推荐
- August 16th 2017 Week 33rd Wednesday
A man can be destroyed but not defeated. 一个人可以被毁灭,但不能被打败. Before he was destroyed, he would have bee ...
- web安全职位面试题目汇总
Domain 解释一下同源策略 同源策略,那些东西是同源可以获取到的 如果子域名和顶级域名不同源,在哪里可以设置叫他们同源 如何设置可以跨域请求数据?jsonp是做什么的? Ajax Ajax是否遵循 ...
- java抽象继承-模板方法
//模板方法:步骤提前设计好,用的时候只需要改步骤内容 public class TemplateDemo { public static void main(String[] args) { // ...
- Java的Calendar类
通过Date类我们可以创建并格式化一个日期对象,但是如何才能设置和获取日期数据的特定部分呢?----Calendar类 Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,通过getIn ...
- redis.conf 具体配置详解
redis.conf 具体配置详解 # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => ...
- 3109. [CQOI2013]新数独【DFS】
Description Input 输入一共15行,包含一个新数独的实例.第奇数行包含左右方向的符号(<和>),第偶数行包含上下方向的符号(^和v). Output 输出包含9行,每行 ...
- BZOJ1468:Tree(点分治)
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- ubuntu16.04 Detectron目标检测库配置(包含GPU驱动,Cuda,Caffee2等配置梳理)
Detectron概述 Detectron是Facebook FAIR开源了的一个目标检测(Object Detection)平台. 用一幅图简单说明下Object Detection.如Mask R ...
- Dubbo实践(四)设计模式
Dubbo框架在初始化和通信过程中使用了多种设计模式,可灵活控制类加载.权限控制等功能. 工厂模式 Provider在export服务时,会调用ServiceConfig的export方法.Servi ...
- JS如何截取-后面的字符串
str为要截取的字符串 通过获取字符串中“-”的坐标index,其他特殊字符以此类推 var index=str.lastIndexOf("\-"); str=str.subst ...

