| | 1 | | using System.Globalization; |
| | 2 | | using System.Reflection; |
| | 3 | |
|
| | 4 | | namespace MRA.Infrastructure.Excel.Attributes; |
| | 5 | |
|
| | 6 | | public class ExcelColumnInfo |
| | 7 | | { |
| 0 | 8 | | public PropertyInfo Property { get; set; } |
| 0 | 9 | | public ExcelColumnAttribute Attribute { get; set; } |
| | 10 | |
|
| | 11 | | public override string ToString() |
| | 12 | | { |
| 0 | 13 | | return $"{Attribute.Name} => {Property.PropertyType}"; |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public bool SameValues<T>(T object1, T object2) |
| | 17 | | { |
| 0 | 18 | | var v1 = GetValue(object1); |
| 0 | 19 | | var v2 = GetValue(object2); |
| | 20 | |
|
| 0 | 21 | | if(v1 is null && v2 is null) |
| | 22 | | { |
| 0 | 23 | | return true; |
| | 24 | | } |
| 0 | 25 | | else if (v1 is null || v2 is null) |
| | 26 | | { |
| 0 | 27 | | return false; |
| | 28 | | } |
| 0 | 29 | | else if (v1 is List<string> list1 && v2 is List<string> list2) |
| | 30 | | { |
| 0 | 31 | | if (list1.Count != list2.Count) return false; |
| | 32 | |
|
| 0 | 33 | | for (int i = 0; i < list1.Count; i++) |
| | 34 | | { |
| 0 | 35 | | if (!list1[i].Equals(list2[i])) return false; |
| | 36 | | } |
| | 37 | |
|
| 0 | 38 | | return true; |
| | 39 | | } |
| | 40 | | else |
| | 41 | | { |
| 0 | 42 | | return v1.Equals(v2); |
| | 43 | | } |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public object? GetValue<T>(T? instance) |
| | 47 | | { |
| 0 | 48 | | if (instance is null || Property == null) return null; |
| | 49 | |
|
| 0 | 50 | | return Property.GetValue(instance); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public string GetValueToPrint<T>(T instance) |
| | 54 | | { |
| 0 | 55 | | var value = GetValue(instance); |
| | 56 | |
|
| 0 | 57 | | if(value is string stringValue) |
| | 58 | | { |
| 0 | 59 | | return stringValue; |
| | 60 | | } |
| 0 | 61 | | else if (value is DateTime valueDate) |
| | 62 | | { |
| 0 | 63 | | return GetStringFromDate(valueDate); |
| | 64 | | } |
| 0 | 65 | | else if (value is bool boolValue) |
| | 66 | | { |
| 0 | 67 | | return boolValue ? "True" : "False"; |
| | 68 | | } |
| 0 | 69 | | else if(value is List<string> listString) |
| | 70 | | { |
| 0 | 71 | | return String.Join("\n", listString); |
| | 72 | | } |
| | 73 | |
|
| 0 | 74 | | return value?.ToString() ?? "null"; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public static string GetStringFromDate(DateTime date) |
| | 78 | | { |
| 0 | 79 | | var cultureInfo = CultureInfo.GetCultureInfo("es-ES"); |
| 0 | 80 | | return date.ToString("yyyy/MM/dd", cultureInfo); |
| | 81 | | } |
| | 82 | | } |