.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 ...
随机推荐
- 乘风破浪:LeetCode真题_012_Integer to Roman
乘风破浪:LeetCode真题_012_Integer to Roman 一.前言 经过了前面的思维训练,我们在某些方面有了一定的提高,但是对于实际的问题,要在短时间内得到答案,还是需要我们多多的练习 ...
- get传参乱码问题
js get请求代码 function toBranchManagerList(branch_name) { window.location=path+"/branch/toBranchMa ...
- python strip()
函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstrip(rm) 删除s字符串中开头 ...
- Spring4 SpringMVC Hibernate4 Freemaker 整合样例
更正改动(2014-05-30 13:47:22):有的IDE中web.xml会报这个错: cvc-complex-type.2.4.a: Invalid content was found star ...
- SoapUI这么好,舍得不用吗?
之前尝试去学习哈SoapUI, 安装都报错,直接拖黑不用,对java开发的产品本身不感冒 后来工作上,和老外沟通,发现他们不会用xmlspy,只会SoapUI,心里都想,不学习看来不方便,然后都安装了 ...
- 30、springboot与检索(2)
项目中进行整合: 1.整合 新建项目加入依赖(NoSql) springboot默认使用SpringDate ElasticSearch模块进行操作 查看自动配置类: SpringBoot默认支持 ...
- canvas二三事之签名板与视频绘制
今天,不知道怎么的就点开了语雀,然后就看到了<HTML5 Canvas 教程>,开始了canvas的研究(学习)之旅. 首先,想到的第一个东西就是签名板,上代码: <canvas i ...
- 安装IIS步骤图解
这几日好些网友来找iis安装包,但是因为新浪爱问的共享资料已关闭导致下载链接不可用,笔者在新浪微盘的备份资料只有5.1版,现共享链接如下: IIS5.1 for windows xp下载链接http: ...
- 软工之词频统计器及基于sketch在大数据下的词频统计设计
目录 摘要 算法关键 红黑树 稳定排序 代码框架 .h文件: .cpp文件 频率统计器的实现 接口设计与实现 接口设计 核心功能词频统计器流程 效果 单元测试 性能分析 性能分析图 问题发现 解决方案 ...
- Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:5
jsp页面出现如下异常: Stacktrace: at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServle ...