Skip to content

Commit b1e4ced

Browse files
Set refkind for raise event invocations - fixes #584
1 parent 6f29db9 commit b1e4ced

4 files changed

Lines changed: 34 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1212
* Handle Option Compare Text case insensitive comparisons in switch statements [#579](https://github.com/icsharpcode/CodeConverter/issues/579)
1313
* Fix compilation error when switching with enum cases [#549](https://github.com/icsharpcode/CodeConverter/issues/549)
1414
* Improve numeric casts [#580](https://github.com/icsharpcode/CodeConverter/issues/580)
15+
* Add ref to conversion of RaiseEvent where needed [#584](https://github.com/icsharpcode/CodeConverter/issues/584)
1516

1617
### C# -> VB
1718

CodeConverter/CSharp/CommonConversions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ public ISymbol GetDeclaredCsOriginalSymbolOrNull(VBasic.VisualBasicSyntaxNode no
633633

634634
public ISymbol GetCsOriginalSymbolOrNull(ISymbol symbol)
635635
{
636+
if (symbol == null) return null;
636637
symbol = symbol.OriginalDefinition;
637638
// Construct throws an exception if ConstructedFrom differs from it, so let's use ConstructedFrom directly
638639
var symbolToFind = symbol is IMethodSymbol m ? m.ConstructedFrom : symbol;

CodeConverter/CSharp/ExpressionNodeVisitor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ public override async Task<CSharpSyntaxNode> VisitSimpleArgument(VBasic.Syntax.S
409409
SyntaxToken token = default(SyntaxToken);
410410
var convertedArgExpression = ((ExpressionSyntax)await node.Expression.AcceptAsync(TriviaConvertingExpressionVisitor)).SkipParens();
411411
var typeConversionAnalyzer = CommonConversions.TypeConversionAnalyzer;
412-
if (symbol is IMethodSymbol methodSymbol) {
413-
var parameters = (CommonConversions.GetCsOriginalSymbolOrNull(methodSymbol.OriginalDefinition) ?? methodSymbol).GetParameters();
414-
var refType = GetRefConversionType(node, argList, parameters, out var argName, out var refKind);
412+
var possibleParameters = (CommonConversions.GetCsOriginalSymbolOrNull(symbol?.OriginalDefinition) ?? symbol)?.GetParameters();
413+
if (possibleParameters.HasValue) {
414+
var refType = GetRefConversionType(node, argList, possibleParameters.Value, out var argName, out var refKind);
415415
token = GetRefToken(refKind);
416416
if (refType != RefConversion.Inline) {
417417
convertedArgExpression = HoistByRefDeclaration(node, convertedArgExpression, refType, argName, refKind);

Tests/CSharp/MemberTests/EventMemberTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,5 +570,34 @@ public void POW_btnV2DBM_Click()
570570
1 target compilation errors:
571571
CS0246: The type or namespace name 'Button' could not be found (are you missing a using directive or an assembly reference?)");
572572
}
573+
574+
[Fact]
575+
public async Task Issue584_EventWithByRefAsync()
576+
{
577+
await TestConversionVisualBasicToCSharpAsync(@"Public Class Issue584RaiseEventByRefDemo
578+
Public Event ConversionNeeded(ai_OrigID As Integer, ByRef NewID As Integer)
579+
580+
Public Function TestConversion(ai_ID) As Integer
581+
Dim i_NewValue As Integer
582+
RaiseEvent ConversionNeeded(ai_ID, i_NewValue)
583+
Return i_NewValue
584+
End Function
585+
End Class", @"using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
586+
587+
public partial class Issue584RaiseEventByRefDemo
588+
{
589+
public event ConversionNeededEventHandler ConversionNeeded;
590+
591+
public delegate void ConversionNeededEventHandler(int ai_OrigID, ref int NewID);
592+
593+
public int TestConversion(object ai_ID)
594+
{
595+
var i_NewValue = default(int);
596+
ConversionNeeded?.Invoke(Conversions.ToInteger(ai_ID), ref i_NewValue);
597+
return i_NewValue;
598+
}
599+
}
600+
");
601+
}
573602
}
574603
}

0 commit comments

Comments
 (0)