| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ObjectExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | |
| | | 10 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 11 | | namespace MyNet.Primitives; |
| | | 12 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 13 | | |
| | | 14 | | public 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> |
| | 3 | 22 | | 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 | | { |
| | 3 | 34 | | ArgumentNullException.ThrowIfNull(ifNull); |
| | | 35 | | |
| | 3 | 36 | | if (value is null) |
| | 3 | 37 | | ifNull(); |
| | 3 | 38 | | } |
| | | 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 | | { |
| | 3 | 50 | | ArgumentNullException.ThrowIfNull(action); |
| | | 51 | | |
| | 3 | 52 | | if (value is T typed) |
| | 3 | 53 | | action(typed); |
| | 3 | 54 | | } |
| | | 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> |
| | 3 | 66 | | 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> |
| | 588 | 73 | | 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 | | { |
| | 3 | 82 | | ArgumentNullException.ThrowIfNull(ifNotNull); |
| | | 83 | | |
| | 3 | 84 | | if (value is not null) |
| | 3 | 85 | | ifNotNull(value); |
| | 3 | 86 | | } |
| | | 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 | | { |
| | 6 | 94 | | ArgumentNullException.ThrowIfNull(selector); |
| | | 95 | | |
| | 6 | 96 | | return value is null ? default : selector(value); |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |