< Summary

Information
Class: MyNet.Utilities.Plugins.PluginLoadContext
Assembly: MyNet.Utilities
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Plugins/PluginLoadContext.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 6
Coverable lines: 6
Total lines: 47
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Load(...)0%620%
LoadUnmanagedDll(...)0%620%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Plugins/PluginLoadContext.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="PluginLoadContext.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Reflection;
 8using System.Runtime.Loader;
 9
 10namespace MyNet.Utilities.Plugins;
 11
 12/// <summary>
 13/// An isolated <see cref="AssemblyLoadContext"/> used to load a single plugin assembly
 14/// and its dependencies without polluting the default load context.
 15/// Each plugin subdirectory gets its own <see cref="PluginLoadContext"/> so that
 16/// different plugins can reference different versions of the same dependency.
 17/// </summary>
 18/// <param name="pluginPath">
 19/// The full path to the plugin DLL. The <see cref="AssemblyDependencyResolver"/>
 20/// uses this path to locate sibling dependency assemblies.
 21/// </param>
 022internal sealed class PluginLoadContext(string pluginPath) : AssemblyLoadContext
 23{
 024    private readonly AssemblyDependencyResolver _resolver = new(pluginPath);
 25
 26    /// <summary>
 27    /// Resolves the managed assembly by probing the plugin's own dependency graph first.
 28    /// Returns <c>null</c> to fall back to the default context when the assembly is not
 29    /// part of the plugin (e.g., shared framework assemblies).
 30    /// </summary>
 31    protected override Assembly? Load(AssemblyName assemblyName)
 32    {
 033        var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
 034        return assemblyPath != null ? LoadFromAssemblyPath(assemblyPath) : null;
 35    }
 36
 37    /// <summary>
 38    /// Resolves a native (unmanaged) library by probing the plugin's dependency graph.
 39    /// Returns <see cref="nint.Zero"/> to fall back to the OS search when not found.
 40    /// </summary>
 41    protected override nint LoadUnmanagedDll(string unmanagedDllName)
 42    {
 043        var libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
 044        return libraryPath != null ? LoadUnmanagedDllFromPath(libraryPath) : nint.Zero;
 45    }
 46}
 47