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

未关闭
#1,302 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 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
平均合并
2 天 15 小时
30 天内合并 PR
9

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

icsharpcode/CodeConverter 的其他 Issue

查看 icsharpcode/CodeConverter 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。