ModuleDefMD module = ModuleDefMD.Load("input.exe"); module.NativeWrite("output.exe");
ModuleDefMD module = new ModuleDefMD("Sample", Guid.NewGuid()); TypeDef type = new TypeDefUser("Namespace", "Class", module.CorLibTypes.Object.TypeDefOrRef); MethodDef method = new MethodDefUser("Method", MethodSig.CreateStatic(module.CorLibTypes.Void), MethodImplAttributes.IL, MethodAttributes.Static | MethodAttributes.Public); type.Methods.Add(method); module.Types.Add(type); MemoryStream stream = new MemoryStream(); module.NativeWrite(stream);Explanation: This code creates a new module with a single type and method, then writes just the code to a memory stream instead of a file. The module can be saved to a file later, or used directly from the memory stream.