Skip to content

Commit

Permalink
Do not output private get/set for interfaces
Browse files Browse the repository at this point in the history
Closes icsharpcode#270

# Conflicts:
#	ICSharpCode.CodeConverter/CSharp/NodesVisitor.cs
  • Loading branch information
mrmonday authored and GrahamTheCoder committed Apr 19, 2019
1 parent 91cd89e commit 8ea6d0d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 10 additions & 3 deletions ICSharpCode.CodeConverter/CSharp/NodesVisitor.cs
Expand Up @@ -455,6 +455,7 @@ public override CSharpSyntaxNode VisitPropertyStatement(VBSyntax.PropertyStateme
var modifiers = CommonConversions.ConvertModifiers(convertibleModifiers, GetMemberContext(node));
var isIndexer = node.Modifiers.Any(m => SyntaxTokenExtensions.IsKind(m, VBasic.SyntaxKind.DefaultKeyword));
var accessedThroughMyClass = IsAccessedThroughMyClass(node, node.Identifier, _semanticModel.GetDeclaredSymbol(node));
bool isInInterface = node.Ancestors().OfType<VBSyntax.InterfaceBlockSyntax>().FirstOrDefault() != null;

var initializer = (EqualsValueClauseSyntax)node.Initializer?.Accept(TriviaConvertingVisitor);
var rawType = (TypeSyntax)node.AsClause?.TypeSwitch(
Expand All @@ -476,9 +477,15 @@ public override CSharpSyntaxNode VisitPropertyStatement(VBSyntax.PropertyStateme
if (isReadonly) {
setAccessor = setAccessor.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
}
// In VB, there's a backing field which can always be read and written to even on ReadOnly/WriteOnly properties.
// Our conversion will rewrite usages of that field to use the property accessors which therefore must exist and be private at minimum.
accessors = SyntaxFactory.AccessorList(SyntaxFactory.List(new[] {getAccessor, setAccessor}));
if (isInInterface && isReadonly) {
accessors = SyntaxFactory.AccessorList(SyntaxFactory.List(new[] { getAccessor }));
} else if (isInInterface && isWriteOnly) {
accessors = SyntaxFactory.AccessorList(SyntaxFactory.List(new[] { setAccessor }));
} else {
// In VB, there's a backing field which can always be read and written to even on ReadOnly/WriteOnly properties.
// Our conversion will rewrite usages of that field to use the property accessors which therefore must exist and be private at minimum.
accessors = SyntaxFactory.AccessorList(SyntaxFactory.List(new[] { getAccessor, setAccessor }));
}
} else {
accessors = SyntaxFactory.AccessorList(
SyntaxFactory.List(
Expand Down
16 changes: 15 additions & 1 deletion Tests/CSharp/MemberTests.cs
Expand Up @@ -321,6 +321,20 @@ public int Test3
}");
}

[Fact]
public void TestReadWriteOnlyInterfaceProperty()
{
TestConversionVisualBasicToCSharpWithoutComments(
@"Public Interface Foo
ReadOnly Property P1() As String
WriteOnly Property P2() As String
End Interface", @"public interface Foo
{
string P1 { get; }
string P2 { set; }
}");
}

[Fact]
public void TestConstructor()
{
Expand Down Expand Up @@ -1259,7 +1273,7 @@ public void TestWriteOnlyProperties()
WriteOnly Property Items As Integer()
End Interface", @"interface TestInterface
{
int[] Items { private get; set; }
int[] Items { set; }
}");
}

Expand Down

0 comments on commit 8ea6d0d

Please sign in to comment.