What if two classes in the same namespace are included in two different libraries in C #

I have a program (the main application that consists of old code) that consumes the library. Unfortunately, both the main application and the library use the classes (with the same names and the same properties) that are called Softwares.SoftwareXSD. When I use a class defined internally Softwares.SoftwareXSD, the main program complains about ambiguity. However, Visual Studio says it's wise to select inside duplicates by displaying a green underline under the class name. I think this is not a good approach.

Are there any problems with this? What is the best workaround for this situation?

The problem is that some classes in XSD are specific to the main application, and some to the library, but these classes are linked through links.

+3
source share
3 answers

If I understand your question correctly, you have two classes Softwares.SoftwareXSDin different assemblies (main application and library), whose full name is identical.

To solve this problem, go to Solution Explorer in Visual Studio, expand Links, right-click the link to your library, and select properties.

In “Aliases,” replace “global” with another nickname, for example. "Library".

Now you can fix the errors as follows:

global::Softwares.SoftwareXSD // is in the main application
library::Softwares.SoftwareXSD // is in the library

However, I still recommend that you choose unique names for your classes.

+9

:

using XSD = Softwares.SoftwareXSD;

:

XSD.SomeClass.SomeLibraryCall();
+3

If the compiler complains about an "unambiguous reference" because you have two namespaces with the same class name, and you have use of operators for both namespaces (in your case), you can get away with global.

ex: using LegacySoftwareXSD = global::LegacySoftwares.Softwares.SoftwareXSD;

+1
source

All Articles