VB -> C#: Generic extension method inference widens element types when using a base-type comparer
維護者通常 4 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
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.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
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
icsharpcode/CodeConverter 的其他 Issue
-
VB -> C#
難度 2/5 1-3 小時 新手友好度 72/100
icsharpcode/CodeConverter#1301 ·
維護者通常 4 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
icsharpcode/CodeConverter#1271 · 1 則留言 ·
維護者通常 4 天內回覆
-
VB -> C#
難度 3/5 1-2 天 新手友好度 68/100
icsharpcode/CodeConverter#1300 ·
維護者通常 4 天內回覆
-
VB -> C#
難度 3/5 1-2 天 新手友好度 62/100
icsharpcode/CodeConverter#1299 ·
維護者通常 4 天內回覆
-
VB -> C#
難度 3/5 1-2 天 新手友好度 62/100
icsharpcode/CodeConverter#1298 ·
維護者通常 4 天內回覆
查看 icsharpcode/CodeConverter 的全部 Issue
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 80/100
Unity-Technologies/unity-agent-plugin#118 ·
維護者通常 1 天內回覆
-
The Recommended preset turns on Import Uno DSP, which only works with the Material theme可能已有人在做 @agneszitte 今天認領。 未關閉
難度 1/5 1 小時以內 新手友好度 92/100
unoplatform/uno.templates#2388 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 77/100
eyalgolan/statecraft-israel#2732 ·
維護者通常 1 天內回覆
-
要トリアージ
難度 2/5 1-3 小時 新手友好度 64/100
維護者通常 1 天內回覆
-
kind/chore status/draft
難度 2/5 1-3 小時 新手友好度 65/100
Altinn/altinn-events#1158 ·
維護者通常 1 天內回覆