logeeks
logeeks

Reputation: 4979

What to do if two classes in same namespace gets included in two different libraries in C#

I have a program (Main application which consists of legacy code ) which consumes a library. Sadly, both main application and the library uses a classes (with same name and same properties) called Softwares.SoftwareXSD. When I use the class defined inside Softwares.SoftwareXSD, the main program complains about ambiguity. However, Visual Studio is saying that it is smart enough to choose within the duplicates by displaying a green underline under the class name. I believe this is not a good approach.

Is there any any problem with this? What is the best workaround for this situation?

The problem is that some classes in the XSD is specific to main application and some is specific to the library but these classes are linked with each other using references.

Upvotes: 4

Views: 2585

Answers (3)

to StackOverflow
to StackOverflow

Reputation: 124794

If I understand your question right, you have two classes Softwares.SoftwareXSD in different assemblies (main application and library) whose fully-qualified name is identical.

To resolve this, go to Solution Explorer in Visual Studio, expand "References", right click on the reference to your library and select properties.

In "Aliases" replace "global" by some other alias, e.g. "library".

You can now disambiguate the references as follows:

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

Nevertheless, I'd still recommend you to choose unique names for your classes.

Upvotes: 9

Vijay Sirigiri
Vijay Sirigiri

Reputation: 4711

If the compiler is complaining about "disambiguous reference" because you have two namespaces with same class name and you happen to have using statements for both the namespaces (in your case) you can get away with global keyword.

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

Upvotes: 1

Andrew Cooper
Andrew Cooper

Reputation: 32596

You can use an alias for the library namespace to disambiguate the members:

using XSD = Softwares.SoftwareXSD;

then later:

XSD.SomeClass.SomeLibraryCall();

Upvotes: 3

Related Questions