c# - C# Linq System.Linq 和 System.Collections 之间的模糊调用
问题描述
有类似的问题 - 但到目前为止,他们都没有帮助。
我打电话给:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
然后在我的一段代码中,VS 指出了 LINQ 方法(.Last() 和 .Max())中的错误。我将鼠标悬停在 .Max() 上。错误说:
以下方法或属性之间的调用不明确:'System.Linq.Enumerable.Max(System.Collections.Generic.IEnumerable<int>)' 和 'System.Linq.Enumerable.Max(System.Collections.Generic.IEnumerable<整数>)'
我已经尝试重新安装我所有的引用和包,我重新启动了 VS、IIS(这是一个网站)。VS 识别 System.Linq 所以这不是问题....
我不明白为什么这是突然的抛出错误。
解决方案
如果您的代码或引用的程序集中的任何地方有人有实现自己的IEnumerable<T>
扩展方法并且不知道更好的可怕想法,使用与框架提供的命名空间相同的命名空间,则可能会发生这种情况:
Alpha 程序集在命名空间中滚动它们自己的
IEnumerable<T>
扩展方法System.Linq
:namespace System.Linq { public static class MyEnumerable { public static T Max<T>(this IEnumerable<T> source) { //... } //... } }
Assembly 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.*
命名空间类似的事情。