问题
Given the following method signature, why is it when a parameter is explicitly named the compiler is unable to automatically infer the type? Visual Studio 2010 SP1 is able to infer the type and shows no warnings or errors.
IEnumerable<T> ExecuteCommand<T>(
string commandText,
string connectionName = null,
Func<IDataRecord, T> converter = null) { ... }
static SomeClass Create(IDataRecord record) { return new SomeClass(); }
void CannotInferType() {
var a = ExecuteCommand(
"SELECT blah",
"connection",
converter: Test.Create);
}
void CanInferType() {
var a = ExecuteCommand(
"SELECT blah",
"connection",
Test.Create);
}
Calling it as described in CannotInferType
and when attempting to compile it the compiler emits error CS0411: The type arguments for method 'Test.ExecuteCommand<T>(string, string, System.Func<System.Data.IDataRecord,T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Whereas calling it as described in CanInferType
works as expected.
As stated above, Visual Studio itself reports no problems, and intellisense for the variable a
shows IEnumerable<SomeClass>
as expected but for some reason it doesn't compile.
回答1:
It was a bug in the C# 4 compiler. It's been fixed in the C# 5 compiler.
I suspect it's not the optional parameter which is causing the problem here - it's the named argument. Try removing the default values for your parameters and I suspect you'll still have the same problem. (It's worth differentiating between optional parameters and named arguments - they're two separate features. They're often used together, but certainly don't have to be.)
That's the conclusion I came to when I sent this bug report to Eric and Mads:
using System;
class Test
{
static void Foo<T>(Func<T> func) {}
static void Main()
{
// Works fine
Foo(() => "hello");
// Type inference fails
Foo(func: () => "hello");
}
}
Happily this now works in the C# 5 beta compiler.
来源:https://stackoverflow.com/questions/9898479/unable-to-infer-generic-type-with-optional-parameters