Using system; using system. LINQ; using system. reflection; using system. reflection. emit; Public sealed class dynamicproxy {Private Static readonly string assemblyname = "dynamicproxy", modulename = "dynamicproxy", typename = "dynamicproxy"; private assemblybuilder createdynamicassembly <t> () where T: class {return appdomain. currentdomain. definedynamicassembly (New assemblyname (assemblyname + typeof (T ). name), assemblybuilderaccess. run);} private modulebuilder createdynamicmodule <t> () where T: Class {return createdynamicassembly <t> (). definedynamicmodule (modulename + typeof (t ). name);} // <summary> // create a dynamic proxy // </Summary> Public t createdynamictype <t> () where T: Class, new () {typebuilder = createdynamicmodule <t> (). definetype (typename + typeof (t ). name, typeattributes. public | Typeattributes. class, typeof (t); typeactuator <t> (typebuilder); Return activator. createinstance (typebuilder. createtype () as t;} private void buildctormethod (type classtype, fieldbuilder, typebuilder) {var structurebuilder = typebuilder. defineconstructor (methodattributes. public, callingconventions. standard, null); var ilctor = structurebuilder. getilgenerator (); ilctor. em It (Opcodes. ldarg_0); ilctor. emit (Opcodes. newobj, classtype. getconstructor (type. emptytypes); ilctor. emit (Opcodes. stctor, fieldbuilder); ilctor. emit (Opcodes. RET);} private void buildmethod (ilgenerator Il, methodinfo, type [] parametertypes) {Il. emit (Opcodes. ldarg_0); For (INT I = 0; I <parametertypes. length; I ++) Il. emit (Opcodes. ldarg_s, (short) (I + 1); Il. emit (Opcodes. call, methodinfo ); Il. emit (Opcodes. RET);} private void typeactuator <t> (typebuilder builder) where T: Class {fieldbuilder = builder. definefield ("_ dynamicproxyactuator", typeof (t), fieldattributes. private); buildctormethod (typeof (t), fieldbuilder, builder); methodinfo [] info = getmethodinfo (typeof (t); foreach (methodinfo in info) {If (! Methodinfo. isvirtual &&! Methodinfo. isabstract) continue; If (methodinfo. name = "tostring") continue; If (methodinfo. name = "gethashcode") continue; If (methodinfo. name = "equals") continue; var parametertypes = methodinfo. getparameters (). select (P => P. parametertype ). toarray (); methodbuilder = createmethod (builder, methodinfo. name, methodattributes. public | methodattributes. virtual, callingconventions. standard, methodinfo. returntype, parametertypes); var ilmethod = methodbuilder. getilgenerator (); buildmethod (ilmethod, methodinfo, parametertypes);} private methodbuilder createmethod (typebuilder, string name, methodattributes attrs, callingconventions, type, type [] parametertypes) {return typebuilder. definemethod (name, attrs, callingconventions, type, parametertypes);} private methodinfo [] getmethodinfo (type) {return type. getmethods (bindingflags. public | bindingflags. instance );}}