public TypeDefinition ResolveTypeDefinition(IScanContext context, Type type) { var token = new MetadataToken((uint)type.MetadataToken); var typeDefinition = _cache[token] as TypeDefinition; if (typeDefinition == null) { var parameters = context.GetReaderParameters(); var assembly = AssemblyDefinition.ReadAssembly(type.Assembly.Location, parameters); var typeReference = assembly.MainModule.GetTypes().Single(_ => _.MetadataToken == token); typeDefinition = typeReference.Resolve(); _cache[token] = typeDefinition; } return(typeDefinition); }
public static IEnumerable <AssemblyDefinition> ReadAssembly(IScanContext context, AssemblyNameReference assemblyName) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (assemblyName == null) { throw new ArgumentNullException(nameof(assemblyName)); } var parameters = context.GetReaderParameters(); var resolver = parameters.AssemblyResolver ?? new DefaultAssemblyResolver(); var assembly = resolver.Resolve(assemblyName, parameters); return(new[] { assembly }); }
public static IEnumerable <AssemblyDefinition> ReadAssembly(IScanContext context, string path) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (path == null) { throw new ArgumentNullException(nameof(path)); } try { var parameters = context.GetReaderParameters(); var assembly = AssemblyDefinition.ReadAssembly(path, parameters); return(new[] { assembly }); } catch (BadImageFormatException) { // skip on error } catch (DirectoryNotFoundException) { // skip on error } catch (FileNotFoundException) { // skip on error } catch (FileLoadException) { // skip on error } catch (Exception exception) { var ignore = context.Options.FindAll <ITransformIgnoreException>(); if (!ignore.Any(_ => _.IgnoreException("ReadAssembly", exception))) { throw; } } return(Enumerable.Empty <AssemblyDefinition>()); }