mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
VBPretty had no coverage of VB's anonymous types, so nothing caught that their use sites decompiled to the raw metadata names while their definitions were hidden from the output. The expected C# is written as it should read once the generated-name predicates agree with each other; it fails until then. Roslyn 2.10 targeting .NET Core 2.2 is branched off with #if: there the query operator calls are not restored to extension-method syntax, so no query expression is formed and the lowered form survives. Assisted-by: Claude:claude-fable-5:Claude Codepull/3964/head
4 changed files with 159 additions and 0 deletions
@ -0,0 +1,89 @@
@@ -0,0 +1,89 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Runtime.CompilerServices; |
||||
|
||||
using Microsoft.VisualBasic.CompilerServices; |
||||
|
||||
[StandardModule] |
||||
public sealed class VBAnonymousTypes |
||||
{ |
||||
public static void MutableAnonymousType() |
||||
{ |
||||
var anon = new { |
||||
Value = 1, |
||||
Name = "test" |
||||
}; |
||||
Console.WriteLine(anon.Value); |
||||
Console.WriteLine(anon.Name); |
||||
} |
||||
|
||||
public static void KeyAnonymousType() |
||||
{ |
||||
var anon = new { |
||||
Value = 1, |
||||
Name = "test" |
||||
}; |
||||
Console.WriteLine(anon.Value); |
||||
Console.WriteLine(anon.Name); |
||||
} |
||||
|
||||
public static void AnonymousTypeAsArgument() |
||||
{ |
||||
Console.WriteLine(new { |
||||
Value = 1, |
||||
Name = "test" |
||||
}.ToString()); |
||||
} |
||||
|
||||
public static void SelectAnonymousType(IEnumerable<int> items) |
||||
{ |
||||
var enumerable = items.Select([SpecialName] (int i) => new { |
||||
Value = i, |
||||
Square = checked(i * i) |
||||
}); |
||||
foreach (var item in enumerable) |
||||
{ |
||||
Console.WriteLine(item.Value); |
||||
Console.WriteLine(item.Square); |
||||
} |
||||
} |
||||
|
||||
public static void LetWhereSelect(IEnumerable<int> items) |
||||
{ |
||||
var enumerable = from i in items |
||||
let square = checked(i * i) |
||||
where square > 4 |
||||
select new { i, square }; |
||||
foreach (var item in enumerable) |
||||
{ |
||||
Console.WriteLine(item.i); |
||||
Console.WriteLine(item.square); |
||||
} |
||||
} |
||||
|
||||
public static void JoinSelect(IEnumerable<int> left, IEnumerable<int> right) |
||||
{ |
||||
var enumerable = from x in left |
||||
join y in right on x equals y |
||||
select new { x, y }; |
||||
foreach (var item in enumerable) |
||||
{ |
||||
Console.WriteLine(item.x); |
||||
Console.WriteLine(item.y); |
||||
} |
||||
} |
||||
|
||||
public static void OrderBySelect(IEnumerable<int> items) |
||||
{ |
||||
var enumerable = from i in items |
||||
let doubled = checked(i * 2) |
||||
orderby doubled |
||||
select new { i, doubled }; |
||||
foreach (var item in enumerable) |
||||
{ |
||||
Console.WriteLine(item.i); |
||||
Console.WriteLine(item.doubled); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
Imports System |
||||
Imports System.Collections.Generic |
||||
Imports System.Linq |
||||
|
||||
Public Module VBAnonymousTypes |
||||
Public Sub MutableAnonymousType() |
||||
Dim value = New With {.Value = 1, .Name = "test"} |
||||
Console.WriteLine(value.Value) |
||||
Console.WriteLine(value.Name) |
||||
End Sub |
||||
|
||||
Public Sub KeyAnonymousType() |
||||
Dim value = New With {Key .Value = 1, Key .Name = "test"} |
||||
Console.WriteLine(value.Value) |
||||
Console.WriteLine(value.Name) |
||||
End Sub |
||||
|
||||
Public Sub AnonymousTypeAsArgument() |
||||
Console.WriteLine(New With {Key .Value = 1, Key .Name = "test"}.ToString()) |
||||
End Sub |
||||
|
||||
Public Sub SelectAnonymousType(items As IEnumerable(Of Integer)) |
||||
Dim query = From i In items |
||||
Select New With {Key .Value = i, Key .Square = i * i} |
||||
For Each item In query |
||||
Console.WriteLine(item.Value) |
||||
Console.WriteLine(item.Square) |
||||
Next |
||||
End Sub |
||||
|
||||
Public Sub LetWhereSelect(items As IEnumerable(Of Integer)) |
||||
Dim query = From i In items |
||||
Let square = i * i |
||||
Where square > 4 |
||||
Select i, square |
||||
For Each item In query |
||||
Console.WriteLine(item.i) |
||||
Console.WriteLine(item.square) |
||||
Next |
||||
End Sub |
||||
|
||||
Public Sub JoinSelect(left As IEnumerable(Of Integer), right As IEnumerable(Of Integer)) |
||||
Dim query = From x In left |
||||
Join y In right On x Equals y |
||||
Select x, y |
||||
For Each item In query |
||||
Console.WriteLine(item.x) |
||||
Console.WriteLine(item.y) |
||||
Next |
||||
End Sub |
||||
|
||||
Public Sub OrderBySelect(items As IEnumerable(Of Integer)) |
||||
Dim query = From i In items |
||||
Let doubled = i * 2 |
||||
Order By doubled |
||||
Select i, doubled |
||||
For Each item In query |
||||
Console.WriteLine(item.i) |
||||
Console.WriteLine(item.doubled) |
||||
Next |
||||
End Sub |
||||
End Module |
||||
Loading…
Reference in new issue