I am writing a simple desktop client/server application in C#. For self-educational purposes, I built my own serialization system for the messages (defined as classes) sent back and forth between the two applications over a tcp/ip socket connection. The system uses reflection at initialization time to construct serialize/deserialize methods for each message type by emitting IL.
The first version of this system used DynamicMethod, passing in true to the the constructor to allow the generated IL (that's operating on arbitrary fields in the message type) to ignore access permissions. This worked and the people rejoiced, but I was dissatisfied with how painfully opaque debugging the resulting functions was. So I decided to ditch DynamicMethod and use the *Builder classes to construct a dynamic assembly which I could optionally save to disk and examine with a tool like .NET Reflector.
I refactored the system and then hit a bit of a brick wall. Any time one of the new serialization functions attempts to access a private field or method in one of my message types I get a FieldAccessException or MethodAccessException. After much googling and gnashing of teeth, I think I have narrowed the problem to one of permissions; in particular I think my dynamically created assembly is missing the ReflectionPermissionFlag.MemberAccess permission relative to the calling/constructing assembly (where all the reflected types are sitting).
Unfortunately, I can't seem to figure out how to modify the dynamic assembly creation process such that the assembly has reflection permission back into the creating assembly. The permissions parameters to DefineDynamicAssembly seem related to restricting permission, not granting it, which leaves us with the Evidence parameter. Evidence seems to magically translate into a set of permissions, but I can't find any useful examples or explanations for how this occurs.
So my questions are:
(1) Am I correct i