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 的热卸载。
        }
    }
相关推荐
步步为营DotNet4 小时前
解锁.NET 11 新境:ASP.NET Core 10 在微服务安全通信的深化与实践
微服务·asp.net·.net
无风听海1 天前
深入理解 ASP.NET Core 中的 IActionResult
后端·asp.net
无风听海1 天前
ASP.NET Core Results<T1, T2>深度解析
后端·asp.net
大熊程序猿3 天前
ASP.NET Core 认证授权:JWT与OAuth2实战
后端·asp.net
超級二蓋茨4 天前
asp.net core中JwtBearerEvents中几个事件的生命周期
java·服务器·asp.net
编码者卢布6 天前
【Azure Developer】ASP.NET Framework 4.8 集成 Azure Application Insights SDK 完整指南
microsoft·asp.net·azure
祀爱6 天前
定时任务之BackgroundService的详细教程
后端·c#·asp.net
祀爱7 天前
Asp.net core+ Layui 项目中编辑按钮传递数据的方法
前端·c#·asp.net·layui
祀爱7 天前
ASP.NET Core 集成NLog详细教程
数据库·后端·asp.net
gCode Teacher 格码致知8 天前
Asp.net Mvc教学:LINQ to Objects和 LINQ to Entities的经典案例-由Deepseek产生
asp.net·mvc·linq