zoukankan      html  css  js  c++  java
  • netcore热插拔dll

    项目中有有些场景用到反射挺多的,用到了反射就离不开dll的加载。此demo适用于通过反射dll运行项目中加载和删除,不影响项目。

    ConsoleApp:

     1 using AppClassLibrary;
     2 using System;
     3 using System.Collections.Generic;
     4 using System.IO;
     5 using System.Linq;
     6 using System.Runtime.Loader;
     7 using System.Threading;
     8 
     9 namespace ConsoleApp
    10 {
    11     class Program
    12     {
    13         static void Main(string[] args)
    14         {
    15             Console.WriteLine("************非反射调用**************");
    16             ICommon common = new Say();
    17             common.Invoke();
    18             Console.WriteLine("************非反射调用**************");
    19             Console.WriteLine();
    20             Console.WriteLine("**************反射调用***************");
    21             InvokeMethod();
    22             Console.WriteLine("**************反射调用***************");
    23             Console.Read();
    24         }
    25         private static void InvokeMethod()
    26         {
    27             bool IsGo = true;
    28             while (IsGo)
    29             {
    30                 Thread.Sleep(3000);
    31                 List<ICommon> common_List = new List<ICommon>();
    32                 var path = Directory.GetCurrentDirectory();
    33                 string[] fileInfos = Directory.GetFiles(path).Where(f => f.Contains("comm")).ToArray();
    34                 var _AssemblyLoadContext = new AssemblyLoadContext(Guid.NewGuid().ToString("N"), true);
    35                 Dictionary<string, ICommon> fileInfoList = new Dictionary<string, ICommon>();
    36                 foreach (var file in fileInfos)
    37                 {
    38                     ICommon common;
    39                     using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
    40                     {
    41                         var assembly = _AssemblyLoadContext.LoadFromStream(fs);
    42                         Type type = assembly.GetExportedTypes().Where(t => t.GetInterfaces().Contains(typeof(ICommon))).FirstOrDefault();
    43                         common = Activator.CreateInstance(type) as ICommon;
    44                     }
    45                     FileInfo fileInfo = new FileInfo(file);
    46                     common.Invoke();
    47                 }
    48             }
    49         }
    50     }
    51     public class Say : ICommon
    52     {
    53         public void Invoke()
    54         {
    55             Console.WriteLine($"{this.GetType()} Say Method ");
    56         }
    57     }
    58 }

    接口类库:

    using System;
    
    namespace AppClassLibrary
    {
        public interface ICommon
        {
            void Invoke();
        }
    }

    comm_Print.dll:

    using AppClassLibrary;
    using System;
    
    namespace comm_Print
    {
        public class Print : ICommon
        {
            public void Invoke()
            {
                Console.WriteLine($"{this.GetType()} Print Method ");
            }
        }
    }

    comm_Trace.dll: 

    using AppClassLibrary;
    using System;
    
    namespace comm_Trace
    {
        public class Trace : ICommon
        {
            public void Invoke()
            {
                Console.WriteLine($"{this.GetType()} Trace Method ");
            }
        }
    }

    下面2个dll可以任意添加删除:

    删除trace.dll运行结果:

    Fork me on GitHub
  • 相关阅读:
    python 学习 第一天
    pip 自己的源 搭建
    hadoop 完全分布式部署
    OpenCV Python 数字图像处理 基础系列(0)环境配置与准备环节
    STM32学习笔记 —— 0.1 Keil5安装和DAP仿真下载器配置的相关问题与注意事项
    TensorFlow学习笔记(1)—— 基本概念与框架
    算法研讨会-含有回溯的递归算法设计探讨
    Nvidia Jetson TX2开发板学习历程( 2 )- 更换pip源,提高下载速度
    STM32学习笔记 —— 1.1 什么是寄存器(概念分析)
    Nvidia Jetson TX2开发板学习历程(1)- 详细开箱、上电过程
  • 原文地址:https://www.cnblogs.com/morec/p/14581268.html
Copyright © 2011-2022 走看看