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. Excel英语成绩单

  2. 等待事件P1 P2 P3含义

    从以下两个视图中查到的session中,有P1,P2,P3参数select * from v$sessionselect * from v$session_waitselect * from v$se ...

  3. java查找字符串里与指定字符串相同的个数

    public class EmployeeDemo { //方法一: public int search(String str,String strRes) {//查找字符串里与指定字符串相同的个数 ...

  4. D3——scale

    d3.scale 比例尺 “Scales are functions that map from an input domain to an output range” Domains 定义域 和 R ...

  5. swift中的"类型擦除"

    代理模式.或者协议模式 因为swift泛型还不支持逆变和协变也就不会有真的类型擦除,而这里说的"类型擦除"是指:利用一个具体实现的通用泛型类(参看系统库的AnySequence), ...

  6. angularjs select下拉搜索框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 4、Dubbo-工程实践

    4.实践测试 4.1).提出需求 某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址: 我们现在 需要创建两个服务模块进行测试  测试预期结果: 订单服务web模块在A服务器,用户服务模块在 ...

  8. js 日历插件开发

    1.HTML完整代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  9. Selenium自动化测试值环境搭建

    Selenium自动化测试之环境搭建 一.背景介绍 自动化测试近几年在测试领域很火,出去面试要是说不会自动化测试薪资都不好意思往高了要!很多公司做敏捷测试用到自动化,其他一些公司也是跟风,即使用不上自 ...

  10. SVG图形的简单修改

    svg格式的图片是一种矢量图片,最近我就喜欢使用这种图片在做html的元素.网上也有很多现成的svg图片,比如:http://www.sfont.cn这个网站,就能很快的找到各种您想要的图片.但是下载 ...