通过 AppDomain 的 ShadowCopying 功能实现 Assembly 的动态替换

本文探讨了如何利用AppDomain的ShadowCopying特性,实现在运行时动态替换Assemblies,以达到更新插件或保护主程序的目的。详细讲解了相关代码实现和Demo展示。
摘要由CSDN通过智能技术生成
// ShadowCopyDemo.cs
namespace ShadowCopyDemo
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;
    using System.Security;
    using System.Security.Permissions;
    using System.Threading;

    sealed class ShadowCopyDemo
    {
        private readonly string             _shadowCopyCachePath;
        private readonly string             _pluginDirectoryName;
        private readonly string             _pluginPath;
        private readonly string             _candidatesPath;

        private bool        _isVersion1;
        private AppDomain   _sandbox;
        private PluginHost  _pluginHost;

        private readonly FileSystemWatcherEx _fsWatcher;

        public ShadowCopyDemo()
        {
            this._shadowCopyCachePath = Path.Combine(
                Environment.GetEnvironmentVariable("TEMP"),
                Assembly.GetEntryAssembly().GetName().Name);

            this._pluginDirectoryName   = "Plugins";
            this._isVersion1            = true;

            this._pluginPath = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, 
                this._pluginDirectoryName);

            this._candidatesPath = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory,
                "Candidates");

            this.GetSandboxAndHost(out this._sandbox, out this._pluginHost);

            this._fsWatcher = new FileSystemWatcherEx(this._pluginPath, "*.dll");
            this._fsWatcher.Changed += FileSystemWatcherOnChanged;
            this._fsWatcher.EnableRaisingEvents = true;
        }

        public static void Main()
        {
            ShadowCopyDemo o = new ShadowCopyDemo();
            o._pluginHost.LoadPlugins();
            o._pluginHost.Invoke();

            string cmd = ""
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值