Ejemplo n.º 1
0
        public virtual void UpdateTo <TEntity>(TEntity entity)
            where TEntity : class
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            var type = entity.GetType();

            foreach (var item in Values)
            {
                if (SourceExcludes.Any(r => r.EqualsIgnoreCase(item.Key)))
                {
                    continue;
                }

                var targetPro   = item.Key;
                var targetValue = item.Value;
                if (Converters.TryGetValue(item.Key, out var converter))
                {
                    targetPro   = converter.TargetPro;
                    targetValue = converter.ConvertFunction(item.Value);
                }

                var pro = type.GetProperty(targetPro,
                                           BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.SetProperty);
                if (pro == null || !pro.GetIndexParameters().IsNullOrEmpty())
                {
                    continue;
                }

                pro.SetValue(entity, targetValue);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 排除源字段,不允许更新,一般用于不同的权限用户更新数据用
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="expression"></param>
 public void Exclude <T>(params Expression <Func <T, object> >[] expression)
 {
     expression.ForEachItem(r => SourceExcludes.Add(r.GetPropertyName()));
 }