Weaving aspects (AOP) into an assembly during build. Is this possible?

At Unite11 there was a session on using reflection. Previously my team had given thought to weaving certain codes (null checks, logs, etc) into our codebase in our development environment that we do not want in the real game. To us at least, the $64M question is not about weaving the byte codes but where to do it in the build process. We asked this question and got a vague answer about using post processing.

The AssetPostProcessor class seemed to be a natural place to do this but does not look like it supports source assets per se. I guess I could always do this post build on the entire assembly but it would be really nice to roll it into process before the actual DLLs that Unity produces are written out. Post build processing would not buy me much in the editor.

Has anyone done any weaving during the build process, i.e., other than reading the DLL off the disk post build and instrumenting it?

I do it a bit like this:

[InitializeOnLoad]
public static class AssemblyPostProcessor
{
   static AssemblyPostProcessor()
   {
      DoCecilStuff();
      if( anyAssembliesWereChanged )
      {
         UnityEditorInternal.InternalEditorUtility.RequestScriptReload();
      }
   }
}

Provided you keep track of whether you actually injected any code anywhere, you shouldn’t end up in an infinite script reloading loop. I do this by adding a custom attribute to the assembly to mark it as processed, but equally you could just keep track of whether or not you added any code.