Browse Source

Make sure mappingInfo is not null when passed to typedefs, methoddefs, properties and events.

pull/1763/head
Siegfried Pammer 6 years ago
parent
commit
d0f32b93c1
  1. 32
      ICSharpCode.Decompiler/CSharp/RequiredNamespaceCollector.cs
  2. 3
      ILSpy/LoadedAssembly.cs

32
ICSharpCode.Decompiler/CSharp/RequiredNamespaceCollector.cs

@ -55,10 +55,10 @@ namespace ICSharpCode.Decompiler.CSharp
{ {
if (entity == null || entity.MetadataToken.IsNil) if (entity == null || entity.MetadataToken.IsNil)
return; return;
if (mappingInfo == null)
mappingInfo = CSharpDecompiler.GetCodeMappingInfo(entity.ParentModule.PEFile, entity.MetadataToken);
switch (entity) { switch (entity) {
case ITypeDefinition td: case ITypeDefinition td:
if (mappingInfo == null)
mappingInfo = CSharpDecompiler.GetCodeMappingInfo(entity.ParentModule.PEFile, entity.MetadataToken);
namespaces.Add(td.Namespace); namespaces.Add(td.Namespace);
HandleAttributes(td.GetAttributes()); HandleAttributes(td.GetAttributes());
HandleTypeParameters(td.TypeParameters); HandleTypeParameters(td.TypeParameters);
@ -100,23 +100,19 @@ namespace ICSharpCode.Decompiler.CSharp
CollectNamespacesForTypeReference(param.Type); CollectNamespacesForTypeReference(param.Type);
} }
HandleTypeParameters(method.TypeParameters); HandleTypeParameters(method.TypeParameters);
if (!method.MetadataToken.IsNil) { var reader = module.PEFile.Reader;
if (mappingInfo == null) var parts = mappingInfo.GetMethodParts((MethodDefinitionHandle)method.MetadataToken).ToList();
mappingInfo = CSharpDecompiler.GetCodeMappingInfo(entity.ParentModule.PEFile, entity.MetadataToken); foreach (var part in parts) {
var reader = module.PEFile.Reader; HandleOverrides(part.GetMethodImplementations(module.metadata), module);
var parts = mappingInfo.GetMethodParts((MethodDefinitionHandle)method.MetadataToken).ToList(); var methodDef = module.metadata.GetMethodDefinition(part);
foreach (var part in parts) { if (method.HasBody) {
HandleOverrides(part.GetMethodImplementations(module.metadata), module); MethodBodyBlock body;
var methodDef = module.metadata.GetMethodDefinition(part); try {
if (method.HasBody) { body = reader.GetMethodBody(methodDef.RelativeVirtualAddress);
MethodBodyBlock body; } catch (BadImageFormatException) {
try { continue;
body = reader.GetMethodBody(methodDef.RelativeVirtualAddress);
} catch (BadImageFormatException) {
continue;
}
CollectNamespacesFromMethodBody(body, module);
} }
CollectNamespacesFromMethodBody(body, module);
} }
} }
break; break;

3
ILSpy/LoadedAssembly.cs

@ -156,7 +156,8 @@ namespace ICSharpCode.ILSpy
// runs on background thread // runs on background thread
if (state is Stream stream) { if (state is Stream stream) {
// Read the module from a precrafted stream // Read the module from a precrafted stream
module = new PEFile(fileName, stream, stream is MemoryStream ? PEStreamOptions.PrefetchEntireImage : PEStreamOptions.Default, metadataOptions: options); var streamOptions = stream is MemoryStream ? PEStreamOptions.PrefetchEntireImage : PEStreamOptions.Default;
module = new PEFile(fileName, stream, streamOptions, metadataOptions: options);
} else { } else {
// Read the module from disk (by default) // Read the module from disk (by default)
stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

Loading…
Cancel
Save