.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 ...
随机推荐
- 性状、生成器、闭包、OPcache【Modern PHP】
目录 性状 Trait 生成器 闭包 Zend OPcache PHP发展这么多年,技术.架构都已经革新,了解现代PHP很重要,最近在看Model PHP这本书,系统的了解下PHP相关的概念. 性状 ...
- 乘风破浪:LeetCode真题_014_Longest Common Prefix
乘风破浪:LeetCode真题_014_Longest Common Prefix 一.前言 如何输出最长的共同前缀呢,在给定的字符串中,我们可以通过笨办法去遍历,直到其中某一个字符不相等了,这样就得 ...
- composer随笔
composer dump-autoload 生成autoload.php文件
- 阿里云 CentOS 镜像和 EPEL 源
配置阿里云网络yum源 阿里云镜像源地址http://mirrors.aliyun.com/ 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.re ...
- jquery 中 $.map 用法
$.map(data,function(item,index){return XXX}) 遍历data数组中的每个元素,并按照return中的计算方式 形成一个新的元素,放入返回的数组中 var b ...
- 使用android studio检测app内存泄漏【转载】
Android开发中难免会遇到各种内存泄漏,如果不及时发现处理,会导致出现内存越用越大,可能会因为内存泄漏导致出现各种奇怪的crash,甚至可能出现因内存不足而导致APP崩溃. 一般检测android ...
- Python的优雅写法
枚举 之前我们这样操作: Python 1 2 3 4 i = 0 for item in iterable: print i, item i += 1 现在我 ...
- BZOJ 1008 越狱 组合数学
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1008 题目大意: 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗 ...
- 2594. [WC2006]水管局长数据加强版【LCT+最小生成树】
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- robotframwork接口测试(三)—HttpLibrary.HTTP安装
1. 安装: 1)可以用命令:pip install robotframework-httplibrary(或pip install --upgrade robotframework-httplib ...

