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#: unnecessary casts to object when late binding is involved cause RuntimeBinderException #1071

Closed TymurGubayev closed 8 months ago

TymurGubayev commented 9 months ago

VB.Net input code

Option Strict Off

Public Class TestDynamicUsage
    Property Prop As Integer

    Sub S()
        Dim o As Object
        o = New TestDynamicUsage
        o.Prop = 1
    End Sub
End Class

Erroneous output

public class TestDynamicUsage
{
    public int Prop { get; set; }

    public void S()
    {
        object o;
        o = new TestDynamicUsage();
        ((dynamic)o).Prop = (object)1; //throws RuntimeBinderException: "Cannot implicitly convert type 'object' to 'int'" at run time
    }
}

Expected output

    public class TestDynamicUsage
    {
        public int Prop { get; set; }

        public void S()
        {
            object o;
            o = new TestDynamicUsage();
            ((dynamic)o).Prop = 1;
        }
    }

Details