weixin_39574555
weixin_39574555
2020-12-26 08:55

VB -> C# Parameterized property

Input code

vbnet
        Public ReadOnly Property FullName(ByVal lastNameFirst As Boolean) As String
            Get
                If lastNameFirst Then
                    Return LastName & " " & FirstName
                Else
                    Return FirstName & " " & LastName
                End If
            End Get
        End Property

Erroneous output

C#
        public string FullName
        {
            get
            {
                if (lastNameFirst)
                    return LastName + " " + FirstName;
                else
                    return FirstName + " " + LastName;
            }
        }

Expected output

C#
        public string  FullName(bool lastNameFirst)
        {
                if (lastNameFirst)
                    return LastName + " " + FirstName;
                else
                    return FirstName + " " + LastName;
        }

Details

Product in use: VS extension

Version in use: 6.1.0.0

该提问来源于开源项目:icsharpcode/CodeConverter

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • weixin_39713317 weixin_39713317 4月前

    Thanks for the report. I'm happy to accept a PR. It might take me a while to get around to fixing it, though I wonder if is interested in looking at this since it's in a similar area to their recent PR #197

    点赞 评论 复制链接分享
  • weixin_39574555 weixin_39574555 4月前

    Sadly all i can help with is identify and workarounds.

    Speaking of which, in case anyone else wants to change it before converting it to C# here is a regex to identify the parameterized properties.

    Property?\s+(\w+)\s*(\([^()]+\))

    点赞 评论 复制链接分享
  • weixin_39989190 weixin_39989190 4月前

    The only thing that gets odd about this is the setter. It impossible in C# to call a function and immediately assign something to the result of the function.

    For example, if I have a property such as:

    
    Private Readonly dict As New Dictionary(Of String, Boolean)()
    Public Property Test(str As String) As Boolean
        Get
            Return dict(str)
        End Get
        Set
            If Not dict.ContainsKey(str) Then
                dict.Add(str, False)
            End If
            dict(str) = Value
        End Set
    End Property
    

    How does the set block work? C# can call this property, but they have to call it through the implicit functions created in VB (in this case it would be get_Test and set_Test). I suppose we could swap this out with two overloads, one that takes in the value, one without. The one that takes in the value would be a sub while the one that doesn't would be a function. Any references to a property that has a parameter and is set would then have to be switched to a function call rather than a property set.

    点赞 评论 复制链接分享
  • weixin_39989190 weixin_39989190 4月前

    My mistake... It is actually possible to call a function and set the underlying value... It doesn't work for the case above though in C# and it's only supported in C# 7.3 (I believe)

    Example C# code:

    
    void Main()
    {
        Test() = 5;
        Test().Dump();
    }
    
    private int underlyingTest = 0;
    ref int Test()
    {
        return ref underlyingTest;
    }
    

    P.S. It doesn't work because the above version uses a dictionary under the hood, and you can only return ref values if it's a member of the current instance, rather than part of a collection/other object/etc...

    点赞 评论 复制链接分享
  • weixin_39713317 weixin_39713317 4月前

    Nice investigation. Yep I usually look at the decompiled IL version of a VB assembly using the feature to see if I can at least take some inspiration. Obviously it doesn't always map across nicely though. Since the setter sounds like a complicated (and not super-common) case, I'd happily take a PR that only works in C# 7.3+ if you can get it working. ref sounds like a plausible way to get it working.

    点赞 评论 复制链接分享

相关推荐