在VS2010不明确引用错误 [英] Ambiguous reference error in VS2010

查看:1027
本文介绍了在VS2010不明确引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个在我们的库称为动作类型。我们支持VS2005,2008年和现在正试图以支持VS2010了。当我有包含我们的动作类型的命名空间,也'系统'一起在一个文件,并尝试使用它,它婴儿床说我们的操作类型和System.Action代表之间的暧昧引用调用。这仅发生在VS2010,并没有在VS2008或VS2005抛出错误,即使什么都没有用Action委托改变(命名空间,装配一切都是3.5和4.0之间的相同)。任何想法,为什么这可能是发生?

We have a type called Action in our library. We support VS2005, 2008 and now trying to support VS2010 too. When I include the namespace containing our 'Action' type and also 'System' together in a file and try to use it, it cribs saying ambiguous reference call between our Action type and System.Action delegate. This is happening only in VS2010 and it does not throw error in VS2008 or VS2005 even though nothing has changed with the Action delegate (namespace, assembly everything is the same between 3.5 and 4.0). Any idea why this could be happening?

更新:

我创建了一个简单的应用程序来重现这样的:

I created a simple application to reproduce this:

using System;
using System.Collections.Generic;
using System.Text;

namespace Namespace1
{
    public enum Action
    {
        Mouse,
        Keyboard
    }
}

Class2.cs:

using Namespace1;
using System;
using System.Collections.Generic;
using System.Text;

namespace Namespace2
{
    public class Class2
    {
        public Class2()
        {
            Action a;
        }
    }
}

以上code编译罚款在VS2008但在VS2010抛出错误:错误CS0104:'行动'是'Namespace1.Action'和'System.Action之间不明确的引用

The above code compiles fine in VS2008 but throws error in VS2010: "error CS0104: 'Action' is an ambiguous reference between 'Namespace1.Action' and 'System.Action'"

我得到同样的错误,当我使用VS2010和目标,而不是4.0框架3.5。

I get the same error when I use VS2010 and target Framework 3.5 instead of 4.0.

谢谢, NIRANJAN

Thanks, Niranjan

推荐答案

跳转到想到的问题是,为什么一点也不含糊在C#3(我认为这将是一个语言的错误)。

The question that jumps to mind is why it isn't ambiguous in C#3 (I think this would be a language error).

和快速测试显示,它是:

And a quick test shows it is:

using System;
using ConsoleApplication1;

namespace ConsoleApplication1 {
    class Action { };
    class Program {
        static void Main(string[] args) {
            Action a = new Action();
        }
    }
}

class P2 {
    public void foo() {
        Action a;
    }
}

在VS2008(C#3)出现错误:'行动'是'System.Action'和'ConsoleApplication1.Action'之间的暧昧参考中 P2.foo()

In VS2008 (C#3) get error: "'Action' is an ambiguous reference between 'System.Action' and 'ConsoleApplication1.Action'" in P2.foo().

有可能是在查找规则有关如何不同情况略有变化(如标识在当前的命名空间需要precedence(如类节目以上)。

There may be slight changes in lookup rules about how different cases (e.g. identifier in current namespace takes precedence (as in class Program above).

有两种选择,以消除歧义:

There are two options to disambiguate:

  1. 使用完全合格标识(在上面,无论是 System.Action ConsoleApplication1.Action
  2. 使用一个命名空间别名完全限定的标识符,没有这么多的打字:

  1. Use fully qualified identifiers (in the above, either System.Action or ConsoleApplication1.Action.
  2. Use a namespace alias to fully qualify the identifier without so much typing:

使用MyAction的= MyNamespace.Action;

using MyAction = MyNamespace.Action;

NB。 VS2010 / C#4(测试版2)给出了相同的结果与code以上为VS2008 / C#3。

NB. VS2010/C#4 (beta 2) gives the same result with the code above as VS2008/C#3.

这篇关于在VS2010不明确引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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