When i write code to show messagebox in Entry point it will not run but if i replace the messagebox to file write it will run without problem. And if i make compile in dnspy and save the assembly again it will run!. Code ** i used dnlib ** :
private void MakeMessageBoxMethod(ModuleDefMD module)
{
var newtype = new TypeDefUser("MBNS", "MB", module.CorLibTypes.Object.TypeDefOrRef);
newtype.Attributes = dnlib.DotNet.TypeAttributes.NotPublic | dnlib.DotNet.TypeAttributes.AutoLayout | dnlib.DotNet.TypeAttributes.Class | dnlib.DotNet.TypeAttributes.AnsiClass;
module.Types.Add(newtype);
// Method that will call the messagebox
var MsgBoxMethod = new MethodDefUser("sh",
MethodSig.CreateStatic(module.CorLibTypes.Void, module.CorLibTypes.String));
MsgBoxMethod.Attributes = dnlib.DotNet.MethodAttributes.Public | dnlib.DotNet.MethodAttributes.Static;
MsgBoxMethod.ImplAttributes = dnlib.DotNet.MethodImplAttributes.IL | dnlib.DotNet.MethodImplAttributes.Managed;
MsgBoxMethod.ParamDefs.Add(new ParamDefUser("_", 1));
newtype.Methods.Add(MsgBoxMethod);
var MessageBoxRef = new TypeRefUser(module, "System.Windows.Forms", "MessageBox", module.CorLibTypes.AssemblyRef);
var ShowRef = new MemberRefUser(module, "Show",
MethodSig.CreateStatic(module.CorLibTypes.Void, module.CorLibTypes.String,module.CorLibTypes.String),
MessageBoxRef);
var epBody = new CilBody();
MsgBoxMethod.Body = epBody;
MsgBoxMethod.Body.Instructions.Add(OpCodes.Ldarg_0.ToInstruction());
MsgBoxMethod.Body.Instructions.Add(OpCodes.Ldstr.ToInstruction("MessageBox Title"));
MsgBoxMethod.Body.Instructions.Add(OpCodes.Call.ToInstruction(ShowRef));
MsgBoxMethod.Body.Instructions.Add(OpCodes.Ret.ToInstruction());
MsgBoxMethod.Body.Instructions.OptimizeBranches();
// Entry point
var MainMethod = module.GetTypes().First(t => t.Name == "Program").Methods.First(m => m.Name == "Main");
MainMethod.Body.Instructions.Insert(1, OpCodes.Ldstr.ToInstruction("The message"));
MainMethod.Body.Instructions.Insert(2, OpCodes.Call.ToInstruction(MsgBoxMethod));
}
The Entrypoint Method | The Method i added it
If i decompile the program and compile it again it will run without any problem. I used dnspy ^