zoukankan      html  css  js  c++  java
  • 根据相同的字段创建或者修改一个Model

    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;
    }
    }

    一个苦逼程序员
  • 相关阅读:
    Java后台校验手机号和邮箱
    centos7安装redis
    centos7安装mysql5.7
    VUE环境运行搭建
    Oracle11gR2安装
    VisualSvn-Server搭建
    SpringCloud-Hoxton.SR1-config整合
    项目简介
    linux常用命令
    JAVA NIO
  • 原文地址:https://www.cnblogs.com/root_u/p/5166170.html
Copyright © 2011-2022 走看看