PostSharp and ILMerge

by Gael Fraiteur on 24 Jul 2008

Combining PostSharp with ILMerge seems a recurring demand. Indeed, many developers don't welcome the multiplication of dependent assemblies as PostSharp.Public.dll and PostSharp.Laos.dll, and ILMerge seems the dreamed solution to that. Unfortunately, it will work only in rare cases.

The Simple Case

Say you have an assembly named MyProgram.exe with aspects defined in this assembly. The application is formed of the following assemblies:

PostSharp.Public.dll
PostSharp.Laos.dll
MyProgram.exe

In this particular case, it is valid to merge all three assemblies into a new assembly equally named MyProgram.exe.

However, if you modify the name of the merged assembly, it will cease to work.

What's Wrong?

Remember that the aspects are serialized into the assembly. It uses the BinaryFormatter to do the job. Unfortunately for our purpose, this formatter stores the full type name of aspects, including the assembly name. So when you rename the assembly, you break the link between the aspect serialization and the underlying types, and you get an exception during deserialization.

The same occurs when you have aspects in one of the dependencies you merge. Say your program references MyLibrary.dll, whose some classes have been enhanced by PostSharp Laos. If you merge it into MyProgram.exe, the serialization of aspects previously defined in MyLibrary.dll will be broken.

Possible Solution

The problem clearly lays in the use of the BinaryFormatter to do the job. We could eventually use another serializer that does not store the full type name.

This feature (pluggable serializers) was actually present in former builds of what is now PostSharp 1.5, but I have removed the feature because it became useless. If there is some interest, I could restore it. It very much depends on the feedback I will get.

How is this a critical feature for you?

 

Happy PostSharping,

~Gael