In my previous post I claimed that it was not possible, in general, to merge assemblies enhanced by PostSharp, because it would break the assembly references stored as strings in aspect serialization data.

I did not realize that the BinaryFormatter provides a way to solve our problem. It is indeed possible to provide its own SerializationBinder, whose role is precisely to return the System.Type corresponding to an assembly name and a type name. We can provide a custom binder to redirect merged assemblies.

This is the feature I have added to build available for download in the builds section; I could still do it in 1.0 since it is a no-risk feature, breaking nothing existing (if you don't use it, nothing can go wrong).

I have added a new class PostSharp.Laos.LaosSerializationBinder. You can set the static property Current to your own implementation of SerializationBinder. If you don't set it, nothing is changed. The LaosSerializationBinder class it itself derived from SerializationBinder and implements the feature we need: assembly retargeting. So all you have to do is to create an instance of this class, set up retargeting policies, and assign it to LaosSerializationBinder.Current.

The following code sets up PostSharp Laos so that references to 'MergedPostSharpLib.dll' are retargeted to 'MergedPostSharp.exe' (supposing that the library has been merged into the executable):

LaosSerializationBinder binder = new LaosSerializationBinder();
binder.Retarget("MergedPostSharpLib", "MergedPostSharp");
LaosSerializationBinder.Current = binder;

The only thing you have to really take care about is to set up the binder before the first aspect is deserialized. Aspect deserialization is triggered from static constructors of enhanced classes, so you may need to initialize the weaver very soon, and eventually to refactor your "Main(string args[])" method.

Apart from that, it should work very well.

Happy PostSharping!


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:


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,


Now that PostSharp 1.0 is pretty stable and anyway virtually frozen, I am pleased to announce that the first community technical preview of PostSharp 1.5, the current living branch of PostSharp, is now available for download.

This CTP makes a great leap forward in supporting more platforms:

  • Novell Mono. PostSharp now run on Mono and has been tested both in the Windows and the Linux implementation. PostSharp is now truly cross-platform, since the same PostSharp executable can now run on any platform.
  • Microsoft Silverlight 2. Who said aspects are only for server applications? Thanks to PostSharp 1.5, you can now apply aspects to your projects targeting Silverlight 2.
  • Microsoft .NET Compact Framework 2.0.

Enlarging PostSharp support to these platforms required the possibility to read and transform assemblies without loading them in the CLR. Therefore, one of the hidden new features of PostSharp 1.5 is the ability to read assemblies directly from disk.

Another hidden feature is the support for assembly retargeting, which happens when an assembly reference is resolved to an assembly of higher version than was required. This feature, supported by the .NET framework, was missing in PostSharp. This first CTP offers a first yet incomplete solution to this issue.

The documentation and samples have been updated to include support for Silverlight and the Compact Framework.

Note that PostSharp 1.5 is not fully compatible with PostSharp 1.0. End-users should not see the difference, but if you played with PostSharp Core or with some advanced features of PostSharp Laos, your code will need some changes.

Happy PostSharping!