本文介绍了使用两个具有相同名称和相同命名空间的 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目需要引用两个同名的 DLL.DLL 不是强命名的,具有完全相同的名称.

I have a project which needs to reference two DLLs with the same name. The DLLs are not strong named, have the same exact name.

我需要访问每个 DLL 中的某些类型,但这些类型具有相同的完全限定名称.所以假设第一个是带有 someProduct.Type1 的 companyDLL.dll 和第二个是带有 someProduct.Type1 的 companyDLL.dll.

I need to access some types within each DLL, but these types have the same fully qualified name.So let's say the first one is companyDLL.dll with someProduct.Type1 andthe second one is companyDLL.dll with someProduct.Type1.

如何在同一个项目中访问两个 Type1 类?

How can I access both Type1 classes within the same project?

我已经尝试过使用 extern alias,但它需要我更改其中一个 DLL 的名称.

I have already tried using extern alias, but it requires me to change the name of one of the DLLs.

推荐答案

如果您的两个 DLL 具有相同的名称,您将不得不重命名它们.如Assembly1.dll 和Assembly2.dll.

If your two DLLs have the same name, you are going to have to rename them. Such as Assembly1.dll and Assembly2.dll.

像往常一样将这些 DLL 作为引用添加到您的项目中,并在每个引用的属性中指定一个别名.

Add these DLLs as a reference in your project as you normally would and in the properties for each reference specify an alias.

在使用 DLL 的代码中,使用 extern alias 来指定要引用的 dll.

in your code when using the DLLs use extern alias to specify what dll you want to reference.

extern alias Assembly1Reference;
using Assembly1Reference::AssemblyNamespace.MyClass;

如果你保持这样,你很可能会得到一个 FileNotFoundException 说它无法加载文件或程序集.要解决此问题,您需要添加一个 ResolveEventHandler 来加载您尝试使用的正确程序集.为此,您必须准确指定 DLL 文件的存储位置.在下面的情况下,我手动将 Dll 文件复制到项目调试文件夹.在它显示程序集名称"的地方,您可以在引用 DLL、构建项目并使用记事本打开 csproj 文件后找到该名称.要查找的内容将在我的示例代码下方.

If you leave it like this, you will most likely get a FileNotFoundException saying that it Could not load file or assembly. To fix this you need to add a ResolveEventHandler that will load the correct assembly you are trying to use. To do this you have to specify exactly where you are storing your DLL files. In the case below, I manually copied the Dll files to the projects debug folder. Where it says "name of assembly1" you can find the name after you reference the DLL, build the project, and open the csproj file with notepad. What to look for will be below my example code.

extern alias Assembly1Reference;
extern alias Assembly2Reference;

static void Load()
{
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
    Do();
}

static void Do()
{
    new Assembly1Reference.Assembly.Class();
    new Assembly2Reference.Assembly.Class();
}

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string currentPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    if(args.Name == "Name of assembly1")//Found in csproj file after referenced and built
    {
        return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(currentPath, "Assembly1.dll"));
    }
    if(args.Name == "Name of assembly2")//Found in csproj file after referenced and built
    {
        return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(currentPath, "Assembly2.dll"));
    }
    return null;
}

正如所承诺的,csproj 文件中的引用如下所示.名称是包含属性中的所有内容.

As promised, here is what a reference looks like in the csproj file. The name is everything inside the include attribute.

<Reference Include="MyAssembly_3.6.2.0, Version=3.6.2.0, Culture=neutral, PublicKeyToken=12341234asdafs43, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>ResourcesAssembly1.dll</HintPath>
      <Aliases>Assembly1Reference</Aliases>
</Reference>

我知道这已经晚了,但希望它可以帮助从现在开始访问此页面的任何人.

I know this is late but hopefully it will help anyone coming to this page from now on.

这篇关于使用两个具有相同名称和相同命名空间的 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:31