Cecil is slang for cocaine 7
Everybody’s round tripping, ‘tripping assemblies
Some very exciting progress have been made today. Let me explain in what I’ve spend the lasts minutes:
- I have compiled Cecil HEAD normally, this produces a Mono.Cecil.dll file.
- I have compiled this little piece of code referencing the previous assembly:
<span class="kwrd">using</span> System;
<span class="kwrd">using</span> Mono.Cecil;
<span class="kwrd">class</span> Test {
<span class="kwrd">static</span> <span class="kwrd">void</span> Main ()
{
RoundTripCecil ();
}
<span class="kwrd">static</span> <span class="kwrd">void</span> RoundTripCecil ()
{
Console.WriteLine (<span class="str">"Round Trip Cecil"</span>);
IAssemblyDefinition asm = AssemblyFactory.GetAssembly (
<span class="str">"Mono.Cecil.dll"</span>);
Console.WriteLine (<span class="str">"Cecil is loaded"</span>);
AssemblyFactory.SaveAssembly (asm, <span class="str">"Mono.Cecil.dll"</span>, AssemblyKind.Dll);
Console.WriteLine (<span class="str">"Cecil is written back"</span>);
}
}
- I’ve runned it, the result is a fresh new round tripped Mono.Cecil.dll
- I’ve modified the previous code to round trip mcs, our C# compiler (if you still follow me, we are here using a round tripped Cecil to round trip mcs)
- I’ve re-compiled Cecil using the fresh new mcs
- And the new Cecil is perfectly working, wow.
The conclusion of all of that is that the writing layer of Cecil is more and more mature and stable. Few things are missing, especially multiple modules assemblies, but who uses that, and complex custom attributes, using arrays, or properties and fields.
Also the new API is in the SVN, it is now much easier for users to modify and create assemblies. Previous code samples on this blog are now deprecated, maybe I should remove them. Or update them. I will see. If you are already using Cecil, you fool, you may encounter one or two little API break with SVN head. The first that comes to my mind is the method of the AssemblyFactory to get an assembly, wich used to take a string and an LoadingType enumeration. This enumeration does not exists anymore, as I decided to get rid of two different engines to load assemblies. The LazyLoading was introducing a lot of complexity, so for the sake of simplicity, I’ve removed it.
Few things are still not implemented in the modifying layer, but I prefer focusing on the writing one before the end of Google’s Summer of Code. Some times after the end of the SoC, I will release the first public version of Cecil. Wich means API freeze, bugs reports, blah.
Sadly, Cecil itself can’t be use to make some nice screen shot. I had to find other ways:
Trackbacks
Use the following link to trackback from your own site:
http://www.evain.net/blog/articles/trackback/33

Are u a Junkie ? :D
En tout cas, sacré boulot que tu nous fais la.
Quick question about embedding resources into an existing assembly. Using your code above as an example I tried :
IAssemblyDefinition asm = AssemblyFactory.GetAssembly(
assemblyPath);
byte[] bytes = File.ReadAllBytes(@"d:\dev\somefile.txt");
EmbeddedResource res = new EmbeddedResource("foo", ManifestResourceAttributes.Public, bytes );
asm.MainModule.Resources.Add(res);
AssemblyFactory.SaveAssembly(asm, assemblyPath, AssemblyKind.Dll);
which I got to work after changing the EmbeddedResource class to public. Is there an alternative way to do this without needing to do that ?
Ian>
Yes there is a way:
byte [] bytes = //...
IEmbeddedResource res = asm.MainModule.Factories.ResourceFactory.CreateEmbeddedResource (...);
asm.MainModule.Resources.Add (res);
Feel free to mail me if you have a question regarding the API, until I publish some docs.
Thanks JB. Although it was actually asm.Factories …
Hi,
I downloaded the cecil library from the svn repository.
Could someone please explain me how can i compile it in windows?
Thanks
Angelo, you can use NAnt to build Cecil on windows. Just run nant in Cecil’s folder.
Thanks
When I run NAnt (cecil target) I receive the following error:
No input specified.
However I succeded to compile it by creating an empty project and adding all the source files.
Do you have an example code to dissasemble an assembly like ildasm or monodis with the cecil library?