C# 对象复制
/// <summary>
/// 把DataTable对象转成List<T>对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <returns></returns>
public static List<T> ConvertToModel<T>(this DataTable table) where T : new()
{
var list = new List<T>();
PropertyInfo[] propertys = typeof(T).GetProperties(); foreach (DataRow row in table.Rows)
{
T model = Activator.CreateInstance<T>();
foreach (PropertyInfo pi in propertys)
{
var tempName = pi.Name;
if (!table.Columns.Contains(tempName)) continue;
if (!pi.CanWrite) continue;
object value = Convert.ToString(row[tempName]);
if (value != DBNull.Value)
{
pi.SetValue(model, value, null);
}
} list.Add(model);
}
return list;
} /// <summary>
/// 把具有同结构的实体赋值到另外一个实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="from"></param>
/// <returns></returns>
public static T ConvertTo<T>(this object from) where T : new()
{
if (from == null)
{
return default(T);
}
var to = Activator.CreateInstance<T>();
return from.CopyTo<T>(to);
} /// <summary>
/// 通过比较属性名称来给目标实体赋值
/// </summary>
/// <typeparam name="T">泛型实体</typeparam>
/// <param name="from">原实体</param>
/// <param name="to">目标实体</param>
/// <returns></returns>
public static T CopyTo<T>(this object from, T to) where T : new()
{
if (from == null)
{
return to;
}
if (to == null)
{
to = Activator.CreateInstance<T>();
}
PropertyDescriptorCollection fromProperties = TypeDescriptor.GetProperties(from);
PropertyDescriptorCollection toProperties = TypeDescriptor.GetProperties(to);
foreach (PropertyDescriptor fromProperty in fromProperties)
{
PropertyDescriptor toProperty = toProperties.Find(fromProperty.Name, true);
if (toProperty != null && !toProperty.IsReadOnly)
{
bool isDirectlyAssignable = toProperty.PropertyType.IsAssignableFrom(fromProperty.PropertyType);
bool liftedValueType = !isDirectlyAssignable && Nullable.GetUnderlyingType(fromProperty.PropertyType) == toProperty.PropertyType;
if (isDirectlyAssignable || liftedValueType)
{
object fromValue = fromProperty.GetValue(from);
if (isDirectlyAssignable || (fromValue != null && liftedValueType))
{
toProperty.SetValue(to, fromValue);
}
}
}
}
return to;
} /// <summary>
/// 把实体转换成一个dynamic对象
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static dynamic ToDynamic(this object value)
{
IDictionary<string, object> expando = new ExpandoObject(); foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
{
expando.Add(property.Name, property.GetValue(value));
} return expando as ExpandoObject;
}
C# 对象复制的更多相关文章
- PHP基础知识之对象复制
对象的复制默认为浅复制 进行深复制的方法为:在类中定义魔法方法__clone(),类的对象复制时,会自动调用 __clone方法,在 __clone方法中可以进行各种复制对象的个性化 class My ...
- JS对象复制
在JavaScript很多人复制一个对象的时候都是直接用"=",因为大家都觉得脚本语言是没有指针.引用.地址之类的,所以直接用"="就可以把一个对象复制给另外一 ...
- PHP写时复制, 变量复制和对象复制不同!!!
2016年3月18日 15:09:28 星期五 一直以为PHP对象也是写时复制....... 其实: PHP的变量是写时复制, 对象是引用的 写时复制: $a = $b; 如果$b的内容不改变, $a ...
- 【转】JavaScript中的对象复制(Object Clone)
JavaScript中并没有直接提供对象复制(Object Clone)的方法.因此下面的代码中改变对象b的时候,也就改变了对象a. a = {k1:1, k2:2, k3:3}; b = a; b. ...
- 对象复制、克隆、深度clone
-------------------------------------------------------------------------------- ------------------- ...
- PHP5的对象复制
今天用yii开发程序,一个bug改了一晚上,最后发现问题出在了对象复制机制上,PHP5之前的对象复制只需要$object_a = $object_b即可,但PHP5这样得到的是浅复制,及指针指向,并不 ...
- Java反射 - 2(对象复制,父类域,内省)
为什么要复制对象?假设有个类Car,包含name,color2个属性,那么将car1对象复制给car2对象,只需要car2.setName(car1.getName)与car2.setColor(ca ...
- js原生设计模式——7原型模式之真正的原型模式——对象复制封装
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- JS对象复制(深拷贝、浅拷贝)
如何在 JS 中复制对象 在本文中,我们将从浅拷贝(shallow copy)和深拷贝(deep copy)两个方面,介绍多种 JS 中复制对象的方法. 在开始之前,有一些基础知识值得一提:Javas ...
- JavaScript对象复制(一)(转载)
在JavaScript很多人复制一个对象的时候都是直接用"=",因为大家都觉得脚本语言是没有指针.引用.地址之类的,所以直接用"="就可以把一个对象复制给另外一 ...
随机推荐
- linux学习:进程间通信—管道
1.进程间通信当中一种比較简单的方法是管道操作 /* ========================================================================= ...
- hihocode #1388 : Periodic Signal NTT
#1388 : Periodic Signal 描述 Profess X is an expert in signal processing. He has a device which can ...
- windows下安装RubbitMq
1.下载 下载 rabbitMQ :http://www.rabbitmq.com/download.html,安装rabbitmq需要erlang,下载erlang:http://www.erlan ...
- Scrapy 'module' object has no attribute 'Spider'错误
在“Scrapy入门教程”中,在创建的“dmoz_spider.py”文件中是通过 import scrapy class DmozSpider(scrapy.Spider): 的方式导入.但是用这种 ...
- spring cloud 服务消费
Ribbon Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用. 当Ribbon与Eureka联合使用时,ribbonServerList会 ...
- hash与map的区别联系应用(转)
一,hashtable原理: 哈希表又名散列表,其主要目的是用于解决数据的快速定位问题.考虑如下一个场景. 一列键值对数据,存储在一个table中,如何通过数据的关键字快速查找相应值呢?不要告诉我一个 ...
- hdu-5719 Arrange(组合数学)
题目链接: Arrange Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- [Codeforces 914D] Bash and a Tough Math Puzzle
[题目链接] https://codeforces.com/contest/914/problem/D [算法] 显然 , 当一个区间[l , r]中为d倍数的数的个数 <= 1 , 答案为Ye ...
- .NETFramework:HttpContext
ylbtech-.NETFramework:HttpContext 1.返回顶部 1. #region 程序集 System.Web, Version=4.0.0.0, Culture=neutral ...
- Collection View Programming Guide for iOS---(三)---Designing Your Data Source and Delegate
Designing Your Data Source and Delegate 设计你的数据源和委托 Every collection view must have a data source o ...