| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides extension methods for working with objects |
| | | 7 | | /// </summary> |
| | | 8 | | public static class ObjectExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides object helpers for the given instance. |
| | | 12 | | /// </summary> |
| | | 13 | | extension(object @object) |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Creates a dictionary of the object's properties containing only those with non-null values. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <returns>A dictionary mapping property names to non-null values.</returns> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// Only public, readable instance properties are included. Static properties are not state of the |
| | | 21 | | /// object, and indexers and write-only properties have no single value to read; calling the |
| | | 22 | | /// reflection getter on either used to throw. |
| | | 23 | | /// </remarks> |
| | | 24 | | public Dictionary<string, object> NonNullPropertiesToDictionary() |
| | | 25 | | { |
| | 6 | 26 | | Dictionary<string, object> dictionary = new(); |
| | | 27 | | |
| | 40 | 28 | | foreach (var propertyInfo in ReadableInstanceProperties(@object.GetType())) |
| | | 29 | | { |
| | 14 | 30 | | var value = propertyInfo.GetValue(@object); |
| | | 31 | | |
| | 14 | 32 | | if (value is not null) |
| | | 33 | | { |
| | 12 | 34 | | dictionary[propertyInfo.Name] = value; |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | 6 | 38 | | return dictionary; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Creates a dictionary of the object's properties including those with null values. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <returns>A dictionary mapping property names to values, possibly null.</returns> |
| | | 45 | | /// <remarks> |
| | | 46 | | /// Only public, readable instance properties are included, on the same terms as |
| | | 47 | | /// <see cref="NonNullPropertiesToDictionary"/>. |
| | | 48 | | /// </remarks> |
| | | 49 | | public Dictionary<string, object?> PropertiesToDictionary() |
| | | 50 | | { |
| | 8 | 51 | | Dictionary<string, object?> dictionary = new(); |
| | | 52 | | |
| | 48 | 53 | | foreach (var propertyInfo in ReadableInstanceProperties(@object.GetType())) |
| | | 54 | | { |
| | 16 | 55 | | var value = propertyInfo.GetValue(@object); |
| | | 56 | | |
| | 16 | 57 | | dictionary[propertyInfo.Name] = value; |
| | | 58 | | } |
| | | 59 | | |
| | 8 | 60 | | return dictionary; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// The public instance properties of <paramref name="type"/> that have a getter and take no index. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// <see cref="Type.GetProperties()"/> also returns static properties, and indexers or write-only |
| | | 69 | | /// properties, which <see cref="PropertyInfo.GetValue(object)"/> cannot read without throwing. |
| | | 70 | | /// </remarks> |
| | | 71 | | internal static IEnumerable<PropertyInfo> ReadableInstanceProperties(Type type) => |
| | 26 | 72 | | type.GetProperties(BindingFlags.Public | BindingFlags.Instance) |
| | 90 | 73 | | .Where(property => property.CanRead && property.GetIndexParameters().Length == 0); |
| | | 74 | | } |