< Summary

Information
Class: Pozitron.Convert.ObjectExtensions
Assembly: Pozitron.Convert
File(s): /home/runner/work/Pozitron.Convert/Pozitron.Convert/src/Pozitron.Convert/ObjectExtensions.cs
Tag: 6_24670647575
Line coverage
100%
Covered lines: 12
Uncovered lines: 0
Coverable lines: 12
Total lines: 58
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
To(...)100%22100%
ToNullable(...)100%22100%

File(s)

/home/runner/work/Pozitron.Convert/Pozitron.Convert/src/Pozitron.Convert/ObjectExtensions.cs

#LineLine coverage
 1using System;
 2
 3namespace Pozitron.Convert;
 4
 5/// <summary>
 6/// Conversion extensions to object
 7/// </summary>
 8public static class ObjectExtensions
 9{
 10    /// <summary>
 11    /// It converts the object value to various IConvertible types.
 12    /// </summary>
 13    /// <typeparam name="T">The target IConvertible type</typeparam>
 14    /// <param name="value">The object value to be converted.</param>
 15    /// <param name="defaultValue">The default to be returned if conversion is not successful. By default it's the defau
 16    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
 17    /// <returns>On success, it returns the converted value. On failure or null <see href="value" />, it returns the pro
 18#if NET8_0_OR_GREATER
 19    [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(defaultValue))]
 20#endif
 21    public static T? To<T>(this object? value, T? defaultValue = default, IFormatProvider? provider = null) where T : IC
 22    {
 7523        if (value is null) return defaultValue;
 24
 25        try
 26        {
 5127            return (T?)System.Convert.ChangeType(value, typeof(T), provider);
 28        }
 1029        catch (Exception)
 30        {
 1031        }
 32
 1033        return defaultValue;
 4134    }
 35
 36    /// <summary>
 37    /// It converts the object value to various IConvertible struct types.
 38    /// </summary>
 39    /// <typeparam name="T">The target IConvertible struct type</typeparam>
 40    /// <param name="value">The object value to be converted</param>
 41    /// <param name="provider">An object that supplies culture-specific formatting information.</param>
 42    /// <returns>On success, it returns the converted value. On failure or null <see href="value" />, it returns null.</
 43    public static T? ToNullable<T>(this object? value, IFormatProvider? provider = null) where T : struct, IConvertible
 44    {
 3645        if (value is null) return null;
 46
 47        try
 48        {
 2449            return (T)System.Convert.ChangeType(value, typeof(T), provider);
 50        }
 651        catch (Exception)
 52        {
 653        }
 54
 655        return null;
 1856    }
 57}
 58