csharp/8720826/emud/src/Emprise.Infra/Extensions/PropertyExtension.cs

PropertyExtension.cs
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Emprise.Infra.Extensions
{
    public static clast PropertyExtension
    {
        /// 
        /// 比较实体差异
        /// 
        /// 
        /// 
        /// 修改前
        /// 修改后
        /// 
        public static string ComparisonTo(this T1 source, T2 current)
        {
            string diff = "";
            try
            {
                Type t1 = source.GetType();
                Type t2 = current.GetType();
                PropertyInfo[] property2 = t2.GetProperties();
                //排除主键和基础字段
                List exclude = new List() { "Id" };
                foreach (PropertyInfo p in property2)
                {
                    string name = p.Name;
                    if (exclude.Contains(name)) { continue; }
                    var value1 = t1.GetProperty(name)?.GetValue(source, null)?.ToString();
                    var value2 = p.GetValue(current, null)?.ToString();
                    if (value1 != value2)
                    {
                        diff += $"[{name}]:'{value1}'=>'{value2}';\r\n";
                    }
                }
            }
            catch(Exception)
            {
              
            }
            return diff;
        }
    }
}