fork(7) download
  1. using System;
  2. using System.Linq.Expressions;
  3. using System.Reflection;
  4.  
  5. public class Address
  6. {
  7. public string AddressID { get; set; }
  8. public int AddressStagingID { get; set; }
  9. public string Address1 { get; set; }
  10. public string Address2 { get; set; }
  11. public string City { get; set; }
  12. public string County { get; set; }
  13. public string Postcode { get; set; }
  14. public string Country { get; set; }
  15. public bool PreferredAddress { get; set; }
  16. public int? DBID { get; set; }
  17. }
  18.  
  19. public class Test
  20. {
  21. public static void Main()
  22. {
  23. Address a1 = new Address();
  24. a1.AddressID = "100";
  25.  
  26. Address a2 = new Address();
  27. a2.AddressID = "200";
  28. Console.WriteLine(IsAddressModified(a1,a2,a=>a.AddressID));
  29. }
  30.  
  31. public static bool IsAddressModified(Address a1,Address a2,params Expression<Func<Address,Object>>[] props)
  32. {
  33. if(props == null)
  34. return a1.Equals(a2);
  35.  
  36. foreach(Expression<Func<Address,object>> memberExpression in props)
  37. {
  38. MemberExpression property = memberExpression.Body as MemberExpression;
  39. if(property != null)
  40. {
  41. foreach(PropertyInfo pi in typeof(Address).GetProperties())
  42. {
  43. // exclude all properties we passed in
  44. if(!pi.Name.Equals(property.Member.Name))
  45. {
  46.  
  47. var valueA1 = pi.GetValue(a1);
  48. var valueA2 = pi.GetValue(a2);
  49. if(valueA1 != null && valueA2 != null)
  50. if(!valueA1.Equals(valueA2))
  51. return true;
  52. }
  53. }
  54. }
  55. }
  56.  
  57. return false;
  58. }
  59. }
Success #stdin #stdout 0.08s 24312KB
stdin
Standard input is empty
stdout
False