/// <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="options">Module creation options 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, ModuleCreationOptions options) { if (stream == null) { throw new ArgumentNullException("stream"); } ModuleDef module = null; try { module = ModuleDefMD.Load(stream, options); var asm = module.Assembly; if (asm == null) { throw new BadImageFormatException(string.Format("{0} is only a .NET module, not a .NET assembly. Use ModuleDef.Load().", module.ToString())); } return(asm); } catch { if (module != null) { module.Dispose(); } throw; } }
/// <summary> /// Creates an <see cref="AssemblyDef"/> instance from a memory location /// </summary> /// <param name="addr">Address of a .NET assembly</param> /// <param name="options">Module creation options 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, ModuleCreationOptions options) { if (addr == IntPtr.Zero) { throw new ArgumentNullException("addr"); } ModuleDef module = null; try { module = ModuleDefMD.Load(addr, options); var asm = module.Assembly; if (asm == null) { throw new BadImageFormatException(string.Format("{0} (addr: {1:X8}) is only a .NET module, not a .NET assembly. Use ModuleDef.Load().", module.ToString(), addr.ToInt64())); } return(asm); } catch { if (module != null) { module.Dispose(); } throw; } }