如果同一命名空间中的两个类包含在 C# 中的两个不同库中该怎么办
我有一个程序(由遗留代码组成的主应用程序)使用一个库。遗憾的是,主应用程序和库都使用名为 Softwares.SoftwareXSD
的类(具有相同的名称和相同的属性)。当我使用 Softwares.SoftwareXSD
中定义的类时,主程序抱怨不明确。然而,Visual Studio 表示,它足够智能,可以通过在类名称下显示绿色下划线来在重复项中进行选择。我认为这不是一个好方法。
这有什么问题吗?对于这种情况,最好的解决方法是什么?
问题是 XSD 中的某些类特定于主应用程序,有些类特定于库,但这些类使用引用相互链接。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解你的问题正确的话,你在不同的程序集(主应用程序和库)中有两个类
Softwares.SoftwareXSD
,它们的完全限定名称是相同的。要解决此问题,请转到 Visual Studio 中的解决方案资源管理器,展开“引用”,右键单击对库的引用并选择属性。
在“别名”中,将“global”替换为其他别名,例如“library”。
您现在可以按如下方式消除引用的歧义:
尽管如此,我仍然建议您为您的类选择唯一的名称。
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:
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:
then later:
如果编译器抱怨“明确的引用”,因为您有两个具有相同类名的命名空间,并且您碰巧对这两个命名空间都有 using 语句(在您的情况下),您可以逃脱
global
关键字。例如:
using LegacySoftwareXSD = global::LegacySoftwares.Softwares.SoftwareXSD;
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;