开发者

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.


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.


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

using XSD = Softwares.SoftwareXSD;

then later:

XSD.SomeClass.SomeLibraryCall();


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;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜