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
新聞熱點
疑難解答