Asp.net Core 反射加载dll

  1. 定义一个类库,定义接口
csharp 复制代码
namespace Plugin
{
    public interface IPlugin
    {
        void EllisTest();
    }
}
  1. 定义另外一个类库,引用上面的类库,实现接口
csharp 复制代码
using Plugin;

namespace UserCustom
{
    public class Custom : IPlugin
    {
        public  void EllisTest()
        {
            Console.WriteLine("哈哈,今天这个天气挺好的");
        }
    }
}
  1. 定义API,使用assemble加载dll
csharp 复制代码
[HttpGet(Name = "test")]
public IActionResult DirectLoad()
{
    Assembly assembly = Assembly.LoadFrom("C:\\Users\\84977\\Desktop\\UserCustom.dll");

    var pluginType = assembly.GetTypes().FirstOrDefault(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
    if (pluginType != null)
    {
        IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);

        plugin.EllisTest();
    }
    return Ok();
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
    string pluginPath = "C:\\Users\\84977\\Desktop\\UserCustom.dll";
    var pluginLoader = new PluginLoader();
    pluginLoader.LoadAndExecutePlugin(pluginPath);
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
} 
  1. 使用AssemblyLoadContext 加载dll
csharp 复制代码
public class CustomAssemblyLoadContext : AssemblyLoadContext
    {
        public CustomAssemblyLoadContext() : base(isCollectible: true)
        {
        }

        protected override Assembly Load(AssemblyName assemblyName)
        {
            return null; // 返回 null 以使用默认的加载机制
        }
    }


    public class PluginLoader
    {
        public void LoadAndExecutePlugin(string pluginPath)
        {
            var context = new CustomAssemblyLoadContext();

            // 加载插件程序集
            var assembly = context.LoadFromAssemblyPath(pluginPath);
            // 查找实现了 IPlugin 接口的类型
            var pluginType = assembly.GetTypes().FirstOrDefault(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);

            if (pluginType != null)
            {
                // 创建插件实例并调用方法
                var plugin = (IPlugin)Activator.CreateInstance(pluginType);
               
                plugin.EllisTest();
            }
            assembly = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            context.Unload();
            // 在此处,当 using 块结束时,AssemblyLoadContext 会被卸载,从而实现 DLL 的热卸载。
        }
    }
相关推荐
[email protected]19 小时前
ASP.NET Core SignalR - 部分客户端消息发送
后端·asp.net·.netcore
邪恶的贝利亚1 天前
从webrtc到janus简介
后端·asp.net·webrtc
不超限1 天前
Asp.net core 使用EntityFrame Work
后端·asp.net
不超限2 天前
Asp.net Core 通过依赖注入的方式获取用户
后端·asp.net
新知图书2 天前
下载和安装Visual Studio(开发ASP.NET MVC应用)
ide·asp.net·visual studio
vvilkim4 天前
ASP.NET Core 中间件深度解析:构建灵活高效的请求处理管道
后端·中间件·asp.net
不超限4 天前
Asp.Net Core基于StackExchange Redis 缓存
redis·缓存·asp.net
[email protected]7 天前
ASP.NET Core SignalR 身份认证集成指南(Identity + JWT)
后端·中间件·asp.net·.netcore
[email protected]7 天前
ASP.NET Core SignalR的基本使用
后端·asp.net·.netcore
[email protected]8 天前
Asp.Net Core FluentValidation校验框架
后端·asp.net·.netcore