为什么 AssemblyResolver 即使在加载 dll 时(手动)也会触发?

Why AssemblyResolver fire even when dll is loaded (manualy)?

我有 2 个项目的 C# 解决方案:

  1. DLLTest(控制台应用程序)
  2. BLib(图书馆)

在 DLLTest 中,我设置了对 BLib 的引用并将复制本地 属性 设置为 false。
编译解决方案。
将 BLib.dll 复制到 'C:\BLib.dll' 和 运行 应用程序。
在我的代码的第一步中,我从路径 'C:\BLib.dll' 加载程序集,然后从那里调用方法。从 BLib 程序集调用方法时,会触发 AssemblyResolver 并尝试加载我之前手动加载的程序集。

我可以做一些事情让应用程序知道库已经加载并且不再尝试加载它吗?
这是来自 BLib 项目的 BClass.cs 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLib
{
    public class BClass
    {
        public static void PrintName()
        {
            Console.WriteLine("BLib");
        }
    }
}

这是来自 DLLTest 项目的 Program.cs 文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace DLLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            Assembly.LoadFile(@"C:\BLib.dll");
            Console.WriteLine("Loaded assembles:");
            AppDomain.CurrentDomain.GetAssemblies().ToList()
                .ForEach(p => Console.WriteLine(p));
            Console.WriteLine("End list of assembles");
            try
            {
                PrintMessage();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }

        private static void PrintMessage()
        {
            BLib.BClass.PrintName();
        }

        private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            Console.WriteLine(args.Name);
            return null;
        }
    }
}

我找到了类似的问题 Why is AssemblyResolve called when Assembly is in CurrentDomain? Answer from there follow me to Choosing a Binding Context,其中对所有内容进行了详细描述(行 都不是 缺点 )。

我是这么认为的,这是安全原因。