首页 > 其他 > 详细

类属性赋值

时间:2014-10-22 19:52:30      阅读:227      评论:0      收藏:0      [点我收藏+]

public static T ConvertAndAssignment<T>(this object obj) where T : new()
{
T t = new T();
PropertyInfo[] propertys = obj.GetType().GetProperties();
foreach (PropertyInfo property in propertys)
{
var value = property.GetValue(obj, null);
var propertyNew = typeof(T).GetProperty(property.Name);
if (propertyNew != null)
{
propertyNew.SetValue(t, value, null);
}
}
return t;
}

 

public static T PopulateEntityFromCollection<T>(T entity, IDataReader collection) where T : new()
{
//初始化 如果为null
if (entity == null)
{
entity = new T();
}
//得到类型
Type type = typeof(T);
//取得属性集合
PropertyInfo[] pi = type.GetProperties();
foreach (PropertyInfo item in pi)
{
//给属性赋值
if (collection[item.Name] != null)
{
item.SetValue(entity, Convert.ChangeType(collection[item.Name], item.PropertyType), null);
}
}
return entity;
}


http://blog.csdn.net/xiaohan2826/article/details/8536074
http://www.cnblogs.com/XGLSummer/articles/3017868.html

类属性赋值

原文:http://www.cnblogs.com/zwei1121/p/4043842.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!