Reference two equal assemblies, only public keys differ

╄→гoц情女王★ 提交于 2019-11-29 10:45:44

I wonder if AssemblyResolve would work:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name == "full name with old key")
    {
        return typeof(SomeTypeInReferencedAssembly).Assembly;
    }
    return null;
}

He're I'm assuming that you've referenced your preferred version and can use the type SomeTypeInReferencedAssembly to get the Assembly; you could also use:

return Assembly.Load("full name with new key");

I haven't tried it, but here I'm essentially saying "deploy one version of the dll, and when the system asks for the old one - lie".

You should never have two assemblies with the same version but different public keys, that's a recipe for disaster. If the actual assembly versions are different, then the absolute easiest solution is to place them in the global assembly cache (GAC). This will not, however, play nicely if you'll be dealing with instances of types that are defined in C and used in both A and B (for example, C declares MyType, and you obtain an instance of MyType from B and pass it to A. As far as the runtime is concerned, these two types have absolutely nothing to do with one another aside from sharing a name).

If you're looking for a temporary solution, then I would go with Marc's; it sounds like it should work perfectly. That being said, however, the fact that you're going down this road should be a giant red flag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!