gpt4 book ai didi

c# - 我可以在引用程序集内的程序集上使用 bindingRedirect

转载 作者:太空狗 更新时间:2023-10-29 18:00:09 26 4
gpt4 key购买 nike

我们有一个插件文件夹,我们可以从中加载程序集。大多数情况下这很好。但是,我们有 1 个使用 System.Core 版本 2.0.5.0 的第 3 方插件。

我们使用 .Net 4,因此我们在 PC 上加载了 System.Core 4.0.0.0。

加载插件时,我们收到一个错误,显示为 System.Core Version 2.0.5.0。无法解决。

我认为这会有所帮助:

<dependentAssembly>
<assemblyIdentity name="System.Core"
publicKeyToken="7cec85d7bea7798e"
culture="neutral" />
<bindingRedirect oldVersion="2.0.5.0"
newVersion="4.0.0.0"/>
</dependentAssembly>

但它没有。

如何强制引用 .dll 使用我拥有的 System.Core 版本?

这是正确的方法吗?

==================================

这是我们用来注册插件的代码:

internal class TestCode
{
FileInfo[] assemblies;

public void GoFish()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

foreach (string directory in Directory.GetDirectories(@"E:\Plugins"))
{
assemblies = new DirectoryInfo(directory).GetFiles("*.dll");
foreach (string assemblyFile in Directory.GetFiles(directory, "*.dll"))
{
try
{
FileInfo fi = new FileInfo(assemblyFile);
var assembly = Assembly.LoadFile(fi.FullName);
IntegrationAssemblyAttribute integrationAssemblyAttribute = (IntegrationAssemblyAttribute)assembly.GetCustomAttribute(typeof(IntegrationAssemblyAttribute));
}
catch (Exception ex)
{
//Exception handling
Console.WriteLine("An error has occured while loading plugin from loacation:{0}\n{1}", assemblyFile, ex);
}
}
}
}

Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var reference = assemblies.FirstOrDefault(file => file.Name == args.Name.Split(',').ToList()[0] + ".dll");
if (null == reference)
{
return null;
}
return Assembly.LoadFile(reference.FullName);

}
}

public sealed class IntegrationAssemblyAttribute : Attribute
{
public Guid Guid { get; set; }
public IntegrationAssemblyAttribute(string assemblyGuid)
{
Guid = Guid.Parse(assemblyGuid);
}
}

最佳答案

据我所知,程序集重定向仅从最终应用程序的角度起作用。因此,您必须将重定向添加到应用程序的 app.config/web.config。这很烦人,因为它仍然创建了早期“DLL hell ”的现代版本。

关于c# - 我可以在引用程序集内的程序集上使用 bindingRedirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com