Resolve assembly references from another folder

后端 未结 2 631
慢半拍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:38

    Use SN.exe : SN -T VendorAssembly.dll, this will return a hex number that is the public key token, then, reference the assembly from app.config. To get the version right click your vendor assembly and use that for the codeBase version value, the href=path you mentioned.

      <configuration>
           <runtime>
              <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                 <dependentAssembly>
                    <assemblyIdentity name="VendorAssembly"  culture="neutral" publicKeyToken="307041694a995978"/>
                    <codeBase version="1.1.1.1" href="FILE://D:/ProgramFiles/VendorName/ProductName/Support/API/Bin64/VendorAssembly.dll"/>
                 </dependentAssembly>
              </assemblyBinding>
           </runtime>
        </configuration>
    
    0 讨论(0)
  • 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);
    };
    
    0 讨论(0)
提交回复
热议问题