• .net两个对象比较,抛出不一样字段的结果


    现在应该经常用到记录操作日志,修改和新增必定涉及到两个实体的属性值的变动。

    利用反射,将变动记录下来。

    切记,类中的属性字段上面需要打上Description标签:

    例如:

            /// <summary>
            /// 最后修改时间
            /// </summary>
            [Description("最后修改时间")]
            public DateTime PIUpdateTime { get; set; }

    相关代码直接附上:

     public class OprateLogHelper
        {
            /// <summary>
            /// 获取两个对象间的值发生变化的描述
            /// </summary>
            /// <typeparam name="T">T</typeparam>
            /// <param name="obj1">变化前的对象</param>
            /// <param name="obj2">变化后的对象</param>
            /// <param name="isDes">是否过滤掉没有[Description]标记的</param>
            /// <returns>字符串</returns>
            public static string GetObjCompareString<T>(T obj1, T obj2, bool isDes) where T : new()
            {
                string res = string.Empty;
                if (obj1 == null || obj2 == null)
                {
                    return res;
                }
                var properties =
                    from property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                    select property;
    
                string objVal1 = string.Empty;
                string objVal2 = string.Empty;
    
                foreach (var property in properties)
                {
                    var ingoreCompare = Attribute.GetCustomAttribute(property, typeof(IngoreCompareAttribute));
                    if (ingoreCompare != null)
                    {
                        continue;
                    }
    
                    objVal1 = property.GetValue(obj1, null) == null ? string.Empty : property.GetValue(obj1, null).ToString();
                    objVal2 = property.GetValue(obj2, null) == null ? string.Empty : property.GetValue(obj2, null).ToString();
    
                    string des = string.Empty;
                    DescriptionAttribute descriptionAttribute = ((DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute)));
                    if (descriptionAttribute != null)
                    {
                        des = ((DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute))).Description;// 属性值
                    }
                    if (isDes && descriptionAttribute == null)
                    {
                        continue;
                    }
                    if (!objVal1.Equals(objVal2))
                    {
                        res += (string.IsNullOrEmpty(des) ? property.Name : des) + ":" + objVal1 + "->" + objVal2 + "";
                    }
    
                }
                return res;
            }
        }
        /// <summary>
        /// 加入些特性后,在实体差异比较中会忽略该属性
        /// </summary>
        [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
        public class IngoreCompareAttribute : Attribute
        {
            public IngoreCompareAttribute()
            {
                Flag = true;
            }
    
            public bool Flag { get; set; }
        }

    用到时,直接调用GetObjCompareString方法即可。

  • 相关阅读:
    模块化编程
    flex 弹性布局
    作用域与作用域链
    深入解读JavaScript面向对象编程实践
    javascript Null、Undefined 、NaN的联系与区别
    跨域常见解决方案
    Reverse Pairs
    315. Count of Smaller Numbers After Self
    2. Add Two Numbers
    657. Judge Route Circle
  • 原文地址:https://www.cnblogs.com/ericli-ericli/p/6250122.html
Copyright © 2020-2023  润新知