本文介绍了使用两个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中访问了一些类型,​​但这些类型具有相同的完全限定名。因此,可以说DLL1是companyDLL.dll - > someProduct.Type1DLL 2是companyDLL.dll - > someProduct.Type1

I need to access some types within each DLL, but these types have the same fully qualified name.So lets say DLL1 is companyDLL.dll -> someProduct.Type1DLL 2 is companyDLL.dll -> someProduct.Type1

我怎么能同时访问同一个项目中的类型1?

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

我已经尝试过使用外部别名,但它要求我改变的DLL的名字之一。

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

请让我知道是否需要我的问题进一步澄清。

Please let me know if further clarification is required for my question.

推荐答案

如果你的两个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.

在你的code时使用的DLL使用外部别名来指定哪些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文件到项目debug文件夹中。它说:assembly1的名字你可以找到这个名字,你引用的DLL后,生成项目,并用记事本打开的csproj文件。寻找什么将是我下面的例子code。

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>Resources\Assembly1.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具有相同的名称和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:31