次のメソッドシグネチャがあると、コンパイラがその型を自動的に推論できないというパラメータが明示的に指定されているのはなぜですか? Visual Studio 2010 SP1では、型を推測でき、警告やエラーが表示されません。 CannotInferType
に説明し、それをコンパイルしようとすると、コンパイラは予想通りCanInferType
作品で説明したように、それを呼び出すのに対し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.
を発するようにそれを呼び出すオプションのパラメータを持つジェネリック型を推論することができない
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);
}
。
上記のように、Visual Studio自体は問題はないと報告しています。変数a
のインテリセンスは、期待どおりにIEnumerable<SomeClass>
と表示されますが、何らかの理由でコンパイルされません。
短くはありますが、完全な例を挙げると助かりました。 –