-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (79 loc) · 2.19 KB
/
Copy pathProgram.cs
File metadata and controls
96 lines (79 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
public class Program
{
public static void Main()
{
var p1 = new Person(1, "Arthur", 24);
var p1Edit = new Person(1, null, 25);
var lista = p1.DetailedCompare(p1Edit);
Console.WriteLine("Changed fields count: " + lista.Count);
foreach (var a in lista)
{
Console.WriteLine("Prop: " + a.Property + " | OldVal: " + a.OldValue + " | NewVal: " + a.NewValue);
}
}
}
public class Person
{
public Person(int _id, string _name, int _age)
{
this.Id = _id;
this.Name = _name;
this.Age = _age;
}
public int Id { get; set; }
[IsDetailedComparedProp]
public string Name { get; set; }
[DisplayName("Changed Age Prop")]
[IsDetailedComparedProp]
public int Age { get; set; }
}
public class IsDetailedComparedPropAttribute : Attribute
{
}
public static class Extensions
{
public static List<PropChange> DetailedCompare<T>(this T val1, T val2)
{
var changes = new List<PropChange>();
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(pi => pi.GetCustomAttributes(typeof(IsDetailedComparedPropAttribute), true).Length > 0).ToArray();
foreach (var property in properties)
{
string propName = property.Name;
try
{
var attribute = property.GetCustomAttributes(typeof(DisplayNameAttribute), true).Cast<DisplayNameAttribute>().Single();
propName = attribute.DisplayName;
}
catch (Exception ex){ }
var propChange = new PropChange
{
Property = propName,
OldValue = property.GetValue(val1),
NewValue = property.GetValue(val2)
};
if (propChange.OldValue == null && propChange.NewValue == null)
continue;
if ((propChange.OldValue == null && propChange.NewValue != null) || (propChange.OldValue != null && propChange.NewValue == null)
)
{
changes.Add(propChange);
continue;
}
if (!Equals(propChange.OldValue, propChange.NewValue))
changes.Add(propChange);
}
return changes;
}
public class PropChange
{
public string Property { get; set; }
public object OldValue { get; set; }
public object NewValue { get; set; }
}
}