Before you read any further please see these much more tested and performant proxy solutions:
Be warned: What you are about to see is untested and most likely unperformant, unstable, unfriendly, and unsupported. Don't say I didn't warn you...
However, if you are like me and you just want a tiny solution to call your own, or even just wanting to learn something about dynamic proxy generation then please proceed!
Today I decided to try my hand at making a very simple, single-class proxy. The purpose for something like this would be to be able to implement an interface at run time to either wrap some kind of communication / IO layer or proxy an existing instance of that interface allowing me to intercept method and property interactions.
For example: Let's say I have an application that has a bunch of services that it interacts with. After the application is already written I decide I need to start logging information about each call to my services. I really don't want to go into every method on every service and add calls to a logger. Nor do I want to change the way my application interacts with my services. I'm way too lazy for that!
One annoying way to solve this problem would be to write a whole new class for each service interface that keeps an instance of the actual service and forwards calls to it after logging. Not the most desirable solution since I now have to write even more code than I was when I was just adding logging to the actual service methods. On top of this I would now have 2 classes to maintain if the service's interface is ever added to or changed.
What I really want to do is automate or generate this wrapper dynamically, yet still be able to function in a strongly typed environment. As I've stated before there are a number of existing solutions that will allow me to do this, but for this post I wanted to dive a little deeper and see how this stuff works for myself.
What it does:
Below I have an abstract Proxy<T> base class. This doesn't need to be abstract, but isn't terribly useful by itself as I have written it. The point of this implementation would be to extend this to a concrete class that is more useful for a specific use-case (of which I have several).
- public abstract class Proxy<T> where T : class
- {
- #region Static Cache (NOTE: This is per T)
- /// <summary>
- /// The dynamic proxy type
- /// </summary>
- protected static readonly Type InstanceType;
- /// <summary>
- /// The type of T
- /// </summary>
- protected static readonly Type Interface;
- private static readonly Dictionary<int, MethodInfo> MethodIndex;
- /// <summary>
- /// Looks up the MethodInfo for type T by index
- /// </summary>
- public static MethodInfo Lookup(int index) { return MethodIndex[index]; }
- private static AssemblyBuilder Assembly;
- /// <summary>
- /// Saves the underlying generated assembly to disk
- /// </summary>
- public static void Save() { Assembly.Save(Assembly.GetName().Name + ".dll"); }
- static Proxy()
- {
- Interface = typeof(T);
- if (!Interface.IsInterface) throw new InvalidOperationException(string.Format("{0} is not an interface", Interface.Name));
- MethodIndex = Interface.GetMethods().Select((m, i) => new {m, i}).ToDictionary(i => i.i, m => m.m);
- InstanceType = Generate();
- }
- #endregion
- /// <summary>
- /// The underlying instance being proxied or null
- /// </summary>
- protected T Target { get; private set; }
- /// <summary>
- /// The proxy instance
- /// </summary>
- protected T Instance { get; private set; }
- protected Proxy() { Instance = (T)Activator.CreateInstance(InstanceType, this); }
- protected Proxy(T target) : this() { Target = target; }
- /// <summary>
- /// Invokes the call handlers for this proxy
- /// </summary>
- public object Trigger(MethodInfo method, object[] parameters)
- {
- BeforeCall(method, parameters);
- var result = OnCall(method, parameters);
- AfterCall(method, parameters, result);
- return result;
- }
- /// <summary>
- /// Called prior to OnCall
- /// </summary>
- protected virtual void BeforeCall(MethodInfo method, object[] parameters) {}
- /// <summary>
- /// Called after OnCall
- /// </summary>
- protected virtual void AfterCall(MethodInfo method, object[] parameters, object result) {}
- /// <summary>
- /// When overridden should handle invoking the MethodInfo on Target if any
- /// </summary>
- protected virtual object OnCall(MethodInfo method, object[] parameters)
- {
- if (Target == null) return method.ReturnType.IsValueType ? Activator.CreateInstance(method.ReturnType) : null;
- return method.Invoke(Target, parameters);
- }
- #region Emit Generation (
- private static Type Generate()
- {
- var rootname = new AssemblyName(Interface.Name + "Proxy_" + Guid.NewGuid());
- var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(rootname, AssemblyBuilderAccess.RunAndSave);
- var module = assembly.DefineDynamicModule(rootname.Name, rootname.Name + ".dll");
- var builder = module.DefineType(rootname.Name + ".Proxy", TypeAttributes.Class, typeof(object), new[] { typeof(T) });
- var proxy = builder.DefineField("_proxy", typeof (Proxy<T>), FieldAttributes.Private);
- ConfigureConstructor(builder, proxy);
- ConfigureInterface<T>(builder, proxy);
- Assembly = assembly;
- return builder.CreateType();
- }
- private static void ConfigureConstructor(TypeBuilder builder, FieldInfo proxy)
- {
- var ctor = builder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new[] { typeof(Proxy<T>) });
- var ctorBase = typeof (object).GetConstructor(new Type[0]);
- var encoder = ctor.GetILGenerator();
- encoder.Emit(OpCodes.Ldarg_0);
- encoder.Emit(OpCodes.Call, ctorBase);
- encoder.Emit(OpCodes.Ldarg_0);
- encoder.Emit(OpCodes.Ldarg_1);
- encoder.Emit(OpCodes.Stfld, proxy);
- encoder.Emit(OpCodes.Ret);
- }
- private static void ConfigureInterface<TIFace>(TypeBuilder builder, FieldInfo proxy = null)
- {
- var members = typeof (TIFace).GetMembers();
- var methods = members.OfType<MethodInfo>().Where(m => !m.IsSpecialName).ToList();
- var properties = members.OfType<PropertyInfo>().ToList();
- methods.ForEach(m => ConfigureMethod(builder, proxy, m));
- properties.ForEach(p => ConfigureProperty(builder, proxy, p));
- }
- private static void ConfigureProperty(TypeBuilder builder, FieldInfo proxy, PropertyInfo info)
- {
- var property = builder.DefineProperty(
- info.Name,
- info.Attributes,
- CallingConventions.HasThis,
- info.PropertyType,
- new Type[0]);
- if (info.CanRead) property.SetGetMethod(ConfigureMethod(builder, proxy, info.GetGetMethod()));
- if (info.CanWrite) property.SetSetMethod(ConfigureMethod(builder, proxy, info.GetSetMethod()));
- }
- private static MethodBuilder ConfigureMethod(TypeBuilder builder, FieldInfo proxy, MethodInfo info)
- {
- var index = MethodIndex.First(m => m.Value == info).Key;
- var method = builder.DefineMethod(
- info.Name,
- (MethodAttributes.Public | MethodAttributes.Virtual | info.Attributes | MethodAttributes.Abstract) ^ MethodAttributes.Abstract,
- CallingConventions.HasThis,
- info.ReturnType,
- info.GetParameters().Select(p => p.ParameterType).ToArray());
- var encoder = method.GetILGenerator();
- ConfigureCall(encoder, proxy, info, index);
- return method;
- }
- private static void ConfigureCall(ILGenerator encoder, FieldInfo proxy, MethodInfo method, int index)
- {
- var returnsVoid = method.ReturnType == typeof(void);
- var returnsValue = !returnsVoid && method.ReturnType.IsValueType;
- var lookup = typeof(Proxy<T>).GetMethod("Lookup", BindingFlags.Public | BindingFlags.Static);
- var interceptor = proxy.FieldType.GetMethod("Trigger", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
- var arguments = method.GetParameters().Select(p => p.ParameterType).ToArray();
- encoder.DeclareLocal(returnsVoid ? typeof(object) : method.ReturnType); // return value (local 0)
- encoder.DeclareLocal(typeof (object[])); // parameters (local 1)
- encoder.Emit(OpCodes.Ldarg_0); // this.
- encoder.Emit(OpCodes.Ldfld, proxy); // this._proxy
- encoder.Emit(OpCodes.Ldc_I4_S, index); // index of method to lookup
- encoder.Emit(OpCodes.Call, lookup); // lookup methodinfo by index
- encoder.Emit(OpCodes.Ldc_I4_S, arguments.Length); // set size of parameter array
- encoder.Emit(OpCodes.Newarr, typeof(object)); // create parameter array
- encoder.Emit(OpCodes.Stloc_1); // store to local 1
- for (var i = 0; i < arguments.Length; i++) { // load up parameter array values
- var argument = arguments[i];
- encoder.Emit(OpCodes.Ldloc_1); // get ready to add to array (local 1)
- ConfigureLdc(encoder, i); // index to add to
- ConfigureLdarg(encoder, i + 1); // get argument (index + 1)
- if (argument.IsValueType) encoder.Emit(OpCodes.Box, argument); // convert to object if needed
- encoder.Emit(OpCodes.Stelem_Ref); // push to array
- }
- encoder.Emit(OpCodes.Ldloc_1); // get array
- encoder.Emit(OpCodes.Callvirt, interceptor); // pass stack to method
- if (returnsValue) {
- encoder.Emit(OpCodes.Unbox_Any, method.ReturnType); // un-object a value
- } else if (!returnsVoid) {
- encoder.Emit(OpCodes.Castclass, method.ReturnType); // cast to return type
- } else {
- encoder.Emit(OpCodes.Pop); // discard return value
- }
- encoder.Emit(OpCodes.Ret);
- }
- private static readonly OpCode[] Ldargs = new[] { OpCodes.Ldarg_0, OpCodes.Ldarg_1, OpCodes.Ldarg_2, OpCodes.Ldarg_3 };
- private static void ConfigureLdarg(ILGenerator encoder, int index)
- {
- if (index >= Ldargs.Length) encoder.Emit(OpCodes.Ldarg_S, (short)(index));
- else encoder.Emit(Ldargs[index]);
- }
- private static readonly OpCode[] Ldcs = new[] { OpCodes.Ldc_I4_0, OpCodes.Ldc_I4_1, OpCodes.Ldc_I4_2, OpCodes.Ldc_I4_3, OpCodes.Ldc_I4_4, OpCodes.Ldc_I4_5, OpCodes.Ldc_I4_6, OpCodes.Ldc_I4_7, OpCodes.Ldc_I4_8 };
- private static void ConfigureLdc(ILGenerator encoder, int index)
- {
- if (index >= Ldcs.Length) encoder.Emit(OpCodes.Ldc_I4, index);
- else encoder.Emit(Ldcs[index]);
- }
- #endregion
- }
(This is a huge blob of code I'll explain and break down later)
Let's focus on the example at hand. I want to use this class now to generate proxies for services on the fly. To keep this dynamic I will want to extend the Proxy<T> base class into something like this:
- public class ServiceLogger<T> : Proxy<T> where T : class
- {
- public T Service { get { return Instance; } }
- protected ILogger Logger { get; private set; }
- public ServiceLogger(T service, ILogger logger) : base(service)
- {
- Logger = logger;
- }
- protected override object OnCall(MethodInfo method, object[] parameters)
- {
- try {
- return base.OnCall(method, parameters);
- } catch (Exception ex) {
- Logger.Fatal("{0}.{1} failed : with {2}",
- method.DeclaringType.Name,
- method.Name,
- string.Join(", ", parameters));
- throw;
- }
- }
- protected override void AfterCall(MethodInfo method, object[] parameters, object result)
- {
- Logger.Info("{0}.{1} called : with {2} : returning {3}",
- method.DeclaringType.Name,
- method.Name,
- string.Join(", ", parameters),
- result);
- }
- }
Consider this:
- ILogger logging;
- IMyService myServiceInstance = new MyService();
- ServiceLogger<IMyService> proxyLogger = new ServiceLogger<IMyService>(myServiceInstance, logging);
- IDependentCode willBeLogged = new DependentCode(proxyLogger.Service);
- IDependentCode wontBeLogged = new DependentCode(myServiceInstance);
The "willBeLogged" and the "wontBeLogged" objects both have the same dependency satisfied. One with a proxy, and the other with the direct service. The one with the proxy will have all of its calls to the proxy logged without having to change anything in the "DependentCode" class.
How it works:
All the code you write in .NET eventually becomes IL (Intermediate Language) when it is compiled. Even though you've written your application in C#, your application is "thinking" in IL (well, not really). Therefore you can't directly tell your application to just run a block of dynamically generated C# code at run-time without some kind of other service to compile your code down into IL. Fortunately for you there is no need to seek the services of a compiler because the .NET framework provides you with the ability to write your own IL.The first thing you'll need to do in order to start building your own dynamic types is create a dynamic assembly in which to store it. As it is, all of the assemblies in your application are loaded into memory from DLLs and the like. Once an assembly is loaded into your AppDomain you will no longer be able to edit it. So you need to expect that each dynamic Type that you want to create will need to be housed in its own dynamic assembly.
Because of this, if you are not careful, you will have a memory leak as you create more and more dynamic types and will need to make sure that you are only creating new types when necessary. For this I have added a static field to the base Proxy<T> class that holds the dynamic type which is then generated in the static constructor. Normally I avoid adding static fields to generic classes because a new value is generated for each "T" used, however in this case it is exactly what I want.
- public abstract class Proxy<T> where T : class
- {
- protected static readonly Type InstanceType;
- ...
- static Proxy() {
- ...
- InstanceType = Generate();
- }
The "Generate()" method will create a dynamic type that inherits the "T" interface. The dynamic type will be given a private _proxy field that will reference the owning "Proxy<T>" instance. Each method on the dynamic class will then call the "_proxy.Trigger(...)" method with the appropriate information. The "Trigger" method will call the "BeforeCall", "OnCall", and "AfterCall" virtual methods which can then be overridden by your own subclass to perform whatever interception actions you want.
When generation is complete the dynamically created type resembles a class that would look something like this:
- public class GeneratedProxyClass : IFace
- {
- private Proxy<IFace> _proxy;
- public GeneratedProxyClass(Proxy<IFace> proxy) { _proxy = proxy; }
- public string MethodA(int arg1, int arg2)
- {
- var methodInfo = Proxy<IFace>.Lookup(1);
- var parameters = new object[] { arg1, arg2 };
- return (string)_proxy.Trigger(methodInfo, parameters);
- }
- public void MethodB(string value)
- {
- var methodInfo = Proxy<IFace>.Lookup(2);
- var parameters = new object[] { value };
- _proxy.Trigger(methodInfo, parameters);
- }
- }
- Create a dynamic Assembly in the current AppDomain
- Create a dynamic Module in the dynamic Assembly
- Create a dynamic Type in the dynamic Module
- Create Fields, Properties, and Methods in the dynamic Type
- "Build" the new System.Type
Defining a member on a dynamic type is as easy as ".DefineField", ".DefineMethod", or ".DefineProperty". Obviously underlying methods won't magically know what to do when they are invoked. This is where all the IL encoding comes in.
I'm not, however, going to dive into how to write functionality with IL in this post. My own understanding of how to write IL is very basic and most of that has come from compiling assemblies and looking at what the compiler generates. Nevertheless, I have made sure to leave comments on each Emit statement to help explain what's happening. You can see these in the "ConfigureCall" method.
Enjoy!
Did you like this post?
Let me know: http://markonthenet.com/contact.htm