icsharpcode / CodeConverter

Convert code from C# to VB.NET and vice versa using Roslyn
https://icsharpcode.github.io/CodeConverter/
MIT License
826 stars 216 forks source link

VB -> C#: Object initializers referencing each other cause conversion error #1080

Open GrahamTheCoder opened 7 months ago

GrahamTheCoder commented 7 months ago

VB.Net input code

Imports System.Drawing

Public Class VisualBasicClass
    Public Sub M()
        Dim Shape = New Point() With {
        .X = 1,
        .Y = .X
        }
    End Sub
End Class

Erroneous output

using System.Drawing;

public partial class VisualBasicClass
{
    public void M()
    {
        Point @init = new Point();
        var Shape = (@init.X = 1, @init.Y = default
#error Cannot convert MemberAccessExpressionSyntax - see comment for details
  /* Cannot convert MemberAccessExpressionSyntax, System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
      at System.ThrowHelper.ThrowKeyNotFoundException()
      at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<VisitMemberAccessExpression>d__53.MoveNext() in C:\Users\gph77\source\repos\CodeConverter\CodeConverter\CSharp\ExpressionNodeVisitor.cs:line 435
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
      at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper.<ConvertHandledAsync>d__12`1.MoveNext() in C:\Users\gph77\source\repos\CodeConverter\CodeConverter\CSharp\CommentConvertingVisitorWrapper.cs:line 40

   Input:
   .X

    */, @init).@init;
    }
}System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<VisitMemberAccessExpression>d__53.MoveNext() in C:\Users\gph77\source\repos\CodeConverter\CodeConverter\CSharp\ExpressionNodeVisitor.cs:line 435
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper.<ConvertHandledAsync>d__12`1.MoveNext() in C:\Users\gph77\source\repos\CodeConverter\CodeConverter\CSharp\CommentConvertingVisitorWrapper.cs:line 40

Expected output

using System.Drawing;

public partial class VisualBasicClass
{
    public void M()
    {
        Point @init = new Point();
        var Shape = (@init.X = 1, @init.Y = @init.X, @init).@init;
    }
}

Details

This is due to only handling one of the types of collection initializers