Skip to content

Commit afb4445

Browse files
8.1.7 addendum - Parameterized property assignment - fixes #610
1 parent 2b49fbf commit afb4445

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
2828
* Don't generate unnecessary properties for WithEvents fields [#572](https://github.com/icsharpcode/CodeConverter/issues/572)
2929
* Add type conversion where needed for externally declared loop control variable [#609](https://github.com/icsharpcode/CodeConverter/issues/609)
3030
* Convert string operators in common cases [#608](https://github.com/icsharpcode/CodeConverter/issues/608)
31+
* Type convert parameterized property in assignment [#610](https://github.com/icsharpcode/CodeConverter/issues/610)
3132

3233
### C# -> VB
3334

CodeConverter/CSharp/CommonConversions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,14 +548,21 @@ public static CSSyntax.VariableDeclaratorSyntax CreateVariableDeclarator(string
548548
!VBasic.VisualBasicExtensions.IsDefault(pro.Property)) {
549549
var isSetter = pro.Parent.Kind == OperationKind.SimpleAssignment && pro.Parent.Children.First() == pro;
550550
var extraArg = isSetter
551-
? await operation.Parent.Syntax.ChildNodes().ElementAt(1).AcceptAsync<ExpressionSyntax>(TriviaConvertingExpressionVisitor)
551+
? await GetParameterizedSetterArgAsync(operation)
552552
: null;
553553
return (isSetter ? pro.Property.SetMethod.Name : pro.Property.GetMethod.Name, extraArg);
554554
}
555555

556556
return (null, null);
557557
}
558558

559+
private async Task<ExpressionSyntax> GetParameterizedSetterArgAsync(IOperation operation)
560+
{
561+
var vbNode = (VBSyntax.ExpressionSyntax) operation.Parent.Syntax.ChildNodes().ElementAt(1);
562+
var csNode = await vbNode.AcceptAsync<ExpressionSyntax>(TriviaConvertingExpressionVisitor);
563+
return TypeConversionAnalyzer.AddExplicitConversion(vbNode, csNode, forceTargetType: operation.Type);
564+
}
565+
559566
public CSSyntax.IdentifierNameSyntax GetRetVariableNameOrNull(VBSyntax.MethodBlockBaseSyntax node)
560567
{
561568
if (!node.MustReturn()) return null;

CodeConverter/CSharp/LiteralConversions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ public static ExpressionSyntax GetLiteralExpression(object value, string textFor
4949
case ulong ul:
5050
return SyntaxFactory.LiteralExpression(CSSyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(textForUser, ul));
5151
case double d:
52-
// The value is passed as a double from VB expression: "3.5F"
52+
// The value is passed as a double from VB expression: "3.5F" and "3.5M"
5353
// Important to use value text, otherwise "10.0" gets coerced to and integer literal of 10 which can change semantics
54-
var syntaxToken = convertedType?.SpecialType == SpecialType.System_Single ? SyntaxFactory.Literal(textForUser, (float) d) : SyntaxFactory.Literal(textForUser, d);
54+
var syntaxToken = convertedType?.SpecialType switch
55+
{
56+
SpecialType.System_Single => SyntaxFactory.Literal(textForUser, (float)d),
57+
SpecialType.System_Decimal => SyntaxFactory.Literal(textForUser, (decimal)d),
58+
_ => SyntaxFactory.Literal(textForUser, d)
59+
};
5560
return SyntaxFactory.LiteralExpression(CSSyntaxKind.NumericLiteralExpression, syntaxToken);
5661
case float f:
5762
return SyntaxFactory.LiteralExpression(CSSyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(textForUser, f));

Tests/CSharp/MemberTests/PropertyMemberTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,44 @@ public override string ToString()
128128
}", hasLineCommentConversionIssue: true);//TODO: Improve comment mapping for parameterized property
129129
}
130130

131+
[Fact]
132+
public async Task TestParameterizedPropertyRequiringConversionAsync()
133+
{
134+
await TestConversionVisualBasicToCSharpAsync(
135+
@"Public Class Class1
136+
Public Property SomeProp(ByVal index As Integer) As Single
137+
Get
138+
Return 1.5
139+
End Get
140+
Set(ByVal Value As Single)
141+
End Set
142+
End Property
143+
144+
Public Sub Foo()
145+
Dim someDecimal As Decimal = 123.0
146+
SomeProp(123) = someDecimal
147+
End Sub
148+
End Class", @"using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
149+
150+
public partial class Class1
151+
{
152+
public float get_SomeProp(int index)
153+
{
154+
return 1.5F;
155+
}
156+
157+
public void set_SomeProp(int index, float value)
158+
{
159+
}
160+
161+
public void Foo()
162+
{
163+
decimal someDecimal = 123.0M;
164+
set_SomeProp(123, Conversions.ToSingle(someDecimal));
165+
}
166+
}", hasLineCommentConversionIssue: true);//TODO: Improve comment mapping for parameterized property
167+
}
168+
131169
[Fact]
132170
public async Task TestOptionalParameterizedPropertyAsync()
133171
{

0 commit comments

Comments
 (0)