.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 ...
随机推荐
- 猴子选大王【PHP】
目录 猴子选大王 指针解决 数组压栈 猴子选大王 一群猴子排成一圈,按1,2,...,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去...,如此不 ...
- 沉淀再出发:ElasticSearch的中文分词器ik
沉淀再出发:ElasticSearch的中文分词器ik 一.前言 为什么要在elasticsearch中要使用ik这样的中文分词呢,那是因为es提供的分词是英文分词,对于中文的分词就做的非常不好了 ...
- 4星|《助推(实践版)》:英国政府用AB测试检验政策效果的经验
助推:小行动如何推动大变革(实践版)(诺贝尔经济学奖得主理查德•塞勒的助推实践) 作者作为学者说服英国政府实施助推策略的经过,提到一些具体主推策略. 所谓的助推,很像IT业流行的AB测试,对政策的执行 ...
- 低版本兼容使用Fragment (转)
Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持.以前的版本必须用兼容模式开发,本人在网上找了大量资料,终于找到些线索正常运行于2.1版本的安卓系统.现在浅说一下兼容版本使用Fr ...
- java继承-重写-super实例补充
方法重写: 是指子类根据需要父类继承来的方法进行改写,是多态机制的前奏. 重写注意点: 1.重写方法必须和被重写方法具有相同的方法名,参数列表和返回值. 2.重写方法方法不能使用比被重写方法更严格的访 ...
- [转] linux下 /etc/profile、~/.bash_profile ~/.profile的执行过程
分类: linux 2015-03-13 16:24 1572人阅读 评论(0) 收藏 举报linuxprofile关于登录linux时,/etc/profile.~/.bash_profile等几个 ...
- jq实现鼠标经过出现上拉菜单
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Codeforces Round #441 (Div. 2)【A、B、C、D】
Codeforces Round #441 (Div. 2) codeforces 876 A. Trip For Meal(水题) 题意:R.O.E三点互连,给出任意两点间距离,你在R点,每次只能去 ...
- CATransaction:原子化操作、批量操作、整体设置、自动添加
Transactions are CoreAnimation's mechanism for batching multiple layer- tree operations into atomic ...
- 【Vue.js】高仿饿了么外卖App(一)
1.架构从传统的MVC向REST API+前端MV*迁移 参考链接: http://blog.csdn.net/broadview2006/article/details/8615055 http:/ ...

