簡體   English   中英

無法從使用中推斷出打開泛型類型參數

[英]Open generic type arguments cannot be inferred from the usage

在這個問題中,當提到編譯器時,我實際上指的是Roslyn編譯器。 使用IntelliSense時出現問題, IntelliSense被認為是相同的編譯器。

出於演示目的和完整性,使用以下類( 使用Visual Studio 2015與C#6.0和.NET 4.6.1 ):

public class A
{
    public IEnumerable<B> B { get; set; }
}
public class B
{
    public IEnumerable<C> C { get; set; }
}
public class C { }
public class Helper<T> { }

請看以下擴展方法:

public static void FooBar<T1, T2>(
    this Helper<IEnumerable<T1>> helper,
    Expression<Func<T1, IEnumerable<T2>>> expression) { ... }

編譯器能夠在消耗時推斷它,如下所示:

Helper<IEnumerable<B>> helper = ...;
helper.FooBar(l => l.C); //T1 is B and T2 is C

還有這個重載的擴展方法:

public static void FooBar<T1, T2, T3>(
    this Helper<T1> helper,
    Expression<Func<T1, IEnumerable<T2>>> expression1,
    Expression<Func<T2, IEnumerable<T3>>> expression2) { ... }

編譯器不能推斷T1像這樣,只要當它:

Helper<A> helper = ...;
helper.FooBar(l => l. //compiler/IntelliSense cannot infer that T1 is A

這個截圖示例將描述更多我無法推斷的含義:
在此輸入圖像描述

當我用鼠標懸停在擴展方法上時,我收到此錯誤消息( 我已經分別用[]替換了<>字符,因為StackOverflow無法格式化報價中的那些 ):

方法'FooBar [T1,T2](此Helper [IEnumerable [T1]],Expression [Func [T1,IEnumerable [T2]]])'的類型參數不能從用法中推斷出來。 嘗試顯式指定類型參數。

但是當像這樣手動完成它時:

helper.FooBar(l => l.B, l => l.C); //compiler infers that T1 is A, T2 is B and T3 is C

編譯器很高興。

為什么編譯器/ IntelliSense( 或Visual Studio的自動完成功能 )無法找出T1並希望我在開始輸入時明確指定類型參數?

請注意 ,如果我在示例中省略了IEnumerable<> ,編譯器可以在鍵入時愉快地推斷出所有內容。
手動輸入l => lB后,編譯器也很高興。 然后它知道T1A ,所以你可以表達智能感知的幫助下,最后一個參數。

如果我理解正確,在VS2013中一切都按預期工作:

你的第一個案例:

你的第二個案例:

我開始打字了l. 和IntelliSense告訴我l有一個可以使用的屬性B 所以,如果我是對的,它在VS2013 正確地推斷出 ,而不會VS2015 推斷 ,那么它肯定在VS2015的智能感知的錯誤可以報告給微軟。

我發現你的問題很有趣,因為我正在使用VS 2015,我想我也可以嘗試一下。 我得到了和你一樣的錯誤,所以我猜它可能是一個VS bug,因為在其他版本中它運行正常。

這是我的錯誤:

在此輸入圖像描述

我也對CTRL + SPACE 沒有任何建議

編輯:

這里可以看到類似的錯誤。

所以,因為在舊版本的VS中,這是有效的,所以這就是它被認為是一個bug的原因。

正如我所說,我有同樣的問題。 是錯誤報告)
它與Expressions或IEnumerable無關。
這是一個簡化的例子

using System;

namespace ConsoleApplicationExpressionTree
{

    public static class Extentions
    {
        static void Main() { }
        static void AMethod(string[] args)
        {
            SetValue(new Customer(), e => e.Name   /*type here */, "TheName");

        }

        public static void SetValue<TEntity, TProperty>(TEntity instance, Func<TEntity, TProperty> expression, TProperty newValue)
        {

        }
    }
    public class Customer
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

我剛剛注意到,如果從方法中刪除第三個參數,它就可以了!

using System;

namespace ConsoleApplicationExpressionTree
{

    public static class Extentions
    {
        static void Main() { }
        static void AMethod(string[] args)
        {
            SetValue(new Customer(), e => e.Name   /*type here */);

        }

        public static void SetValue<TEntity, TProperty>(TEntity instance, Func<TEntity, TProperty> expression)
        {

        }
    }
    public class Customer
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

因此,如果您非常幸運,您可以通過調整參數來修復當前示例。
如果沒有,你只需要等待MS修復它......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM