Skip to content

Commit b19fb82

Browse files
VB -> C#: Convert use of event backing field - fixes #186
1 parent 2a1f6b8 commit b19fb82

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

ICSharpCode.CodeConverter/CSharp/CommonConversions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ public SyntaxToken ConvertIdentifier(SyntaxToken id, bool isAttribute = false)
286286
// The case above is basically that if the symbol is a parameter, and the corresponding definition is a property set definition
287287
// AND the first explicitly declared parameter is this symbol, we need to replace it with value.
288288
text = "value";
289-
} else if (text.StartsWith("_", StringComparison.OrdinalIgnoreCase) && symbol is IFieldSymbol fieldSymbol && fieldSymbol.AssociatedSymbol?.IsKind(SymbolKind.Property) == true) {
290-
text = fieldSymbol.AssociatedSymbol.Name;
289+
} else if (text.StartsWith("_", StringComparison.OrdinalIgnoreCase) && symbol is IFieldSymbol propertyFieldSymbol && propertyFieldSymbol.AssociatedSymbol?.IsKind(SymbolKind.Property) == true) {
290+
text = propertyFieldSymbol.AssociatedSymbol.Name;
291+
} else if (text.EndsWith("Event", StringComparison.OrdinalIgnoreCase) && symbol is IFieldSymbol eventFieldSymbol && eventFieldSymbol.AssociatedSymbol?.IsKind(SymbolKind.Event) == true) {
292+
text = eventFieldSymbol.AssociatedSymbol.Name;
291293
}
292294
}
293295
}

Tests/CSharp/ExpressionTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,37 @@ public void Tuple()
997997
}");
998998
}
999999

1000+
[Fact]
1001+
public void UseEventBackingField()
1002+
{
1003+
TestConversionVisualBasicToCSharpWithoutComments(
1004+
@"Public Class Foo
1005+
Public Event Bar As EventHandler(Of EventArgs)
1006+
1007+
Protected Sub OnBar(e As EventArgs)
1008+
If BarEvent Is Nothing Then
1009+
System.Diagnostics.Debug.WriteLine(""No subscriber"")
1010+
Else
1011+
RaiseEvent Bar(Me, e)
1012+
End If
1013+
End Sub
1014+
End Class",
1015+
@"using System;
1016+
1017+
public class Foo
1018+
{
1019+
public event EventHandler<EventArgs> Bar;
1020+
1021+
protected void OnBar(EventArgs e)
1022+
{
1023+
if (Bar == null)
1024+
System.Diagnostics.Debug.WriteLine(""No subscriber"");
1025+
else
1026+
Bar?.Invoke(this, e);
1027+
}
1028+
}");
1029+
}
1030+
10001031
[Fact]
10011032
public void StringInterpolationWithDoubleQuotes()
10021033
{

0 commit comments

Comments
 (0)