码迷,mamicode.com
首页 > Windows程序 > 详细

C# 反射

时间:2015-10-02 22:21:40      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
/// 根据源对象的属性填充目标对象的对应字段
/// </summary>
/// <param name="source">源对象</param>
/// <param name="target">目标对象</param>
public static void GetViewModelProperties(object source, object target)
{
if (source != null)
{
foreach (PropertyInfo item in target.GetType().GetProperties())
{

//获取对象中的实例>属性>某一个字段进行对比 item.name 获取字段
var attr = source.GetType().GetProperties().FirstOrDefault(p => p.Name == item.Name);
if (attr != null)
{

//SetValue 设置值  GetValue 获取值
item.SetValue(target, attr.GetValue(source, null), null);

}
}
}
}
}

C# 反射

标签:

原文地址:http://www.cnblogs.com/louby/p/4852743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!