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. wuzhicms短信API 实例调用

    1.接口调用 $sendsms = load_class('sms','sms'); echo $sendsms->send_sms('18911549611', '888888', 1); / ...

  2. uva 11468 Substring

    题意:给你 k 个模板串,然后给你一些字符的出现概率,然后给你一个长度 l ,问你这些字符组成的长度为 l 的字符串不包含任何一个模板串的概率. 思路:AC自动机+概论DP 首先用K个模板构造好AC自 ...

  3. Windows下Android SDK Manage下载速度缓慢的解决方法

    在SDK Manager下Tools->Options打开了SDK Manager的Settings,选中“Force https://… sources to be fetched using ...

  4. angular中的promise

    angular中的promise用法 标签(空格分隔): angular 前言 Promise其实是一个规范,用类似then().then()这样的链式调用形式来处理因为异步带来意大利面条式的代码(多 ...

  5. SVG 矢量图形格式

    SVG(Scalable Vector Graphics)是基于 XML 的一种矢量图形格式,它即可以作为单独的图形文件使用也可以嵌入到网页中并由 JavaScript 来操作,非常方便和灵活.SVG ...

  6. eclipse项目导入到Android Studio Plugin with id 'android-library' not found

    在主项目的build.gradle 中加入以下代码buildscript { repositories { mavenCentral() } dependencies { classpath 'com ...

  7. ios项目不能再用UDID了

    今天更新项目时,出现 Apps are note permitted to access the UDID and must not use the uniqueIdentifier method o ...

  8. Windows Phone开发工具初体验【转载】

    Windows Phone开发工具在MIX 2010上火热登场了.Windows Mobile开发者们压抑许久的热情终于爆发出来,对于Windows Phone的华丽转身,开发者们褒贬不一,有人对Si ...

  9. hdu 5442 Favorite Donut 后缀数组

    Favorite Donut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...

  10. JNI总结(一)

    JNI 是 Java平台中的一个强大特性.应用程序能够通过 JNI把 C/C++代码集成进 Java程序中.通过 JNI.开发人员在利用 Java平台强大功能的同一时候,又不必放弃对原有代码的投资.由 ...