Skip to content

Commit

Permalink
Fix icsharpcode#1927: NRE in ExpressionBuilder when trying to decompi…
Browse files Browse the repository at this point in the history
…le catch-when blocks consisting of multiple statements.
  • Loading branch information
siegfriedpammer committed Feb 15, 2020
1 parent 63590f3 commit e2b10ad
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,11 +1970,21 @@ protected internal override TranslatedExpression VisitBlockContainer(BlockContai
var body = statementBuilder.ConvertAsBlock(container);
body.InsertChildAfter(null, new Comment(" Could not convert BlockContainer to single expression"), Roles.Comment);
var ame = new AnonymousMethodExpression { Body = body };
var delegateType = new ParameterizedType(compilation.FindType(typeof(Func<>)), InferReturnType(body));
var systemFuncType = compilation.FindType(typeof(Func<>));
var blockReturnType = InferReturnType(body);
var delegateType = new ParameterizedType(systemFuncType, blockReturnType);
var invocationTarget = new CastExpression(ConvertType(delegateType), ame);
ResolveResult rr;
// This might happen when trying to decompile an assembly built for a target framework where System.Func<T> does not exist yet.
if (systemFuncType.Kind == TypeKind.Unknown) {
rr = new ResolveResult(blockReturnType);
} else {
var invokeMethod = delegateType.GetDelegateInvokeMethod();
rr = new CSharpInvocationResolveResult(new ResolveResult(delegateType), invokeMethod, EmptyList<ResolveResult>.Instance);
}
return new InvocationExpression(new MemberReferenceExpression(invocationTarget, "Invoke"))
.WithILInstruction(container)
.WithRR(new CSharpInvocationResolveResult(new ResolveResult(delegateType), delegateType.GetDelegateInvokeMethod(), EmptyList<ResolveResult>.Instance));
.WithRR(rr);
} finally {
statementBuilder.currentReturnContainer = oldReturnContainer;
statementBuilder.currentResultType = oldResultType;
Expand Down

0 comments on commit e2b10ad

Please sign in to comment.