码迷,mamicode.com
首页 > 其他好文 > 详细

根据相同的字段创建或者修改一个Model

时间:2016-01-28 15:20:31      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

public static class ObjectUtils
{
/// <summary>
/// 根据source创建一个强类型的Object,并根据相同属性名进行赋值.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static T CreateObject<T>(object source) where T : class, new()
{
var obj = new T();
var propertiesFromSource = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

foreach (var property in properties)
{
var sourceProperty = propertiesFromSource.FirstOrDefault(x => x.Name == property.Name);
if (sourceProperty != null)
{
property.SetValue(obj, sourceProperty.GetValue(source, null), null);
}
}

return obj;
}
/// <summary>
/// 根据source修改一个强类型的Target Object,并根据相同属性名进行赋值.
/// </summary>
/// <typeparam name="TTarget"></typeparam>
/// <typeparam name="TSource"></typeparam>
/// <param name="target"></param>
/// <param name="source"></param>
/// <param name="propertyExpressionsFromSource"></param>
public static void UpdateObject<TTarget, TSource>(TTarget target, TSource source, params Expression<Func<TSource, object>>[] propertyExpressionsFromSource)
where TTarget : class
where TSource : class
{
if (target == null)
{
throw new ArgumentNullException("target");
}
if (source == null)
{
throw new ArgumentNullException("source");
}
if (propertyExpressionsFromSource == null)
{
throw new ArgumentNullException("propertyExpressionsFromSource");
}

var properties = target.GetType().GetProperties();

foreach (var propertyExpression in propertyExpressionsFromSource)
{
var propertyFromSource = GetProperty<TSource, object>(propertyExpression);
var propertyFromTarget = properties.SingleOrDefault(x => x.Name == propertyFromSource.Name);
if (propertyFromTarget != null)
{
propertyFromTarget.SetValue(target, propertyFromSource.GetValue(source, null), null);
}
}
}

private static PropertyInfo GetProperty<TSource, TProperty>(Expression<Func<TSource, TProperty>> lambda)
{
var type = typeof(TSource);
MemberExpression memberExpression = null;

switch (lambda.Body.NodeType)
{
case ExpressionType.Convert:
memberExpression = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
break;
case ExpressionType.MemberAccess:
memberExpression = lambda.Body as MemberExpression;
break;
}

if (memberExpression == null)
{
throw new ArgumentException(string.Format("Invalid Lambda Expression ‘{0}‘.", lambda.ToString()));
}

var propInfo = memberExpression.Member as PropertyInfo;
if (propInfo == null)
{
throw new ArgumentException(string.Format("Expression ‘{0}‘ refers to a field, not a property.", lambda.ToString()));
}

if (type != propInfo.ReflectedType && !type.IsSubclassOf(propInfo.ReflectedType))
{
throw new ArgumentException(string.Format("Expresion ‘{0}‘ refers to a property that is not from type {1}.", lambda.ToString(), type));
}

return propInfo;
}
}

根据相同的字段创建或者修改一个Model

标签:

原文地址:http://www.cnblogs.com/root_u/p/5166170.html

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