有与此类似的问题 - 但到目前为止都没有帮助。
我在打电话:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
然后在我的代码的一部分中,VS 指出了 LINQ 方法(.Last() 和 .Max())中的错误。我将鼠标悬停在 .Max() 上。错误说:
The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Max(System.Collections.Generic.IEnumerable<int>)' and 'System.Linq.Enumerable.Max(System.Collections.Generic.IEnumerable<int>)'
我已经尝试重新安装所有引用资料和软件包,我重新启动了 VS、IIS(这是一个网站)。 VS 识别 System.Linq,所以这不是问题....
我不明白为什么这是突然的抛出错误。
最佳答案
如果您的代码中的任何地方或引用的程序集中有人有实现他们自己的 IEnumerable<T>
的可怕想法,就会发生这种情况。扩展方法并且不知道更好,使用与框架提供的 namespace 相同的 namespace :
Alpha 组件自己滚动
IEnumerable<T>
System.Linq
中的扩展方法命名空间:namespace System.Linq { public static class MyEnumerable { public static T Max<T>(this IEnumerable<T> source) { //... } //... } }
程序集 Charlie 认为 Alpha 很棒,因为它提供了一些其他功能,并且引用它时完全没有注意到它隐藏在里面的令人讨厌的惊喜。
using System.Linq; //woops namespace Charlie { class C { void Foo() { var l = new List<object>() { 1, 2, 3 }; var m = l.Max() /*compile time error. Ambiguous call*/ } } }
我不知道这是否会成为您的情况,但这是一种可能的情况。另一个潜在的候选者可能是一些版本控制冲突,但我不确定这在 VS 中是否可能。我从来没有遇到过与 System.*
类似的事情命名空间。
关于C# Linq System.Linq 和 System.Collections 之间的模糊调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54045748/