Browse Source

Fix NullReferenceException in TransformDelegateConstruction()

pull/1108/head
Daniel Grunwald 8 years ago
parent
commit
935575859f
  1. 53
      ICSharpCode.Decompiler/IL/Transforms/DelegateConstruction.cs

53
ICSharpCode.Decompiler/IL/Transforms/DelegateConstruction.cs

@ -134,34 +134,35 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (!IsDelegateConstruction(value)) if (!IsDelegateConstruction(value))
return null; return null;
var targetMethod = ((IInstructionWithMethodOperand)value.Arguments[1]).Method; var targetMethod = ((IInstructionWithMethodOperand)value.Arguments[1]).Method;
if (IsAnonymousMethod(decompilationContext.CurrentTypeDefinition, targetMethod)) { if (!IsAnonymousMethod(decompilationContext.CurrentTypeDefinition, targetMethod))
target = value.Arguments[0]; return null;
var methodDefinition = (Mono.Cecil.MethodDefinition)context.TypeSystem.GetCecil(targetMethod); target = value.Arguments[0];
var localTypeSystem = context.TypeSystem.GetSpecializingTypeSystem(targetMethod.Substitution); var methodDefinition = (Mono.Cecil.MethodDefinition)context.TypeSystem.GetCecil(targetMethod);
var ilReader = new ILReader(localTypeSystem); if (methodDefinition == null)
ilReader.UseDebugSymbols = context.Settings.UseDebugSymbols; return null;
var function = ilReader.ReadIL(methodDefinition.Body, context.CancellationToken); var localTypeSystem = context.TypeSystem.GetSpecializingTypeSystem(targetMethod.Substitution);
function.DelegateType = value.Method.DeclaringType; var ilReader = new ILReader(localTypeSystem);
function.CheckInvariant(ILPhase.Normal); ilReader.UseDebugSymbols = context.Settings.UseDebugSymbols;
var function = ilReader.ReadIL(methodDefinition.Body, context.CancellationToken);
var contextPrefix = targetMethod.Name; function.DelegateType = value.Method.DeclaringType;
foreach (ILVariable v in function.Variables.Where(v => v.Kind != VariableKind.Parameter)) { function.CheckInvariant(ILPhase.Normal);
v.Name = contextPrefix + v.Name;
}
var nestedContext = new ILTransformContext(function, localTypeSystem, context.Settings) { var contextPrefix = targetMethod.Name;
CancellationToken = context.CancellationToken, foreach (ILVariable v in function.Variables.Where(v => v.Kind != VariableKind.Parameter)) {
DecompileRun = context.DecompileRun v.Name = contextPrefix + v.Name;
};
function.RunTransforms(CSharpDecompiler.GetILTransforms().TakeWhile(t => !(t is DelegateConstruction)), nestedContext);
function.AcceptVisitor(new ReplaceDelegateTargetVisitor(target, function.Variables.SingleOrDefault(v => v.Index == -1 && v.Kind == VariableKind.Parameter)));
// handle nested lambdas
((IILTransform)new DelegateConstruction()).Run(function, nestedContext);
function.AddILRange(target.ILRange);
function.AddILRange(value.Arguments[1].ILRange);
return function;
} }
return null;
var nestedContext = new ILTransformContext(function, localTypeSystem, context.Settings) {
CancellationToken = context.CancellationToken,
DecompileRun = context.DecompileRun
};
function.RunTransforms(CSharpDecompiler.GetILTransforms().TakeWhile(t => !(t is DelegateConstruction)), nestedContext);
function.AcceptVisitor(new ReplaceDelegateTargetVisitor(target, function.Variables.SingleOrDefault(v => v.Index == -1 && v.Kind == VariableKind.Parameter)));
// handle nested lambdas
((IILTransform)new DelegateConstruction()).Run(function, nestedContext);
function.AddILRange(target.ILRange);
function.AddILRange(value.Arguments[1].ILRange);
return function;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save