How do you use Mono.Cecil to create a simple program from scratch? All the examples and tutorials I've been able to find so far, assume you are working with an existing assembly, reading or making small changes.
Asked
Active
Viewed 4,383 times
2 Answers
23
See the following code to get you started:
var myHelloWorldApp = AssemblyDefinition.CreateAssembly(
new AssemblyNameDefinition("HelloWorld", new Version(1, 0, 0, 0)), "HelloWorld", ModuleKind.Console);
var module = myHelloWorldApp.MainModule;
// create the program type and add it to the module
var programType = new TypeDefinition("HelloWorld", "Program",
Mono.Cecil.TypeAttributes.Class | Mono.Cecil.TypeAttributes.Public, module.TypeSystem.Object);
module.Types.Add(programType);
// add an empty constructor
var ctor = new MethodDefinition(".ctor", Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.HideBySig
| Mono.Cecil.MethodAttributes.SpecialName | Mono.Cecil.MethodAttributes.RTSpecialName, module.TypeSystem.Void);
// create the constructor's method body
var il = ctor.Body.GetILProcessor();
il.Append(il.Create(OpCodes.Ldarg_0));
// call the base constructor
il.Append(il.Create(OpCodes.Call, module.Import(typeof(object).GetConstructor(Array.Empty<Type>()))));
il.Append(il.Create(OpCodes.Nop));
il.Append(il.Create(OpCodes.Ret));
programType.Methods.Add(ctor);
// define the 'Main' method and add it to 'Program'
var mainMethod = new MethodDefinition("Main",
Mono.Cecil.MethodAttributes.Public | Mono.Cecil.MethodAttributes.Static, module.TypeSystem.Void);
programType.Methods.Add(mainMethod);
// add the 'args' parameter
var argsParameter = new ParameterDefinition("args",
Mono.Cecil.ParameterAttributes.None, module.Import(typeof(string[])));
mainMethod.Parameters.Add(argsParameter);
// create the method body
il = mainMethod.Body.GetILProcessor();
il.Append(il.Create(OpCodes.Nop));
il.Append(il.Create(OpCodes.Ldstr, "Hello World"));
var writeLineMethod = il.Create(OpCodes.Call,
module.Import(typeof(Console).GetMethod("WriteLine", new[] { typeof(string) })));
// call the method
il.Append(writeLineMethod);
il.Append(il.Create(OpCodes.Nop));
il.Append(il.Create(OpCodes.Ret));
// set the entry point and save the module
myHelloWorldApp.EntryPoint = mainMethod;
myHelloWorldApp.Write("HelloWorld.exe");

fknx
- 1,775
- 13
- 19
-
Does the PDB also get generated ? – Akshay Bheda Jan 17 '19 at 14:43
-
1@AkshayBheda No, but I don't think that generating a PDB is necessary. As far as I know the job of the PDB file is to act as a bridge between the source code and the compiled code (dll), so that the debugger knows which line of code corresponds to the code which is executed. In this case there is no source code, since the program is created by defining the IL instructions directly. Mono.Cecil has support for [PDBs](https://github.com/jbevain/cecil/wiki/Debug-symbols), but I think that only works for modifying an existing dll that comes with a PDB file. – fknx Jan 17 '19 at 15:21
2
I'm without laptop here (I'm walking in street) so I can't write and show you a full example, but it's simple like other examples you already saw. but inatead of read existed assembly you need to create one and save it.
You need to create an AssemblyDefinition, a ModuleDefinition, a Type and Method and save it to disk.
You need to remember that its IL and metadata writer without compiler.
Check this for example.

Dudi Keleti
- 2,946
- 18
- 33