using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Reflection; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
UserModel u = new UserModel(); u.UserName = "jinshuai";
u.Age = ; IdentityModel im = new IdentityModel(); im= ConvertType<PropertyMapAttribute,IdentityModel,UserModel>(u,im); Console.WriteLine(im.userName); Console.WriteLine(im.age); Console.Read(); } static T2 ConvertType<T1,T2,T3>(T3 SourceObj,T2 DestObj)
{
Type imt = DestObj.GetType();
Type ut = SourceObj.GetType(); PropertyInfo[] Parray = imt.GetProperties(); PropertyInfo[] UParray = ut.GetProperties(); foreach (PropertyInfo p in Parray)
{
PropertyInfo propert = imt.GetProperty(p.Name); object[] atarray = p.GetCustomAttributes(typeof(T1), true); var data = atarray.FirstOrDefault(); dynamic a = (T1)data; if (a != null)
{
foreach (PropertyInfo Up in UParray)
{
if (a.DestName == propert.Name && Up.Name == a.SourceName)
{
propert.SetValue(DestObj, Up.GetValue(SourceObj, null), null);
}
}
}
} return DestObj; }
} public class UserModel { public string UserName { get; set; }
public int Age { get; set; } } public class IdentityModel
{
[PropertyMapAttribute(SourceName = "UserName", DestName = "userName")]
public string userName { get; set; } [PropertyMapAttribute(SourceName = "Age", DestName = "age")]
public int age { get; set; } } [AttributeUsage( AttributeTargets.Property )]
public class PropertyMapAttribute:Attribute
{
public string SourceName { get; set; }
public string DestName { get; set; } }
}

本代码只提供一种思路........

具体可能还有其它方法......

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

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

    一.使用 class Program { static void Main(string[] args) { User u1 = new User(); u1.UserName = "aaa ...

  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 ee@ Myeclipse 2015 stable 1.0 完美破解方法

    Myeclipse 2015 stable 1.0 完美破解方法 破解步骤: 使用以前的注册机算号,版本选择Blue即可,后续可解锁Spring高级功能,即Bling的所有功能全部具备 1.1 进入m ...

  2. Read Asia Embedded fell

    first and foremost, 很久没写了,心痒了,手贱了,于是乎在这一刻心静时,积攒的思绪开始回放了,惊世Copy-on-write之文随之面世了; 臭毛孩子拉的 屎 特臭,小毛孩子前途黯淡 ...

  3. 转载ASP.NET 状态管理Application,Session,Cookie和ViewState用法

    转载原地址 http://www.cnblogs.com/cuishao1985/archive/2009/09/24/1573403.html ASP.NET状态管理 APPlication,Ses ...

  4. ltt.js

    var dailyBox = $('.daily-box-office'), curDate = new Date(), curYear = curDate.getFullYear(), curMon ...

  5. java线程知识点

    1. 进程与线程 一个进程拥有多个线程,多个线程共享进程的内存块.操作系统不分配新的内存,因此线程之间通信很容易.不同的进程因处于不同的内存块,因此进程之间通信较为困难. 进程:每个进程都有独立的代码 ...

  6. PHP 文件包含之文件路径截断(转)

    PHP 文件包含之文件路径截断 以下是网络摘要: 1. 本来还以为挖到金矿了,跟黑哥交流后发现只能应用于Win32平台,使这个BUG的威力暴减,基本没有太大危害了,因为在WIN32平台使用PHP的实在 ...

  7. C#-创建自定义双击事件

    .NET Compact Framework 不支持按钮的 Windows 窗体 DoubleClick 事件.但是您可以创建一个从 Button 类派生的控件来实现该事件. 创建自定义双击事件 创建 ...

  8. 树套树专题——bzoj 3110: [Zjoi2013] K大数查询 &amp; 3236 [Ahoi2013] 作业 题解

    [原题1] 3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 978  Solved: 476 Descri ...

  9. IOS - 常用宏定义和功能方法

    可能不定期添加新的东西 github地址:https://github.com/yuqingzhude/CommonUseDemo /************************Tools**** ...

  10. ios开发——实用技术篇Swift&Swift调用C、C++、Object

    Swift调用C.C++.Object 1.Swift调用C语言a,首先在项目中添加 CFile 文件命名为CHello,同时产生桥梁文件. b,创建之后的项目结构 b,在CHello.h文件中编写接 ...