.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 ...
随机推荐
- ajax post data 获取不到数据
ajax post data 获取不到数据,注意 content-type的设置 .post/get关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的.好吧今天我也遇到了,网 ...
- C语言程序员必读的5本书
本文由 伯乐在线 - programmer_lin 翻译自 fromdev.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. 你正计划着通过看书来学习C语言吗?“书籍是人类最忠诚的朋友“.海明威一定 ...
- langularJs的MVC模式
1.数据的挂载 在函数中设置数据 function Aaa($scope){ $scope.name = 'hello'; $scope.age = '20'; } 2.ng-controller 这 ...
- python各种模块的使用
Pexpect模块:http://www.ibm.com/developerworks/cn/linux/l-cn-pexpect1/ ConfigParser模块:http://blog.china ...
- python SimpleHTTPServer
Python2 使用的是SimpleHTTPServer python -m SimpleHTTPServer Python3 合并到了http.server python -m http.serve ...
- 1、Android-活动(上)
1.1.活动是什么 活动(Activity)是最容易吸引用户的地方,他是一种可以包含用户界面的组件 主要用于和用户进行交互 一个用户可以包含零个或多个活动,不包含活动的程序少见 1.2.活动的基本用法 ...
- face++
1.链表反转 2.快排 3.m*k n*k两矩阵计算欧几里得距离np.tile 4.链表排序,要求时间复杂度小于O(N^2),空间O(1),不允许改变链表的值 5.2sum及其变体 6.给一个数组 ...
- jquery mobile各类组件刷新方法
1.Combobox or select dropdowns var myselect = $("#sCountry"); myselect[0].selectedIndex ...
- ROS C++ 规范概要
一.动机 代码一致才能可读.联调.高效率.高复用.可移植性. 二.命名方式 CamelCased camelCased under_scored ALL_CAPITALS 2.1 Package命名方 ...
- PHP+JQUERY+AJAX上传、裁剪图片(2)
<script type="text/javascript"> var imgCut = { imgOpt : { imgPrototypeId : 'imgProto ...

