VB -> C#: Generic extension method inference widens element types when using a base-type comparer

未關閉
#1,302 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 4 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
55/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
csharp
領域
devtools, tooling

研究方向

Start from the VB-to-C# conversion path for generic extension-method calls and reproduce the issue with the VB input in the report. Compare the generated Distinct call with the expected output; done means it specifies DerivedValue and preserves the declared return type.

由索引模型根據 Issue 內容生成。

描述

VB -> C#
VB.Net input code
Imports System.Collections.Generic
Imports System.Linq

Public Class BaseValue
    Public Property Key As Integer
End Class

Public Class DerivedValue
    Inherits BaseValue
End Class

Public Module Helpers
    Public Function ToDictionary(values As IEnumerable(Of DerivedValue)) As Dictionary(Of Integer, DerivedValue)
        Return values.Distinct(EqualityComparer(Of BaseValue).Default).ToDictionary(Function(value) value.Key)
    End Function
End Module
Erroneous output

The converted method leaves the generic arguments of Distinct implicit:

public static Dictionary<int, DerivedValue> ToDictionary(IEnumerable<DerivedValue> values)
{
    return values.Distinct(EqualityComparer<BaseValue>.Default).ToDictionary(value => value.Key);
}

C# infers BaseValue as the element type of Distinct, so ToDictionary returns Dictionary<int, BaseValue>.

This produces:

CS0029: Cannot implicitly convert type
'System.Collections.Generic.Dictionary<int, BaseValue>'
to 'System.Collections.Generic.Dictionary<int, DerivedValue>'
Expected output

The converter should preserve the type argument inferred by VB.NET:

public static Dictionary<int, DerivedValue> ToDictionary(IEnumerable<DerivedValue> values)
{
    return values.Distinct<DerivedValue>(EqualityComparer<BaseValue>.Default).ToDictionary(value => value.Key);
}
Details
  • Product: Visual Studio extension, built locally from master.
  • Version: 10.0.1 (local development build).
  • Previous working version: No.

VB.NET and C# use different type inference rules for generic extension methods.

VB.NET first infers the relevant type parameters from the receiver. In this example, it fixes the element type as DerivedValue. The base-type comparer is then accepted through the contravariance of IEqualityComparer(Of T).

C# inference considers both the receiver and the comparer, resulting in BaseValue instead. Omitting explicit type arguments therefore changes the type of the resulting sequence.

Microsoft's VB team describes this receiver-first inference here:
https://devblogs.microsoft.com/vbteam/extension-methods-and-generics-extension-methods-part-5/

A workaround is to specify the type argument explicitly in the VB source:

Return values.Distinct(Of DerivedValue)(EqualityComparer(Of BaseValue).Default).ToDictionary(Function(value) value.Key)
主要語言
C#
星號
917
分支
243
平均合併
4 天 42 分鐘
30 天內合併 PR
11

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

icsharpcode/CodeConverter 的其他 Issue

查看 icsharpcode/CodeConverter 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。