C#两个Object进行比较,Object里只是简单属性,不存在层级关系还比较好处理,如果遇到多层级的就有点麻烦。
/// <summary> /// 比较字段 /// </summary> /// <param name="beforeUpdateData">原来的值</param> /// <param name="afterUpdateData">页面提交的新值</param> /// <param name="specialFields">特殊处理字段</param> /// <param name="ignoreFields">忽略处理字段</param> /// <returns></returns> public static List<FieldItem> ComparisonField(Object beforeUpdateData,Object afterUpdateData,List<string> specialFields,List<string> ignoreFields) { var fieldItems = new List<FieldItem>(); if (null == afterUpdateData || null == beforeUpdateData) return fieldItems; Type type = afterUpdateData.GetType(); var fields = type.GetPRoperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (var field in fields) {// 取得字段的值 object afterUpdateItem = field.GetValue(afterUpdateData); var beforeUpdateItem = beforeUpdateData.GetType().GetProperty(field.Name).GetValue(beforeUpdateData, null); if (field.PropertyType.IsValueType || field.PropertyType.Name.StartsWith("String")) { // String类型处理 // 直接比较是否相等,若不相等直接记录 if (afterUpdateItem.ToString() != beforeUpdateItem.ToString()) { fieldItems.Add(new FieldItem { ChangeField = field.Name, BeforeChangeContent = beforeUpdateItem.ToString(), AfterChangeContent = afterUpdateItem.ToString(), Description = "修改" }); } } } return fieldItems; }
对比product 与 newProduct 两个Object,以下是修改项:
class Program { static void Main(string[] args) { var earphoneID = Guid.NewGuid(); var rabbit = new ProductSubitem() { ID = Guid.NewGuid(), Name = "米兔", Detail = new Detail() { ID = Guid.NewGuid(), Introduce = "小米米兔", Price = 10 } }; var subitem1 = new ProductSubitem() { ID = earphoneID, Name = "耳机", Detail = new Detail() { ID = Guid.NewGuid(), Introduce = "小米耳机", Price = 79 } }; var subitem11 = new ProductSubitem() { ID = earphoneID, Name = "耳机", Detail = new Detail() { ID = Guid.NewGuid(), Introduce = "小米耳机", Price = 89 } }; var subitem2 = new ProductSubitem() { ID = Guid.NewGuid(), Name = "手机贴膜", Detail = new Detail() { ID = Guid.NewGuid(), Introduce = "小米手机贴膜", Price = 5 } }; // 构建产品原始数据 var beijing = new Province { ID = Guid.NewGuid(), Name = "北京" }; var mifan1 = new User() { ID = Guid.NewGuid(), UserName = "mifan1", RealName = "北京米粉" }; var attachmentFiles = new List<AttachmentFile>() { new AttachmentFile(){ID = Guid.NewGuid(),Name = "说明书",SavePath = @"/xiaomi/document/note.doc",Size = 20} }; var id = Guid.NewGuid(); var detailID = Guid.NewGuid(); var product = new Product() { ID = id, Name = "小米Note", Detail = new Detail() { ID = detailID, Introduce = "小米新款手机", Price = 2299 }, Province = beijing, CreateUser = mifan1, CreateTime = DateTime.Now, ProductSubitems = new List<ProductSubitem>() { subitem1, subitem2 }, AttachmentFiles = attachmentFiles }; var newProduct = new Product() { ID = id, Name = "小米Note2", // 1.修改了Name:小米Note -> 小米Note2 Detail = new Detail() { ID = detailID, Introduce = "小米新款手机", Price = 3399 }, // 2.修改了Detail中的Price:2299 -> 3399 Province = beijing, CreateUser = mifan1, CreateTime = DateTime.Now, ProductSubitems = new List<ProductSubitem>() { rabbit, subitem11, subitem2 }, // 3.修改耳机价格:79 -> 89 | 4.产品子项新增了米兔。 AttachmentFiles = attachmentFiles }; var comparisonFieldList = Common.ComparisonField(product, newProduct, SpecialFields, IgnoreFields); foreach (var fieldItem in comparisonFieldList) { Console.WriteLine(string.Format("变更字段:{0}, 修改前内容:{1}, 修改后内容:{2}, 描述:{3}", fieldItem.ChangeField, fieldItem.BeforeChangeContent, fieldItem.AfterChangeContent,fieldItem.Description)); } Console.ReadLine(); } /// <summary> /// 特殊字段,只做ID比较 /// </summary> public static readonly List<string> SpecialFields = new List<string>() { "Province","AttachmentFiles" }; /// <summary> /// 忽略字段 /// </summary> public static readonly List<string> IgnoreFields = new List<string>() { "ID", "CreateTime", "CreateUser" };
1 /// <summary> 2 /// 产品 3 /// </summary> 4 public class Product 5 { 6 public Guid ID { get; set; } 7 8 /// <summary> 9 /// 名称 10 /// </summary> 11 public string Name { get; set; } 12 13 /// <summary> 14 /// 详情 15 /// </summary> 16 public Detail Detail { get; set; } 17 18 /// <summary> 19 /// 产地 20 /// </summary> 21 public Province Province { get; set; } 22 23 /// <summary> 24 /// 创建用户 25 /// </summary> 26 public User CreateUser { get; set; } 27 28 public DateTime CreateTime { get; set; } 29 30 /// <summary> 31 /// 产品子项 32 /// </summary> 33 public List<ProductSubitem> ProductSubitems { get; set; } 34 35 /// <summary> 36 /// 相关文件 37 /// </summary> 38 public List<AttachmentFile> AttachmentFiles { get; set; } 39 } 40 41 /// <summary> 42 /// 用户 43 /// </summary> 44 public class User 45 { 46 public Guid ID { get; set; } 47 48 public string UserName { get; set; } 49 50 public string RealName { get; set; } 51 } 52 53 /// <summary> 54 /// 省份 55 /// </summary> 56 public class Province 57 { 58 public Guid ID { get; set; } 59 60 public string Name { get; set; } 61 } 62 63 /// <summary> 64 /// 介绍 65 /// </summary> 66 public class Detail 67 { 68 public Guid ID { get; set; } 69 70 /// <summary> 71 /// 介绍 72 /// </su
新闻热点
疑难解答