.Net 两个对像之间的映射 (一 )
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 两个对像之间的映射 (一 )的更多相关文章
- .Net 两个对像之间的映射 ( 二 )
一.使用 class Program { static void Main(string[] args) { User u1 = new User(); u1.UserName = "aaa ...
- .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 ...
随机推荐
- August 27th 2017 Week 35th Sunday
You can't be brave if you've only had wonderful things happen to you. 人生若只是有美好的境遇,那你也没办法学会勇敢. Wherea ...
- MVC 入门-MvcMovie
入门教程 ASP.NET MVC 5 入门 https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/introduct ...
- IDEA中配置Scala开发编译环境
安装JDK 因为Scala是运行在JVM平台上的,所以安装Scala之前要安装JDK. 安装Scala Windows安装Scala编译器 访问Scala官网http://www.scala-lang ...
- [TOP10]最受欢迎的10个Metasploit模块和插件
很多人都想知道最受欢迎的10个Metasploit模块和插件是什么(TOP10),事实上这是一个很难回答的问题,因为什么才叫"Top"?我想每个人都有每个人的看法.于是我们通过调查 ...
- Safari自动代理
1. 准备一个代理服务器,我使用的是GoAgent. 2. 准备一个PAC文件,我是从chrome导出的. 3. 准备一个本地文件服务器或web服务器,我是因为手头有一个使用NodeJS的小项目,所以 ...
- Mac Sublime Text 3
安装Package Control安装过程: 使用快捷键 control + ` 或者菜单栏选择View > Show Console安装Package Control参考官方页面.Sublim ...
- [XML123] FpML
Wiki Fpml FpML (Financial products Markup Language) is a business information exchange standard base ...
- mysql5.6编译安装
1.安装编译源码所需的工具和库 yum install gcc gcc-c++ ncurses-devel perl -y yum -y install wget gcc-c++ ncurses nc ...
- CocoaPods安装指定版本
Cocoapods目前最新的正式版本是0.35.0,如果升级到这个版本,并且在project中使用XMPPFramework,在pod install之后会出现如下循环依赖的问题 There is a ...
- 在 S5PV210 的 开发板上 点亮 一个 LED 灯
参考学习教程:周立功嵌入式Linux开发教程-(上册) 材料:首先 准备一个 安装好 Linux 的 开发板 使用 xshell 工具 连接 开发板 ,winscp 工具 连接 开发板 , 准 ...