编译器生成错误:调用不明确以下方法或属性之间 [英] compiler build error : The call is ambiguous between the following methods or properties

查看:228
本文介绍了编译器生成错误:调用不明确以下方法或属性之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的编译器错误与扩展方法。我有喜欢的扩展方法组装

I am experiencing a strange compiler error with extension methods. I have an assembly which has an extension method like

public static class MyClass
{
    public static Bar GetBar(this Foo foo)
    {
        return new Bar();
    }
}

和在同一个组件在其他地方我做这样的事

And elsewhere in the same assembly i do something like this

Foo foo = new Foo();
var bar = foo.GetBar();

当我清理和编译一切正常。但一旦我做出一个小的变化(如额外的空格)在装配和再建我得到这样一个错误:

When i clean and compile everything is OK. BUT once i make a small change (like an extra whitespace) in the assembly and build again I get an error like this:

错误973的调用不明确以下方法或属性之间:MyNameSpace.MyClass.GetBar(富)'和'MyNameSpace.MyClass.GetBar(美孚)

Error 973 The call is ambiguous between the following methods or properties: 'MyNameSpace.MyClass.GetBar(Foo)' and 'MyNameSpace.MyClass.GetBar(Foo)'

后,才清理项目,我还可以再建。这是在使用旧版本的程序集的编译器有问题?只有解决我现在看到的是正常的静态方法取代我的扩展方法。

Only after i clean the project I can build again. Is this a problem in the compiler using an old version of the assembly? Only work around I see now is to replace my extension methods with normal static methods.

推荐答案

确定已经做了一些玩一轮,我可以通过将文件引用到输出文件复制类似的情况,以本 - 第一次圆它建立successflly (作为参考,不使用 - 你只是得到一个引用未解决的警告),不过从这点上我看到的调用不明确,在code编辑错误出现

Ok having done a bit of playing round I can reproduce a similar situation to this by adding a file reference to the output file - the first time round it builds successflly (as the reference is not used - you simply get a "reference not resolved" warning), however from this point on I see the "The call is ambiguous" error appear in the code editor.

对于我来说不过这并从构建解决方案并不prevent我(我测试这个使用Visual Studio 2010),但误差确实出现 - 在性能稍微不同的情况下,也许,如不同的Visual Studio版本,这会从编译停止该项目。

For me however this doesn't prevent me from building the solution (I'm testing this using Visual Studio 2010), however the error does appear - maybe under slighly different circumstances, such as a different Visual Studio version, this would stop the project from compiling.

您也可以工程师与复制输出组件生成后的步骤同样的情况。

You could also engineer this same situation with post-build steps that copy the output assembly.

这篇关于编译器生成错误:调用不明确以下方法或属性之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆