CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: Properties not handled correctly for ref extension methods

Open gaschd opened this issue 1 year ago • 0 comments

When applying extension methods with ref parameters to properties, they should be handled in the same way as ref parameters in regular methods.

VB.Net input code

Public Class ExtensionMethodsRefPropertyParameter
    Public Property Number As Integer = 3
    Public Sub WithExtensionMethod()
        Number.NegEx()
    End Sub
    Public Sub WithMethod()
        Neg(Number)
    End Sub
End Class

Public Module MathEx
    <Extension()>
    Public Sub NegEx(ByRef num As Integer)
        num = -num
    End Sub
    Public Sub Neg(ByRef num As Integer)
        num = -num
    End Sub
End Module

Erroneous output

public partial class ExtensionMethodsRefPropertyParameter
{
    public int Number { get; set; } = 3;
    public void WithExtensionMethod()
    {
        this.Number.NegEx();
    }
    public void WithMethod()
    {
        int argnum = Number;
        MathEx.Neg(ref argnum);
        Number = argnum;
    }
}

public static partial class MathEx
{
    public static void NegEx(this ref int num)
    {
        num = -num;
    }
    public static void Neg(ref int num)
    {
        num = -num;
    }
}
1 target compilation errors:
CS0206: A property or indexer may not be passed as an out or ref parameter

Expected output

public partial class ExtensionMethodsRefPropertyParameter
{
    public int Number { get; set; } = 3;
    public void WithExtensionMethod()
    {
        int argnum = Number;
        argnum.NegEx();
        Number = argnum;
    }
    public void WithMethod()
    {
        int argnum = Number;
        MathEx.Neg(ref argnum);
        Number = argnum;
    }
}
              
public static partial class MathEx
{
    public static void NegEx(this ref int num)
    {
        num = -num;
    }    
    public static void Neg(ref int num)
    {
        num = -num;
    }
}

Details

  • Product in use: Test
  • Version in use: master abea701

gaschd avatar Nov 11 '24 21:11 gaschd