Code - Dynamic Proxies

Code Drop

Download it here.

Overview

This is an assembly that allows you to make a dynamic proxy of an object whose class is non-sealed and non-abstract. The new object inherits from the given object's class, wraps the given object as a private field, and delegates all virtual invocations to this inner object. It also calls a set of invocation handlers to support pre- and post-invocation notifications. The test suite contains numerous code example that demonstrates how the proxies are created. There's also a WinForms client that shows how to use a proxy object.

The assembly also has two key features that will help the user create and maintain sound proxies. The first feature is the ability to verify the generated proxy code (this can be turned on either via configuration or by setting the verify argument to true on the ProxyContext constructor). Underneath the scenes, peverify.exe is used to accomplish this, but the errors are broken down into useable classes. These classes are defined in the AssemblyVerifier.dll assembly that DynamicProxies.dll uses. Essentially, if something is non-verifiable with the proxy code generation, the user will get a VerificationException, which contains a list of verification errors.

The other key feature is the ability to debug the generated proxy code (this can be turned on either via configuration or by setting the generateDebugging argument to true on the ProxyContext constructor). Underneath the scenes, an IL file is generated on the fly, so the user can easily step into the proxy code and see what's going on. Here are a couple of screen shots of this in action. First, here's a test where I generated the PDB and IL files - the breakpoint is on a virtual method:

Now, when I hit F11...well, sometime I have to tell VS where the IL file is. But once I get to the right test output directory and find the IL file, I'm in the proxy code:

Personally, this is the biggest feature out of everything I did with the proxy generation. To have the ability to step right into the dynamic code that's generated...that's an extremely nice feature in my book!

Note that the EmitDebugging.dll assembly contains all of the code to generate the dynamic IL file generation. My intention is to have this assembly as a separate, "supported" project on my site (as it's not directly tied to the dynamic proxies code - you could use it in your own Reflection.Emit-based code if you wanted to), but right now it's kind of hard to follow, and I'd rather keep it "opaque" until I get more tests and documentation around it.

Here's a list of a number of other interesting features:

  • The Proxy.Create() call is generic, so the return type is type-safe.
  • The user can specify if they want transient and/or persistable proxies.
  • Generics are supported. This includes generic methods.
  • All proxies implement an interface called IProxy<T>, with a read-only Target property so you can always get to the underlying wrapped object in a type-safe manner.
  • The ProxyContext object is configurable, so you can turn verification and debugging on and off along with the type of assembly generation without having to recompile code.
  • The name generation is consistent. Assemblies have the original type name plus ".Proxy". The type name is the same with "Proxy" appended, and hooked virtual methods have "Proxy" appended to their names.
  • If anyone was trying to use the proxy code I have in my "CIL Programming" book...don't bother using it. It has numerous bugs, all of which are fixed with this release.

I should note that all of the unit tests use the VS Team Edition-based testing framework, so if you don't have on your machine you will need to remove the DynamicProxies.Tests project from the solution so you don't get any compilation errors.

Known Issues

  • No current issues.

Future Enhancements

  • No future enhancements planned.

Version History

  • 1.0.0.0
    • Code was released.
Blog History