根据相同的字段创建或者修改一个Model
2016-01-28 14:36
323 查看
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;
}
}
{
/// <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;
}
}
相关文章推荐
- linux用户态与内核态
- DES加密例子
- shell截取字符串的方法
- 4.0十九章 检测----性能计数器
- 物联网
- 俄罗斯方块Tetris (javascript+HTML+CSS)
- POJ 2718 -- 穷竭搜索
- gridcontrol中LayoutView层叠图片效果
- SoftKeyboard按键View找焦点
- Centos6 安全防护设置指南
- MySQL死锁原因分析
- Struts2入门实例
- LAMP中如何设置open_basedir
- 简单的 数据加载显示“数据正在加载,请稍后”
- 版本控制系统SVN
- 10条命令,一分钟分析Linux性能问题
- POJ 1979 -- DFS
- android 更新下载apk
- 数据库备份,及清理备份计划
- bat批处理笔记——为指定类型文件添加前缀名