问题描述
(请随意为这个问题提出更准确的标题.)
(Please feel free to suggest a more accurate title to this question.)
在我的 Visual Studio 2015 解决方案中,我有三个项目(我们称它们为 Alpha、Beta 和 Gamma),它们或多或少是相同的,但不同之处在于它们定义了不同的后端.这两个项目都将一个类热插入到同一个命名空间中:
In my Visual Studio 2015 solution, I have three projects (let's call them Alpha, Beta, and Gamma) that are more or less the same thing, but differ in that they define different backends. Both of these projects hot-plug a class into the same namespace:
阿尔法:
namespace SharedNamespace {
public class SharedClass {
// implement SharedClass using Alpha's backend
}
}
测试版:
namespace SharedNamespace {
public class SharedClass {
// implement SharedClass using Beta's backend
}
}
伽玛:
namespace SharedNamespace {
public class SharedClass {
// implement SharedClass using Gamma's backend
}
}
有几个项目使用这个热插拔类,每个项目都引用 Alpha、Beta 或 Gamma.其中之一(我们称之为 Omricon)曾经引用 Alpha,但现在引用 Gamma:
Several projects use this hot-plugged class, each referencing either Alpha, Beta, or Gamma. One of them (let's call it Omricon) used to reference Alpha, but now references Gamma:
// ...
SharedNamespace.SharedClass sharedClass;
sharedClass.DoThing();
// ...
然而,当我尝试构建 Omricon 时,C# 编译器给出错误 CS0433:
When I attempt to build Omricon, however, the C# compiler gives error CS0433:
The type 'SharedClass' exists in both 'Alpha, Version=0.0.0.0 (etc)'
and 'Gamma, Version=0.0.0.0 (etc)'
然而,Omricon 仅在构建时引用 Gamma - 当我进入项目引用列表时,只出现对 Gamma 的引用.据我了解,Omricon 应该对 Alpha 一无所知一无所知,更不用说它在同一位置定义了一个类.只有 Omricon 无法构建 - 其他使用 Alpha 和 Beta 的项目工作正常,当我将 Omricon 切换回使用 Alpha 时,它也工作正常!
However, Omricon only references Gamma when it is built - when I go into the project references list, only the reference to Gamma appears. As far as I understand it, Omricon should know nothing about Alpha at all, much less that it defines a class in the same location. Only Omricon fails to build - other projects that use Alpha and Beta work fine, and when I switch Omricon back to using Alpha, it works fine as well!
在我看来,对 Alpha 的引用正在其他地方维护.如何在我的代码中找到对 Alpha 的杂散引用,并将其删除?
It appears to me that a reference to Alpha is being maintained, then, somewhere else. How can I find the stray reference to Alpha, wherever it lies in my code, and remove it?
请注意,我已尝试强制完全重建(如 此答案 建议的那样),但仍然出现相同的错误,所以它与坏对象缓存无关.
Note that I have tried forcing a full rebuild (as this answer suggested), and the same error still appears, so it has nothing to do with bad object caching.
澄清倒数第二段
推荐答案
首先,您可能已经意识到:这是一个糟糕的情况.如果您可以避免在引用它们的两个不同程序集中的同一个命名空间中使用同一个命名类,请避免这种情况.它强烈表明您的应用程序中存在架构缺陷.在我看来,您应该在第四个程序集中定义一个接口,然后让所有程序集都同意使用该接口.
First off, as you probably have realized: this is a terrible situation to be in. If you can possibly avoid having the same named class in the same named namespace in two different assemblies that you reference both of them, avoid that situation. It is strongly indicative of an architectural flaw in your application. It sounds to me like you should be defining an interface in yet a fourth assembly, and then have all your assemblies agree to use that interface.
但是,有一种方法可以在 C# 中处理这种情况.
However, there is a way to deal with this situation in C#.
编译 Omicron 时,应该给 Alpha.dll、Beta.dll 和 Gamma.dll 一个引用别名:
When you compile Omicron, you should give Alpha.dll, Beta.dll and Gamma.dll a reference alias:
/reference:AlphaDLL=Alpha.DLL /reference:BetaDLL=Beta.DLL ... etc
然后在 Omicron 内部你说:
then inside Omicron you say:
extern alias AlphaDLL;
extern alias BetaDLL;
extern alias GammaDLL;
在一个文件中,然后在该文件中你可以说:
In a file, and then later in that file you can say:
AlphaDLL::SharedNamespace.SharedClass
为了消除歧义.
但同样,不要陷入这种情况.而是创建一个 SharedClass 实现的接口,并让 Alpha、Beta 和 Gamma 实现都使用名称不冲突的类来实现该接口.
But again, do not get into this situation. Instead make an interface that SharedClass implements, and have the Alpha, Beta and Gamma implementations all implement that interface with a class whose name does not conflict.
这篇关于如何诊断和删除 C# 中的歧义引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!