< Summary

Information
Class: MyNet.Primitives.Helpers.ResourcesHelper
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Helpers/ResourcesHelper.cs
Tag: 323_28699572109
Line coverage
91%
Covered lines: 11
Uncovered lines: 1
Coverable lines: 12
Total lines: 56
Line coverage: 91.6%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
OpenEmbeddedResource(...)87.5%8891.66%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Helpers/ResourcesHelper.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ResourcesHelper.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.IO;
 9using System.Reflection;
 10
 11namespace MyNet.Primitives.Helpers;
 12
 13/// <summary>
 14/// Helper class for working with embedded resources in assemblies.
 15/// </summary>
 16public static class ResourcesHelper
 17{
 18    /// <summary>
 19    /// Opens an embedded resource stream from the specified assembly based on a resource name suffix.
 20    /// </summary>
 21    /// <param name="assembly">The assembly containing the embedded resource.</param>
 22    /// <param name="resourceNameSuffix">The suffix of the resource name to match.</param>
 23    /// <param name="comparison">The string comparison type to use when matching the resource name.</param>
 24    /// <returns>A stream representing the embedded resource, or null if not found.</returns>
 25    public static Stream OpenEmbeddedResource(
 26        Assembly assembly,
 27        string resourceNameSuffix,
 28        StringComparison comparison = StringComparison.OrdinalIgnoreCase)
 29    {
 2130        ArgumentNullException.ThrowIfNull(assembly);
 1831        ArgumentException.ThrowIfNullOrWhiteSpace(resourceNameSuffix);
 32
 933        string? match = null;
 34
 3635        foreach (var resourceName in assembly.GetManifestResourceNames())
 36        {
 937            if (!resourceName.EndsWith(resourceNameSuffix, comparison))
 38            {
 39                continue;
 40            }
 41
 642            if (match is not null)
 43            {
 044                throw new InvalidOperationException($"Multiple embedded resources match '{resourceNameSuffix}'.");
 45            }
 46
 647            match = resourceName;
 48        }
 49
 950        return match is null
 951            ? throw new InvalidOperationException(
 952                $"Embedded resource '{resourceNameSuffix}' not found.")
 953            : assembly.GetManifestResourceStream(match)!;
 54    }
 55}
 56