-
Notifications
You must be signed in to change notification settings - Fork 1
/
Comparer.cs
234 lines (212 loc) · 8.31 KB
/
Comparer.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
namespace DotStd
{
/// <summary>
/// Direction of sort. like System.Web.UI.WebControls.SortDirectionNullable
/// </summary>
public enum SortDirection
{
None = 0,
Ascending, // default.
Descending,
}
/// <summary>
/// How should a field be sorted ? Comparer.cs
/// This can be related to LambdaExpression? GetOrderByExp(string name)
/// </summary>
public class ComparerDef
{
public string PropName { get; set; } // Reflection property name of some object.
public SortDirection SortDir { get; set; }
public ComparerDef(string PropName, SortDirection SortDir = SortDirection.Ascending)
{
this.PropName = PropName;
this.SortDir = SortDir;
}
/// <summary>
/// Compare 2 simple typed objects.
/// Assume both objects are the same type! TypeCode
/// Allow nulls
/// String is compared case ignored.
/// </summary>
/// <param name="ox"></param>
/// <param name="oy">oy is not typically null ?</param>
/// <param name="typeCode"></param>
/// <returns>0=equal, >0=x is greater (1), <0=x is lesser. (-1)</returns>
public static int CompareType(object? ox, object? oy, TypeCode typeCode)
{
if (ox == null || oy == null)
{
if (ox == oy)
return 0;
if (ox == null)
return -1;
return 1;
}
switch (typeCode)
{
case TypeCode.Boolean:
case TypeCode.String:
return String.Compare(Convert.ToString(ox), Convert.ToString(oy), true);
case TypeCode.DateTime:
return DateTime.Compare(Convert.ToDateTime(ox), Convert.ToDateTime(oy));
case TypeCode.Decimal: // "System.Decimal": // Money
return Decimal.Compare(Convert.ToDecimal(ox), Convert.ToDecimal(oy));
case TypeCode.Int64:
{
Int64 ix = Convert.ToInt64(ox);
Int64 iy = Convert.ToInt64(oy);
if (ix > iy)
return 1;
else if (ix < iy)
return -1;
}
return 0;
case TypeCode.Double:
{
Double ix = Convert.ToDouble(ox);
Double iy = Convert.ToDouble(oy);
if (ix > iy)
return 1;
else if (ix < iy)
return -1;
}
return 0;
default:
// Numeric's are treated differently.
return ox.GetHashCode() - oy.GetHashCode();
}
}
public static int CompareType(object? ox, object? oy, TypeCode eTypeCodeX, TypeCode eTypeCodeY)
{
// we dont know if they are the same type.
if (eTypeCodeX == eTypeCodeY || ox == null || oy == null)
{
return CompareType(ox, oy, eTypeCodeX);
}
// diff types! compare as strings.
return CompareType(ox.ToString(), oy.ToString(), TypeCode.String);
}
}
public class ComparerSimple : ComparerDef, System.Collections.IComparer
{
// Compare based on a reflected property (string field name) of some object.
protected System.Reflection.PropertyInfo? _oProp; // cache this.
public ComparerSimple(string _PropName, SortDirection _SortDir = SortDirection.Ascending)
: base(_PropName, _SortDir)
{
}
public virtual int Compare(object? x, object? y)
{
// Compare properties of 2 objects.
// like System.Collections.Generic.Comparer<object>.Default.Compare(col1, col2);
// RETURN: 0=equal, >0=x is greater (1), <0=x is lesser. (-1)
// ASSUME objects are the same type.
if (this.SortDir == SortDirection.None)
{
return 0;
}
if (_oProp == null)
{
if (x != null)
{
_oProp = x.GetType().GetProperty(this.PropName);
}
else if (y != null)
{
_oProp = y.GetType().GetProperty(this.PropName);
}
else
{
return 0;
}
ValidState.ThrowIfNull(_oProp, nameof(_oProp));
}
int iRet = CompareType(_oProp.GetValue(x, null), _oProp.GetValue(y, null), Type.GetTypeCode(_oProp.PropertyType));
// Reverse?
if (this.SortDir == SortDirection.Descending)
iRet *= -1;
return iRet;
}
}
public class ComparerGeneric<ItemType> : ComparerSimple, System.Collections.Generic.IComparer<ItemType>
{
// compare using a generic <ItemType> .
// ItemType = a complex structure with a PropName value to be sorted.
public ComparerGeneric(string PropName, SortDirection SortDir = SortDirection.Ascending)
: base(PropName, SortDir)
{
}
public virtual int Compare(ItemType? x, ItemType? y)
{
// Compare properties of 2 objects.
if (this.SortDir == SortDirection.None)
{
return 0;
}
if (_oProp == null) // cache this.
{
_oProp = ValidState.GetNotNull(typeof(ItemType).GetProperty(this.PropName), nameof(_oProp));
}
int iRet = CompareType(_oProp.GetValue(x, null), _oProp.GetValue(y, null), Type.GetTypeCode(_oProp.PropertyType));
// Reverse?
if (this.SortDir == SortDirection.Descending)
iRet *= -1;
return iRet;
}
}
/// <summary>
/// General helpers for compare.
/// </summary>
public static class ComparerUtil
{
public static IEnumerable<T> GetSortedList<T>(IEnumerable<T> src, string sortColumn, SortDirection sortDir)
{
// Sort this list in memory.
PropertyInfo? prop = typeof(T).GetProperty(sortColumn);
if (prop == null)
return src;
int multiplier = sortDir == SortDirection.Descending ? -1 : 1;
var list = new List<T>();
list.AddRange(src);
list.Sort((t1, t2) =>
{
var col1 = prop.GetValue(t1);
var col2 = prop.GetValue(t2);
return multiplier * Comparer<object>.Default.Compare(col1, col2);
});
return list;
}
public static Dictionary<string, object?>? DiffProps<T>(T? objNew, T? objOld)
{
// What props changed (is New) in these objects?
// NOTE: Does not compare fields JUST props.
// null values are ok.
// return dictionary of changes. null = nothing changed.
Dictionary<string, object?>? changes = null;
Type typeComp = typeof(T);
Type ignoreAttrType = typeof(IgnoreDataMemberAttribute); // CHECK [IgnoreDataMember] or [Ignore()] ?
foreach (PropertyInfo prop in typeComp.GetProperties()) // enum props via reflection.
{
// Stuff to ignore?
object[] attrs2 = prop.GetCustomAttributes(ignoreAttrType, false); // This was probably not populated correctly so ignore it.
if (attrs2.Length > 0)
continue;
object? valNew = (objNew == null) ? null : prop.GetValue(objNew, null);
object? valOld = (objOld == null) ? null : prop.GetValue(objOld, null);
if (!object.Equals(valNew, valOld))
{
if (changes == null)
changes = new Dictionary<string, object?>();
changes.Add(prop.Name, valNew);
}
}
return changes;
}
}
}