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 的热卸载。
        }
    }
相关推荐
冷冷的菜哥4 天前
ASP.NET Core文件分片上传
c#·asp.net·asp.net core·文件分片上传
前端世界6 天前
ASP.NET 实战:用 SqlCommand 打造一个安全的用户注册功能
后端·安全·asp.net
冷冷的菜哥7 天前
ASP.NET Core上传文件到minio
后端·asp.net·上传·asp.net core·minio
忧郁的蛋~8 天前
在.NET标准库中进行数据验证的方法
后端·c#·asp.net·.net·.netcore
jiasting15 天前
高通平台wifi--p2p issue
asp.net·p2p·issue
一枚小小程序员哈17 天前
基于asp.net 的在线餐饮订餐系统的设计与实现/基于c#的网上订餐系统/餐厅管理系统
后端·c#·asp.net
一枚小小程序员哈24 天前
基于微信小程序的家教服务平台的设计与实现/基于asp.net/c#的家教服务平台/基于asp.net/c#的家教管理系统
后端·c#·asp.net
做一位快乐的码农25 天前
基于.net、C#、asp.net、vs的保护大自然网站的设计与实现
c#·asp.net·.net
一枚小小程序员哈1 个月前
基于C#、.net、asp.net的心理健康咨询系统设计与实现/心理辅导系统设计与实现
c#·asp.net·.net
Archy_Wang_11 个月前
ASP.NET 上传文件安全检测方案
后端·c#·asp.net