< Summary

Information
Class: ArturRios.Extensions.ObjectExtensions
Assembly: ArturRios.Extensions
File(s): /home/ci/agent/workspace/dotnet-extensions-docs_main/app/src/ObjectExtensions.cs
Line coverage
100%
Covered lines: 13
Uncovered lines: 0
Coverable lines: 13
Total lines: 74
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage
100%
Covered methods: 3
Fully covered methods: 3
Total methods: 3
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
NonNullPropertiesToDictionary(...)100%44100%
PropertiesToDictionary(...)100%22100%
ReadableInstanceProperties(...)100%22100%

File(s)

/home/ci/agent/workspace/dotnet-extensions-docs_main/app/src/ObjectExtensions.cs

#LineLine coverage
 1using System.Reflection;
 2
 3namespace ArturRios.Extensions;
 4
 5/// <summary>
 6///     Provides extension methods for working with objects
 7/// </summary>
 8public 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        {
 626            Dictionary<string, object> dictionary = new();
 27
 4028            foreach (var propertyInfo in ReadableInstanceProperties(@object.GetType()))
 29            {
 1430                var value = propertyInfo.GetValue(@object);
 31
 1432                if (value is not null)
 33                {
 1234                    dictionary[propertyInfo.Name] = value;
 35                }
 36            }
 37
 638            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        {
 851            Dictionary<string, object?> dictionary = new();
 52
 4853            foreach (var propertyInfo in ReadableInstanceProperties(@object.GetType()))
 54            {
 1655                var value = propertyInfo.GetValue(@object);
 56
 1657                dictionary[propertyInfo.Name] = value;
 58            }
 59
 860            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) =>
 2672        type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
 9073            .Where(property => property.CanRead && property.GetIndexParameters().Length == 0);
 74}