icsharpcode / CodeConverter

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

VB -> C#: DefaultMemberAttribute marked properties are lost #1144

Open TymurGubayev opened 1 month ago

TymurGubayev commented 1 month ago

VB.Net input code

We need a C# project with a class like this:

[System.Reflection.DefaultMember(nameof(Caption))]
public class ClassWithReflectionDefaultMember
{
    public string Caption { get; set; }
}

Then in VB.NET, access its DefaultMember:

<System.Reflection.DefaultMember(NameOf(LoosingProperties.Caption))>
Public Class LoosingProperties
    Public Property Caption As String

    Sub S()
        Dim x = New LoosingProperties()
        x.Caption = "Hello"

        Dim y = New ClassWithReflectionDefaultMember() 'from C#
        y.Caption = "World"
    End Sub
End Class

Erroneous output

[System.Reflection.DefaultMember(nameof(Caption))]
public class LoosingProperties
{
    public string Caption { get; set; }

    public void S()
    {
        var x = new LoosingProperties();
        x.Caption = "Hello"; // works differently for equivalent VB.NET class

        var y = new ClassWithReflectionDefaultMember(); // from C#
        y = "World"; // <----- notice `.Caption` is missing here
    }
}

Expected output

[System.Reflection.DefaultMember(nameof(Caption))]
public class LoosingProperties
{
    public string Caption { get; set; }

    public void S()
    {
        var x = new LoosingProperties();
        x.Caption = "Hello";

        var y = new ClassWithReflectionDefaultMember(); // from C#
        y.Caption = "World";
    }
}

Details