Resolve assembly references from another folder

后端 未结 2 653
慢半拍i
慢半拍i 2020-11-27 19:49

I am developing an application which references and uses some third party assemblies from a certain Vendor; in development box I have these 3 assemblies in a reference folde

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 20:39

    You should first find the folder where theses dlls are installed then use AppDomain.AssemblyResolve to hook assembly resolution and try to load the requested assemblies from this folder.

    It will look something like this (not tested, and you need to check what args.Name contain exactly, could contain the version and the strong name along with type name) :

    var otherCompanyDlls = new DirectoryInfo(companyFolder).GetFiles("*.dll");
    
    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
    {
        var dll = otherCompanyDlls.FirstOrDefault(fi => fi.Name == args.Name);
        if (dll == null)
        {
            return null;
        }
    
        return Assembly.Load(dll.FullName);
    };
    

提交回复
热议问题