Ilgenerator. emit dynamic msil programming (iii) Dynamic proxy

Source: Internet
Author: User
Tags emit
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 );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.