Skip to content

Commit 24eff41

Browse files
Implicilty typed inherited events no longer create extra delegates - fixes #700
1 parent 98bbddd commit 24eff41

3 files changed

Lines changed: 66 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99

1010

1111
### VB -> C#
12-
12+
* All handlers from multi-line handles syntax now converted [#701](https://github.com/icsharpcode/CodeConverter/issues/701)
13+
* Implicilty typed inherited events no longer create extra delegates [#700](https://github.com/icsharpcode/CodeConverter/issues/700)
1314

1415
### C# -> VB
1516

CodeConverter/CSharp/DeclarationNodeVisitor.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,8 @@ public override async Task<CSharpSyntaxNode> VisitEventStatement(VBSyntax.EventS
12281228
var modifiers = CommonConversions.ConvertModifiers(node, node.Modifiers, GetMemberContext(node));
12291229
var id = CommonConversions.ConvertIdentifier(node.Identifier);
12301230

1231-
if (node.AsClause == null) {
1231+
var symbol = _semanticModel.GetDeclaredSymbol(node);
1232+
if (node.AsClause == null && symbol.BaseMember() == null) {
12321233
var delegateName = SyntaxFactory.Identifier(id.ValueText + "EventHandler");
12331234

12341235
var delegateDecl = SyntaxFactory.DelegateDeclaration(
@@ -1251,12 +1252,13 @@ await node.ParameterList.AcceptAsync<ParameterListSyntax>(_triviaConvertingExpre
12511252
_additionalDeclarations.Add(node, new MemberDeclarationSyntax[] { delegateDecl });
12521253
return eventDecl;
12531254
}
1254-
1255+
var type = symbol.Type != null || node.AsClause == null ? CommonConversions.GetTypeSyntax(symbol.Type) : await node.AsClause.Type.AcceptAsync<TypeSyntax>(_triviaConvertingExpressionVisitor);
1256+
var declaration = SyntaxFactory.VariableDeclaration(type,
1257+
SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(id)));
12551258
return SyntaxFactory.EventFieldDeclaration(
12561259
SyntaxFactory.List(attributes),
12571260
modifiers,
1258-
SyntaxFactory.VariableDeclaration(await node.AsClause.Type.AcceptAsync<TypeSyntax>(_triviaConvertingExpressionVisitor),
1259-
SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(id)))
1261+
declaration
12601262
);
12611263
}
12621264

Tests/CSharp/MemberTests/EventMemberTests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,64 @@ public void RaisingFlour()
6262
}");
6363
}
6464

65+
[Fact]
66+
public async Task TestEventsOnInterfaceAsync()
67+
{
68+
await TestConversionVisualBasicToCSharpAsync(
69+
@"Public Interface IFileSystem
70+
71+
Event FileChanged(FileData As String)
72+
Event FileCreated(FileData As String)
73+
Event FileDeleted(FileData As String)
74+
Event FileRenamed(e As RenamedEventArgs)
75+
Event WatcherError(e As ErrorEventArgs)
76+
77+
End Interface
78+
79+
Public Class FileSystemWin
80+
Implements IFileSystem
81+
82+
Public Event FileChanged(FileData As String) Implements IFileSystem.FileChanged
83+
Public Event FileCreated(FileData As String) Implements IFileSystem.FileCreated
84+
Public Event FileDeleted(FileData As String) Implements IFileSystem.FileDeleted
85+
Public Event FileRenamed(e As RenamedEventArgs) Implements IFileSystem.FileRenamed
86+
Public Event WatcherError(e As ErrorEventArgs) Implements IFileSystem.WatcherError
87+
88+
End Class", @"using System.IO;
89+
90+
public partial interface IFileSystem
91+
{
92+
event FileChangedEventHandler FileChanged;
93+
94+
delegate void FileChangedEventHandler(string FileData);
95+
96+
event FileCreatedEventHandler FileCreated;
97+
98+
delegate void FileCreatedEventHandler(string FileData);
99+
100+
event FileDeletedEventHandler FileDeleted;
101+
102+
delegate void FileDeletedEventHandler(string FileData);
103+
104+
event FileRenamedEventHandler FileRenamed;
105+
106+
delegate void FileRenamedEventHandler(RenamedEventArgs e);
107+
108+
event WatcherErrorEventHandler WatcherError;
109+
110+
delegate void WatcherErrorEventHandler(ErrorEventArgs e);
111+
}
112+
113+
public partial class FileSystemWin : IFileSystem
114+
{
115+
public event IFileSystem.FileChangedEventHandler FileChanged;
116+
public event IFileSystem.FileCreatedEventHandler FileCreated;
117+
public event IFileSystem.FileDeletedEventHandler FileDeleted;
118+
public event IFileSystem.FileRenamedEventHandler FileRenamed;
119+
public event IFileSystem.WatcherErrorEventHandler WatcherError;
120+
}");
121+
}
122+
65123
[Fact]
66124
public async Task TestModuleHandlesWithEventsAsync()
67125
{

0 commit comments

Comments
 (0)