diff --git a/src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpConstructsVisitor.cs b/src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpConstructsVisitor.cs index de6b57b911..a94a4c5ae8 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpConstructsVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/Parser/Visitors/CSharpConstructsVisitor.cs @@ -101,7 +101,10 @@ namespace ICSharpCode.NRefactory.Parser } if (ident != null && se != null) { InvocationExpression ie = se.Expression as InvocationExpression; - if (ie != null && (ie.TargetObject as IdentifierExpression).Identifier == ident.Identifier) { + if (ie != null && + ie.TargetObject is IdentifierExpression && + (ie.TargetObject as IdentifierExpression).Identifier == ident.Identifier) + { ReplaceCurrentNode(new RaiseEventStatement(ident.Identifier, ie.Arguments)); } } diff --git a/src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs b/src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs index c8da50e3b7..9fdad66b5a 100644 --- a/src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs +++ b/src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs @@ -124,6 +124,29 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter "RaiseEvent MyEvent(Me, EventArgs.Empty)"); } + [Test] + public void IfStatementSimilarToRaiseEvent() + { + TestStatement("if (FullImage != null) DrawImage();", + "If FullImage IsNot Nothing Then\n" + + "\tDrawImage()\n" + + "End If"); + // regression test: + TestStatement("if (FullImage != null) e.DrawImage();", + "If FullImage IsNot Nothing Then\n" + + "\te.DrawImage()\n" + + "End If"); + // with braces: + TestStatement("if (FullImage != null) { DrawImage(); }", + "If FullImage IsNot Nothing Then\n" + + "\tDrawImage()\n" + + "End If"); + TestStatement("if (FullImage != null) { e.DrawImage(); }", + "If FullImage IsNot Nothing Then\n" + + "\te.DrawImage()\n" + + "End If"); + } + [Test] public void AnonymousMethod() {