两个不同的具有相同命名空间的 DLL

我有两个具有相同命名空间的 DLL 文件,但它们具有不同的方法和类型。 如何在项目中引用这两个 DLL 并使用它们的方法和类型?

顺便说一下,这两个 DLL 具有一些名称相同但实现不同的方法和类型以及一些惟一的方法和类型。

69620 次浏览

There's nothing special you need to do - just reference them and use the types. Namespaces can span accross several assemblies without problems, because they're not really opaque types. A namespace is just a way of adding a common prefix to all the types it contains, allowing you to have multiple types of the same name under different namespaces. (The framework doesn't see them as having the same names, because it sees the "fully qualified" name of everything - which has an alias and a namespace attached to the front of it.)

In the rare event that you reference 2 assemblies which have the same type names and the same namespaces (such as 2 different versions of the same dll) - you can distinguish which assembly to use for a given type using an alias. The default alias for all references is global, but you can specify your own alias for any assembly when you reference it (using a compiler switch - or just use the properties box in Visual Studio) - and have an extern alias <name> clause at the top of your code file where you use it - you would access the types from different assemblies with <name>::MyNamespace.Type

you can use the alias feature of the /reference (Import Metadata) (C# Compiler Options) compiler option to solve your problems, read from here for more details

If you have 2 types with the exact same name (note that the name includes the namespace) but in different DLLs and you are interested in using both of them, then you can do this.

Short Answer

You have type Acme.Foo in 2 different DLLs and you want to use them. Give the reference an alias in the reference properties window (View | Properties Window) then use it like this:

extern alias TheAliasYouGaveTheReference


TheAliasYouGaveTheReference::Acme.Foo f = new
TheAliasYouGaveTheReference::Acme.Foo();

The default namespace is global for any C# program but note above we are using the alias we created instead of global.

The best approach is to NOT get into a situation like this in the first place, if both assemblies are your own, then do not create 2 types with the exact same name in the exact same namespace. But sometimes we do not control the source code so for those times, the above solution can be used.

Long Answer

I am copying most of the article from here so it is recorded here in case the article is no longer available.

How do you get into a situation like this?

Firstly, here is how you can replicate the scenario so it is really clear what we are talking about:

  1. Create a C# Class Library called FooVersion1
  2. Replace the template code in Class1.cs with the following:

    using System;
    
    
    namespace Acme
    {
    public class Foo
    {
    public void Bar()
    {
    Console.WriteLine("Bar");
    }
    }
    }
    
  3. Right-click on the solution in solution explorer and select Add | New Project

  4. Save the current project (only applicable in express)
  5. Select a Class Library in the new project dialog and change the project name to FooVersion2 and press OK
  6. Replace the code in Class1.cs with the following:

    using System;
    
    
    namespace Acme
    {
    public class Foo
    {
    public void Bar()
    {
    Console.WriteLine("Bar");
    }
    
    
    public void Goo()
    {
    Console.WriteLine("Goo");
    }
    }
    }
    

Usage of the type in Application

Ok so now we have 2 different assemblies containing Acme.Foo. Let's now create a console application and try to use each one.

  1. Right-click on the solution in solution explorer and select Add | New Project
  2. Select a Console Application and call it Consumer
  3. Right-click on Consumer and select ‘Set as startup project’
  4. Right-click on the references node in the Consumer project and select ‘Add Reference’
  5. Click on the projects tab, and multi-select FooVersion1 and FooVersion2 Click OK
  6. Add the following line to Main in the Program type of the Consumer project:

    Acme.Foo f = new Acme.Foo();
    

Build the solution via Ctrl+Shift+B (or F6) Notice that you get two build errors [as shown below]:

enter image description here

The Fix

Here is how we can fix it:

  1. Open solution explorer and select FooVersion1 in the References folder of the Consumer project
  2. Hit F4 (or select View | Properties Window)
  3. Change the Aliases property to FooVersion1
  4. Build the solution
  5. Now everything will build correctly, because Acme.Foo unambiguously refers to FooVersion2
  6. Add the following directive to the top of Program.cs in the Consumer project:

    extern alias FooVersion1;
    
  7. Change the usage of Acme.Foo to:

    FooVersion1::Acme.Foo f = new FooVersion1::Acme.Foo();
    f.Bar();
    
  8. Notice that when you type ‘f.’ the completion list contains only those methods in FooVersion1 of Acme.Foo (notably it does not include Goo)

  9. Build the solution and everything will build correctly
  10. Finally add the following code under f.Bar() in Program.cs of the Consumer project:

    Acme.Foo f2 = new Acme.Foo();
    f2.Goo();
    
  11. Notice that f2’s completion list contains Goo.

  12. Build again using Ctrl+Shift+B and notice that there are still no build errors