static public void ImplementMethod(Type interfaceType, MethodInfo method, TypeBuilder typeBuilder, FieldInfo invoker, FieldInfo target, Hashtable props) { Type[] args = Reflector.getParameterTypes(method.GetParameters()); // Don't need to do these for Object if ((method.Name == "Equals" || method.Name == "ToString" || method.Name == "Finalize") && args.Length == 0) { return; } MethodBuilder methodBuilder = null; if (!isPropertyFunc(method.Name)) { methodBuilder = typeBuilder.DefineMethod(method.Name , MethodAttributes.Public | MethodAttributes.Virtual , method.ReturnType , args); } else { methodBuilder = typeBuilder.DefineMethod(method.Name , MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.SpecialName , method.ReturnType , args); } // Forward all calls to the invocationHandler delegate genDelegateCall(interfaceType, args, methodBuilder, invoker, target); checkForPropertyFuncs(methodBuilder, props); //Bind functions to properties if applicable }
static Type buildTypeForInterfaces(Type[] interfaces) { AppDomain appDomain = AppDomain.CurrentDomain; AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = "_Dymamic__PROXY_TT_"; AssemblyBuilder assemblyBuilder = appDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("_TT_DymanicModule_"); TypeBuilder builder = moduleBuilder.DefineType(String.Format("TTProxy{0}.{1}", rand.Next(), interfaces[0]) , TypeAttributes.Public , typeof(Proxy) , interfaces ); Type proxyType = typeof(Proxy); FieldInfo fb = proxyType.GetField("_invocationDelegate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); FieldInfo target = proxyType.GetField("target", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); foreach (Type t in interfaces) { Hashtable properties = new Hashtable(); // Now define the properties of any given interface. foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { Type[] args = Reflector.getParameterTypes(pi.GetIndexParameters()); PropertyBuilder pb = builder.DefineProperty(pi.Name, pi.Attributes, pi.PropertyType, args); properties[pb.Name] = pb; } foreach (MethodInfo m in t.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { Implementor.ImplementMethod(t, m, builder, fb, target, properties); } } return(builder.CreateType()); }