当前位置: 代码迷 >> C# >> 一个Linq的编译异常,帮小弟我看一下
  详细解决方案

一个Linq的编译异常,帮小弟我看一下

热度:73   发布时间:2016-05-05 03:20:45.0
一个Linq的编译错误,帮我看一下

typeof (Type).GetMethods().Select(x=>Console.WriteLine(x));

编译错误显示
Error 1 The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我是想循环打印GetMethods()的结果,不想自己写个for循环,因此尝试了一下Linq。这个错误是什么含义,改如何改呢?


------解决思路----------------------
Console.WriteLine(x)返回类型是void
Select()无法推断此类型
typeof (Type).GetMethods().ToList().ForEach(x=>Console.WriteLine(x));
------解决思路----------------------
推断不出  参数类型,还是用循环吧
------解决思路----------------------
List<T>有ForEach方法,可以先转成List<MethodInfo>。

typeof(Type).GetMethods().ToList().ForEach(x => Console.WriteLine(x));
  相关解决方案