< Summary

Information
Class: MyNet.Primitives.ObjectExtensions
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Extensions/ObjectExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 100
Line coverage: 100%
Branch coverage
66%
Covered branches: 8
Total branches: 12
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CastIn(...)100%11100%
IfNull(...)50%22100%
IfIs(...)50%22100%
OrThrow(...)50%22100%
Or(...)100%22100%
IfNotNull(...)50%22100%
ConvertTo(...)100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Extensions/ObjectExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ObjectExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Runtime.CompilerServices;
 9
 10#pragma warning disable IDE0130 // Namespace does not match folder structure
 11namespace MyNet.Primitives;
 12#pragma warning restore IDE0130 // Namespace does not match folder structure
 13
 14public static class ObjectExtensions
 15{
 16    extension<T>(object value)
 17    {
 18        /// <summary>
 19        /// Casts the value to type T. If the value is not of type T, an <see cref="InvalidCastException"/> will be thro
 20        /// </summary>
 21        /// <returns>The value cast to type T.</returns>
 322        public T CastIn() => (T)value;
 23    }
 24
 25    extension(object? value)
 26    {
 27        /// <summary>
 28        /// Executes the specified action if the value is null.
 29        /// </summary>
 30        /// <param name="ifNull">The action to execute if the value is null.</param>
 31        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="ifNull"/> action is null.</exception>
 32        public void IfNull(Action ifNull)
 33        {
 334            ArgumentNullException.ThrowIfNull(ifNull);
 35
 336            if (value is null)
 337                ifNull();
 338        }
 39    }
 40
 41    extension<T>(object? value)
 42    {
 43        /// <summary>
 44        /// Executes the specified action if the value is of type T, passing the value cast to T as a parameter to the a
 45        /// </summary>
 46        /// <param name="action">The action to execute if the value is of type T.</param>
 47        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="action"/> is null.</exception>
 48        public void IfIs(Action<T> action)
 49        {
 350            ArgumentNullException.ThrowIfNull(action);
 51
 352            if (value is T typed)
 353                action(typed);
 354        }
 55    }
 56
 57    extension<T>(T? value)
 58    {
 59        /// <summary>
 60        /// Returns the value if it is not null; otherwise, throws an <see cref="ArgumentNullException"/> with the speci
 61        /// </summary>
 62        /// <param name="paramName">The name of the parameter that is null.</param>
 63        /// <param name="message">The message to include in the exception.</param>
 64        /// <returns>The value if it is not null.</returns>
 65        /// <exception cref="ArgumentNullException">Thrown if the value is null.</exception>
 366        public T OrThrow(string? paramName = null, string? message = null) => value ?? throw new ArgumentNullException(p
 67
 68        /// <summary>
 69        /// Returns the value if it is not null; otherwise, returns the specified fallback value.
 70        /// </summary>
 71        /// <param name="fallback">The fallback value to return if the value is null.</param>
 72        /// <returns>The value if it is not null; otherwise, the specified fallback value.</returns>
 58873        public T Or(T fallback) => value ?? fallback;
 74
 75        /// <summary>
 76        /// Executes the specified action if the value is not null, passing the non-null value as a parameter to the act
 77        /// </summary>
 78        /// <param name="ifNotNull">The action to execute if the value is not null.</param>
 79        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="ifNotNull"/> action is null.</exceptio
 80        public void IfNotNull(Action<T> ifNotNull)
 81        {
 382            ArgumentNullException.ThrowIfNull(ifNotNull);
 83
 384            if (value is not null)
 385                ifNotNull(value);
 386        }
 87
 88        /// <summary>
 89        /// Converts the value using the provided selector.
 90        /// </summary>
 91        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 92        public TResult? ConvertTo<TResult>(Func<T, TResult> selector)
 93        {
 694            ArgumentNullException.ThrowIfNull(selector);
 95
 696            return value is null ? default : selector(value);
 97        }
 98    }
 99}
 100