Skip to content

Commit 924785a

Browse files
Fix VB -> C# conversion for DefaultMemberAttribute properties
Previously, any property marked with DefaultMemberAttribute was treated as an indexer, causing the property name to be stripped from the C# output. This resulted in invalid C# code for parameterless default properties (e.g., `obj = "val"` instead of `obj.Prop = "val"`). This change ensures that the property name is only stripped if the property actually has parameters (i.e., is a true indexer). Co-authored-by: GrahamTheCoder <2490482+GrahamTheCoder@users.noreply.github.com>
1 parent 4235dd7 commit 924785a

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

CodeConverter/CSharp/NameExpressionNodeVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public async Task<CSharpSyntaxNode> ConvertMemberAccessExpressionAsync(VBasic.Sy
5252

5353
var simpleNameSyntax = await node.Name.AcceptAsync<SimpleNameSyntax>(TriviaConvertingExpressionVisitor);
5454

55-
var isDefaultProperty = nodeSymbol is IPropertySymbol p && VBasic.VisualBasicExtensions.IsDefault(p);
55+
var isDefaultProperty = nodeSymbol is IPropertySymbol p && VBasic.VisualBasicExtensions.IsDefault(p) && p.Parameters.Any();
5656
ExpressionSyntax left = null;
5757
if (node.Expression is VBasic.Syntax.MyClassExpressionSyntax && nodeSymbol != null) {
5858
if (nodeSymbol.IsStatic) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Threading.Tasks;
2+
using ICSharpCode.CodeConverter.Tests.TestRunners;
3+
using Xunit;
4+
5+
namespace ICSharpCode.CodeConverter.Tests.CSharp.MemberTests;
6+
7+
public class DefaultMemberAttributeTests : ConverterTestBase
8+
{
9+
[Fact]
10+
public async Task TestDefaultMemberAttributeConversionAsync()
11+
{
12+
await TestConversionVisualBasicToCSharpAsync(
13+
@"
14+
<System.Reflection.DefaultMember(""Caption"")>
15+
Public Class ClassWithReflectionDefaultMember
16+
Public Property Caption As String
17+
End Class
18+
19+
<System.Reflection.DefaultMember(NameOf(LoosingProperties.Caption))>
20+
Public Class LoosingProperties
21+
Public Property Caption As String
22+
23+
Sub S()
24+
Dim x = New LoosingProperties()
25+
x.Caption = ""Hello""
26+
27+
Dim y = New ClassWithReflectionDefaultMember() 'from C#
28+
y.Caption = ""World""
29+
End Sub
30+
End Class", @"
31+
using System.Reflection;
32+
33+
[DefaultMember(""Caption"")]
34+
public partial class ClassWithReflectionDefaultMember
35+
{
36+
public string Caption { get; set; }
37+
}
38+
39+
[DefaultMember(nameof(Caption))]
40+
public partial class LoosingProperties
41+
{
42+
public string Caption { get; set; }
43+
44+
public void S()
45+
{
46+
var x = new LoosingProperties();
47+
x.Caption = ""Hello"";
48+
49+
var y = new ClassWithReflectionDefaultMember(); // from C#
50+
y.Caption = ""World"";
51+
}
52+
}
53+
");
54+
}
55+
}

0 commit comments

Comments
 (0)