c# - 好奇的依赖解析错误

标签 c# compiler-errors

我最近遇到了一个依赖项解析错误,我希望这里有人能解释一下。

我在第 3 方程序集 (I3rdParty) 中定义了一个接口(interface),一个依赖于该程序集的“通用”程序集和一个依赖于“通用”程序集的“客户端”库。 我们称它们为 3rdparty.dll、common.dll 和 client.dll。

client.dll 不应依赖于 3rdparty.dll。

在 common.dll 中定义了以下内容:

public static class Factory
{
    public static object Create(I3rdParty ifc) { ... }
    public static object Create(string value1, string value2, long? value3 = null) { ... }
}

从 client.dll 中使用了一个工厂方法,例如:

var instance = Factory.Create("SomeValue", "SomeValue2");

此时一切都按预期进行。

然后在common.dll中的第一个工厂方法中引入了一个bool参数,于是就变成了:

public static object Create(I3rdParty ifc, bool value) { ... }

由于缺少对 3rdparty.dll 的依赖,client.dll 的构建开始失败,例如:

The type 'I3rdParty' is defined in an assembly that is not referenced...

我假设这与这些方法现在接受相同数量的参数有关(因为第二个 Create 方法的第三个参数默认为 null)。

但我认为它仍然可以根据参数的类型选择正确的Create方法。谁能解释我所看到的行为的原因?

最佳答案

bool 参数添加到第一个重载后,编译器现在必须检查两个可能的方法签名,以选择应该使用的那个(这是过载决议)。

您正在调用 Create(string, string)

使用两个参数,您可以使用以下重载:

Create(I3rdParty, bool)
Create(string, string)

显然只有第二个可以匹配(因为 string 不能隐式转换为第二个参数的 bool),但编译器似乎不够聪明并且必须知道 I3rdParty 到底是什么(这意味着它需要引用定义它的程序集),然后才能确定 (I3rdParty, bool) 重载不是一个选项。

关于c# - 好奇的依赖解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797357/

相关文章:

c - ':' token 之前应为 ',' 、 ';' 、 '}' 、 '__attribute__' 或 '='

c# - 按钮.PerformClick();不在表单加载中工作

c# - Thread.Join 在 UI 线程中也阻塞子线程

c# - 选项卡高度不反射(reflect)自定义/用户绘画 TabControl 上的高 DPI

python - python 3作业出错

c++ - 将迭代器中的数据插入列表的问题

scala - 在 Play Route 的文件中,如何指示使用来自模块的路由?

ios - dyld : Library not loaded: @rpath/Alamofire. 设备 iOS 9 上的框架/Alamofire 错误,无法编译

c# - 如何查找用户是否是第一次登录 - C#

c# - IEnumerable<T> 和 .Where Linq 方法行为?