我不认为我做了太深奥的任何事情,但我没有看到任何其他的问题。我有一个最大公分母(“A类”),它是一个最大的公分母(“A类”),它是一个最大公约号(“A”),它是一个最大公约号也在方法“Frob”的返回类型中明确定义。编译器不应该在lambda表达式中创建所有返回类型的列表,创建一个祖先树以找到它们的共同祖先,然后将其与包含方法的预期返回类型进行协调?C#类型参数不能从select中的用法推断多个返回
方法'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable,System.Func)'的类型参数不能从用法中推断出来。尝试明确指定类型参数。
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Sample
{
public abstract class A
{
private A(int index) { /* ... */ }
public sealed class A1 : A
{
public A1(string text, int index)
: base(index)
{ /* ... */ }
}
public sealed class A2 : A
{
public A2(int index)
: base(index)
{ /* ... */ }
}
private static Regex _regex = new Regex(@"(to be)|(not to be)");
public static IEnumerable<A> Frob(string frobbable)
{
return _regex.Matches(frobbable)
.Cast<Match>()
.Select((match, i) =>
{
if (match.Groups[1].Success)
{
return new A1(match.Groups[1].Value, i);
}
else
{
return new A2(i);
}
});
}
}
}
你有没有尝试在返回之前将A1和A2投射到A?但是,是的,我可以看到为什么编译器会抱怨,它不会奇迹般地知道您希望共同分母是A级。 – 2012-07-20 22:08:00