Micro 插件开发(.NET Core Plugins)

Micro 插件开发(.NET Core Plugins)

1、bat 语法

参考:https://www.jb51.net/article/5828.htm

2、写入 bat 和执行 bat

参考:https://www.cnblogs.com/wxwx/p/5821059.html

3、WinForm接收命令行参数

参考:https://www.cnblogs.com/joean/p/4870495.html

 4、c#.NET 调用命令控制台 并获取执行结果

参考:https://blog.csdn.net/wangcg123/article/details/46681781

5、.NET Core使用命令行参数库构建控制台应用程序

参考:https://blog.csdn.net/weixin_29576901/article/details/113453292

6、NET Core 命令行应用程序-CommandLineUtils

参考:

https://blog.csdn.net/qq_27843785/article/details/103965036

https://www.cnblogs.com/wangyulong/p/9296164.html

https://www.cnblogs.com/TianFang/p/8424256.html

创建插件

namespace Micro.Plugins
{
    public class Class1 : ICommand
    {
        public int Execute()
        {
            return 10000;
        }
    }
}

 

调用类库(dll)

[Route("api/[controller]")]
[ApiController]
public class PluginController : ControllerBase
{
    [HttpGet]
    public ActionResult Get()
    {
        try
        {
            string fileName = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins\\Micro.Plugins.dll".Replace('\\', Path.DirectorySeparatorChar)));
            AssemblyLoadContext abContext = new AssemblyLoadContext(Guid.NewGuid().ToString(), true);
            using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            var assembly = abContext.LoadFromStream(stream);
            var type = assembly.ExportedTypes.FirstOrDefault();
            if (type == null) 
                return Ok(ResponseMessage.Fail("未将对象引用到对象实例"));
            var plugin = Activator.CreateInstance(type);
            var res = type.GetMethod("Execute")?.Invoke(plugin, null);
            if(res == null)
                return Ok(ResponseMessage.Fail("未将方法引用到对象实例"));
            abContext.Unload();

            return Ok(ResponseMessage.Success(res));
        }
        catch (Exception ex)
        {
            return Ok(ResponseMessage.Fail(ex.Message));
        }
    }
}

 

  

posted @ 2021-05-18 09:24  microsoft-zhcn  阅读(160)  评论(0编辑  收藏  举报