-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathListExtensions.cs
More file actions
365 lines (295 loc) · 12.8 KB
/
ListExtensions.cs
File metadata and controls
365 lines (295 loc) · 12.8 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace RexStudios.CsharpExtensions
{
/// <summary>
///
/// </summary>
public static class ListExtensions
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="item"></param>
public static void AddToFront<T>(this List<T> list, T item)
{
if (ListExtensions.IsNullOrEmpty(list))
list.Insert(0, item);
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static bool IsNullOrEmpty<T>(this List<T> list)
{
return list.Count > 0 ? false : true;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list1"></param>
/// <param name="list2"></param>
/// <returns></returns>
public static bool CompareList<T>(this List<T> list1, List<T> list2)
{
//if any of the list is null, return false
if ((!list1.IsNullOrEmpty() && list2.IsNullOrEmpty()) || (!list2.IsNullOrEmpty() && list1.IsNullOrEmpty()))
return false;
//if both lists are null, return true, since its same
else if (list1 == null && list2 == null)
return true;
//if count don't match between 2 lists, then return false
if (list1.Count != list2.Count)
return false;
bool IsEqual = true;
foreach (T item in list1)
{
T Object1 = item;
T Object2 = list2.ElementAt(list1.IndexOf(item));
Type type = typeof(T);
//if any of the object inside list is null and other list has some value for the same object then return false
if ((Object1 == null && Object2 != null) || (Object2 == null && Object1 != null))
{
IsEqual = false;
break;
}
foreach (System.Reflection.PropertyInfo property in type.GetProperties())
{
if (property.Name != "ExtensionData")
{
string Object1Value = string.Empty;
string Object2Value = string.Empty;
if (type.GetProperty(property.Name).GetValue(Object1, null) != null)
Object1Value = type.GetProperty(property.Name).GetValue(Object1, null).ToString();
if (type.GetProperty(property.Name).GetValue(Object2, null) != null)
Object2Value = type.GetProperty(property.Name).GetValue(Object2, null).ToString();
//if any of the property value inside an object in the list didnt match, return false
if (Object1Value.Trim() != Object2Value.Trim())
{
IsEqual = false;
break;
}
}
}
}
//if all the properties are same then return true
return IsEqual;
}
/// <summary>
/// https://dzone.com/articles/generic-extension-method-to-map-objects-from-one-t
/// Generic Extension Method to Map Objects From One Type to Another
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TDestination"></typeparam>
/// <param name="source"></param>
/// <param name="destination"></param>
public static void MatchAndMap<TSource, TDestination>(this TSource source, TDestination destination)
where TSource : class, new()
where TDestination : class, new()
{
if (source != null && destination != null)
{
List<PropertyInfo> sourceProperties = source.GetType().GetProperties().ToList<PropertyInfo>();
List<PropertyInfo> destinationProperties = destination.GetType().GetProperties().ToList<PropertyInfo>();
foreach (PropertyInfo sourceProperty in sourceProperties)
{
PropertyInfo destinationProperty = destinationProperties.Find(item => item.Name == sourceProperty.Name);
if (destinationProperty != null)
{
try
{
destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
}
catch (Exception ex)
{
}
}
}
}
}
/// <summary>
/// https://dzone.com/articles/generic-extension-method-to-map-objects-from-one-t
/// Generic Extension Method to Map Objects From One Type to Another
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static TDestination MapProperties<TDestination>(this object source)
where TDestination : class, new()
{
var destination = Activator.CreateInstance<TDestination>();
MatchAndMap(source, destination);
return destination;
}
/// <summary>
/// https://www.extensionmethod.net/csharp/enum/generic-enum-to-list-t-converter
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<T> EnumToList<T>()
{
Type enumType = typeof(T);
// Can't use type constraints on value types, so have to do check like this
if (enumType.BaseType != typeof(Enum))
throw new ArgumentException("T must be of type System.Enum");
Array enumValArray = Enum.GetValues(enumType);
List<T> enumValList = new List<T>(enumValArray.Length);
foreach (int val in enumValArray)
{
enumValList.Add((T)Enum.Parse(enumType, val.ToString()));
}
return enumValList;
}
/// <summary>
/// Split list in to chunks of lists
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <param name="nSize"></param>
/// <returns></returns>
public static List<List<T>> SplitList<T>(List<T> items, int nSize)
{
List<List<T>> list = new List<List<T>>();
for (int i = 0; i < items.Count; i += nSize)
list.Add(items.GetRange(i, Math.Min(nSize, items.Count - i)));
return list;
}
/// <summary>
/// Extension method to write list data to excel.
/// </summary>
/// <typeparam name="T">Ganeric list</typeparam>
/// <param name="list"></param>
/// <param name="PathToSave">Path to save file.</param>
//public static void ToExcel<T>(this List<T> list, string PathToSave)
//{
// Excel Excel = new Workbook()
// #region Declarations
// if (string.IsNullOrEmpty(PathToSave))
// {
// throw new Exception("Invalid file path.");
// }
// else if (PathToSave.ToLower().Contains("") == false)
// {
// throw new Exception("Invalid file path.");
// }
// if (list == null)
// {
// throw new Exception("No data to export.");
// }
// Excel.Application excelApp = null;
// Excel.Workbooks books = null;
// Excel._Workbook book = null;
// Excel.Sheets sheets = null;
// Excel._Worksheet sheet = null;
// Excel.Range range = null;
// Excel.Font font = null;
// // Optional argument variable
// object optionalValue = Missing.Value;
// string strHeaderStart = "A2";
// string strDataStart = "A3";
// #endregion
// #region Processing
// try
// {
// #region Init Excel app.
// excelApp = new Excel.Application();
// books = (Excel.Workbooks)excelApp.Workbooks;
// book = (Excel._Workbook)(books.Add(optionalValue));
// sheets = (Excel.Sheets)book.Worksheets;
// sheet = (Excel._Worksheet)(sheets.get_Item(1));
// #endregion
// #region Creating Header
// Dictionary<string, string> objHeaders = new Dictionary<string, string>();
// PropertyInfo[] headerInfo = typeof(T).GetProperties();
// foreach (var property in headerInfo)
// {
// var attribute = property.GetCustomAttributes(typeof(DisplayNameAttribute), false)
// .Cast<DisplayNameAttribute>().FirstOrDefault();
// objHeaders.Add(property.Name, attribute == null ?
// property.Name : attribute.DisplayName);
// }
// range = sheet.get_Range(strHeaderStart, optionalValue);
// range = range.get_Resize(1, objHeaders.Count);
// range.set_Value(optionalValue, objHeaders.Values.ToArray());
// range.BorderAround(Type.Missing, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Type.Missing);
// font = range.Font;
// font.Bold = true;
// range.Interior.Color = Color.LightGray.ToArgb();
// #endregion
// #region Writing data to cell
// int count = list.Count;
// object[,] objData = new object[count, objHeaders.Count];
// for (int j = 0; j < count; j++)
// {
// var item = list[j];
// int i = 0;
// foreach (KeyValuePair<string, string> entry in objHeaders)
// {
// var y = typeof(T).InvokeMember(entry.Key.ToString(), BindingFlags.GetProperty, null, item, null);
// objData[j, i++] = (y == null) ? "" : y.ToString();
// }
// }
// range = sheet.get_Range(strDataStart, optionalValue);
// range = range.get_Resize(count, objHeaders.Count);
// range.set_Value(optionalValue, objData);
// range.BorderAround(Type.Missing, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Type.Missing);
// range = sheet.get_Range(strHeaderStart, optionalValue);
// range = range.get_Resize(count + 1, objHeaders.Count);
// range.Columns.AutoFit();
// #endregion
// #region Saving data and Opening Excel file.
// if (string.IsNullOrEmpty(PathToSave) == false)
// book.SaveAs(PathToSave);
// excelApp.Visible = true;
// #endregion
// #region Release objects
// try
// {
// if (sheet != null)
// System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
// sheet = null;
// if (sheets != null)
// System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);
// sheets = null;
// if (book != null)
// System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
// book = null;
// if (books != null)
// System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
// books = null;
// if (excelApp != null)
// System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
// excelApp = null;
// }
// catch (Exception ex)
// {
// sheet = null;
// sheets = null;
// book = null;
// books = null;
// excelApp = null;
// }
// finally
// {
// GC.Collect();
// }
// #endregion
// }
// catch (Exception ex)
// {
// throw ex;
// }
// #endregion
//}
//
}
}