Beispiel #1
0
 /// <summary>
 /// Creates an <see cref="AssemblyDef"/> instance from a stream
 /// </summary>
 /// <remarks>This will read all bytes from the stream and call <see cref="Load(byte[],ModuleContext)"/>.
 /// It's better to use one of the other Load() methods.</remarks>
 /// <param name="stream">The stream</param>
 /// <param name="context">Module context or <c>null</c></param>
 /// <returns>A new <see cref="AssemblyDef"/> instance</returns>
 /// <exception cref="ArgumentNullException">If <paramref name="stream"/> is <c>null</c></exception>
 /// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
 public static AssemblyDef Load(Stream stream, ModuleContext context)
 {
     return(Load(stream, new ModuleCreationOptions(context)));
 }
Beispiel #2
0
 /// <summary>
 /// Creates an <see cref="AssemblyDef"/> instance from a byte[]
 /// </summary>
 /// <param name="data">Contents of a .NET assembly</param>
 /// <param name="context">Module context or <c>null</c></param>
 /// <returns>A new <see cref="AssemblyDef"/> instance</returns>
 /// <exception cref="ArgumentNullException">If <paramref name="data"/> is <c>null</c></exception>
 /// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
 public static AssemblyDef Load(byte[] data, ModuleContext context)
 {
     return(Load(data, new ModuleCreationOptions(context)));
 }
Beispiel #3
0
 /// <summary>
 /// Creates an <see cref="AssemblyDef"/> instance from a memory location
 /// </summary>
 /// <param name="addr">Address of a .NET assembly</param>
 /// <param name="context">Module context or <c>null</c></param>
 /// <returns>A new <see cref="AssemblyDef"/> instance</returns>
 /// <exception cref="ArgumentNullException">If <paramref name="addr"/> is <c>null</c></exception>
 /// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
 public static AssemblyDef Load(IntPtr addr, ModuleContext context)
 {
     return(Load(addr, new ModuleCreationOptions(context)));
 }
Beispiel #4
0
 /// <summary>
 /// Creates an <see cref="AssemblyDef"/> instance from a file
 /// </summary>
 /// <param name="fileName">File name of an existing .NET assembly</param>
 /// <param name="context">Module context or <c>null</c></param>
 /// <returns>A new <see cref="AssemblyDef"/> instance</returns>
 /// <exception cref="ArgumentNullException">If <paramref name="fileName"/> is <c>null</c></exception>
 /// <exception cref="BadImageFormatException">If it's not a .NET assembly (eg. not a .NET file or only a .NET module)</exception>
 public static AssemblyDef Load(string fileName, ModuleContext context)
 {
     return(Load(fileName, new ModuleCreationOptions(context)));
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="context">Module context</param>
 public ModuleCreationOptions(ModuleContext context)
 {
     this.Context           = context;
     this.PdbImplementation = PdbImplType.Default;
 }
Beispiel #6
0
        private AssemblyDef FindClosestAssembly(IAssembly assembly, AssemblyDef closest, IEnumerable <string> paths, ModuleContext moduleContext)
        {
            if (paths == null)
            {
                return(closest);
            }
            var asmComparer = AssemblyNameComparer.CompareAll;

            foreach (var path in paths.GetSafeEnumerable())
            {
                ModuleDefMD mod = null;
                try
                {
                    mod = ModuleDefMD.Load(path, moduleContext);
                    var asm = mod.Assembly;
                    if (asm != null && asmComparer.CompareClosest(assembly, closest, asm) == 1)
                    {
                        if (!IsCached(closest) && closest != null)
                        {
                            var closeMod = closest.ManifestModule;
                            if (closeMod != null)
                            {
                                closeMod.Dispose();
                            }
                        }
                        closest = asm;
                        mod     = null;
                    }
                }
                catch
                {
                }
                finally
                {
                    if (mod != null)
                    {
                        mod.Dispose();
                    }
                }
            }

            return(closest);
        }
Beispiel #7
0
        /// <summary>
        /// Finds an assembly that exactly matches the requested assembly
        /// </summary>
        /// <param name="assembly">Assembly to find</param>
        /// <param name="paths">Search paths or <c>null</c> if none</param>
        /// <param name="moduleContext">Module context</param>
        /// <returns>An <see cref="AssemblyDef"/> instance or <c>null</c> if an exact match
        /// couldn't be found.</returns>
        private AssemblyDef FindExactAssembly(IAssembly assembly, IEnumerable <string> paths, ModuleContext moduleContext)
        {
            if (paths == null)
            {
                return(null);
            }
            var asmComparer = AssemblyNameComparer.CompareAll;

            foreach (var path in paths.GetSafeEnumerable())
            {
                ModuleDefMD mod = null;
                try
                {
                    mod = ModuleDefMD.Load(path, moduleContext);
                    var asm = mod.Assembly;
                    if (asm != null && asmComparer.Equals(assembly, asm))
                    {
                        mod = null;
                        return(asm);
                    }
                }
                catch
                {
                }
                finally
                {
                    if (mod != null)
                    {
                        mod.Dispose();
                    }
                }
            }
            return(null);
        }
Beispiel #8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="defaultModuleContext">Module context for all resolved assemblies</param>
 public AssemblyResolver(ModuleContext defaultModuleContext)
     : this(defaultModuleContext, true)
 {
 }