简体   繁体   English

无法使用可选参数推断泛型类型

[英]Unable to infer generic type with optional parameters

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. Visual Studio 2010 SP1能够推断类型并且不显示警告或错误。

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. 按照在CannotInferType中的描述调用它,当尝试编译它时编译器发出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. 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. CanInferType描述的调用它按预期工作。

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. 如上所述,Visual Studio本身报告没有问题,并且变量a intellisense显示IEnumerable<SomeClass>按预期,但由于某种原因它不编译。

It was a bug in the C# 4 compiler. 这是C#4编译器中的一个错误。 It's been fixed in the C# 5 compiler. 它已在C#5编译器中修复。

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: 这是我在向Eric和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. 令人高兴的是,这现在适用于C#5 beta编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM