Two different DLLs with the same namespace

I have two DLL files that have the same namespace, but they have different functions and types. How can I reference both DLLs in my project and use their functions and types?

By the way

. these two DLLs have some functions and types with the same name and different implementation and some unique functions and types

+45
c # visual-studio-2010
Sep 09 '10 at 0:20
source share
3 answers

You don’t need to do anything special - just reference them and use types. Namespaces can span multiple assemblies without problems because they are not very opaque types. A namespace is just a way to add a common prefix for all the types it contains, which allows you to have multiple types with the same name in different namespaces. (The frame does not consider them to be the same name, because it sees the "fully qualified" name of everything that has an alias and namespace attached to it.)

In the rare case, when you refer to 2 assemblies that have the same type names and the same namespace (for example, 2 different versions of the same library), you can distinguish which assembly is used for the given type using an alias. The default alias for all links is global , but you can specify your own alias for any assembly when you reference it (using the compiler switch, or just use the properties window in Visual Studio) and have the extern alias <name> clause in at the top of your code file, where you use it, you will access types from different assemblies using <name>::MyNamespace.Type

+68
09 Sep 2018-10-09T00:
source share

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

Short answer

You have an Acme.Foo type in two different DLLs and you want to use them. Give the link to the alias in the help properties window (View | Properties Window), then use it as follows:

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

The default namespace is global for any C # program , but note that we use the alias that we created instead of global .

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

Long answer

I copy most of the article from here , so it is written here if the article is no longer available.

How do you get into this situation?

First, here is how you can replicate the script, so it’s really clear what we are talking about:

  • Create a C # class library called FooVersion1
  • Replace the template code in Class1.cs as follows:

     using System; namespace Acme { public class Foo { public void Bar() { Console.WriteLine("Bar"); } } } 
  • Right-click on a solution in Solution Explorer and select Add | New project

  • Save current project (applicable only in express)
  • Select the class library in the new project dialog and change the project name to FooVersion2 and click OK
  • Replace the code in Class1.cs as follows:

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

Using a Type in an Application

So now we have two different assemblies containing Acme.Foo . Now create a console application and try to use each.

  • Right-click on a solution in Solution Explorer and select Add | New project
  • Select a console application and name it "Consumer"
  • Right-click Consumer and select Install as Startup Project
  • Right-click on the node links in the Consumer project and select Add Link
  • Go to the “Projects” tab and select “FooVersion1” and “FooVersion2” Click OK
  • Add the following line to Main in the Consumer project program type:

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

Create a solution using Ctrl + Shift + B (or F6) Note that you get two build errors [as shown below]:

enter image description here

Correction

Here's how we can fix it:

  • Open Solution Manager and select FooVersion1 in the Links folder of the Consumer project
  • Press F4 (or select View | Properties Window)
  • Change the Aliases property to FooVersion1
  • Create a solution
  • Now everything will be built correctly, because Acme.Foo definitely refers to FooVersion2
  • Add the following directive to the beginning of the Program.cs program in the Consumer project:

     extern alias FooVersion1; 
  • Change the use of Acme.Foo to:

     FooVersion1::Acme.Foo f = new FooVersion1::Acme.Foo(); f.Bar(); 
  • Please note that when you enter "f. The completion list contains only those methods in FooVersion1 Acme.Foo (in particular, it does not include Goo)

  • Build a solution and everything will be built correctly.
  • Finally, add the following code in the f.Bar () file to the Consumer project's Program.cs:

     Acme.Foo f2 = new Acme.Foo(); f2.Goo(); 
  • Note that the f2s completion list contains Goo.

  • Again, create with Ctrl + Shift + B and note that there are no build errors.
+5
Jul 26 '17 at 16:40
source share

you can use the alias function of the compiler options / Import metadata (C # Compiler Options) to solve your problems, read here for more details

+4
Sep 09 '10 at 1:23
source share



All Articles