Simple reflex Exercise

Source: Internet
Author: User

The main:

    • The example is constructed by reflection;
    • And the method of invoking the instance;
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Reflection;usingsystem.security;usingSystem.Security.Permissions;usingSystem.Security.Policy;usingSystem.Text;usingSystem.Threading.Tasks;namespaceassemblybuilder{classProgram { Public StaticAssembly readassembly (stringassemblypath) {            returnAssembly.LoadFrom (assemblypath); }         Public StaticAssembly readassembly (Stream stream) {SecurityPermission SecurityPermission=NewSecurityPermission (securitypermissionflag.controlevidence);            Securitypermission.assert (); intnum = (int) stream.            Length; byte[] Array =New byte[num]; Stream. Read (Array,0, num); Try            {                returnAssembly.Load (array); }            finally{Codeaccesspermission.revertassert (); }        }         Public StaticStream Readassemblytostream (stringassemblypath) {FileStream FileStream=NewFileStream (AssemblyPath, FileMode.Open, FileAccess.Read, FileShare.Read); Try            {                intnum = (int) Filestream.length; byte[] Array =New byte[num]; FileStream.Read (Array,0, num); return NewMemoryStream (array); }            finally{filestream.close (); }        }        Static voidMain (string[] args) {Stream Assemblystream= Readassemblytostream ("./lambda_test.exe"); varAssembly =readassembly (Assemblystream); //initializing an instance with the InvokeMember call constructor can also            varprograminstance = assembly. CreateInstance ("Factorymode.program"); if(Programinstance = =NULL) {Console.WriteLine ("There is no this type"); }            Else            {                varMyType =Programinstance.gettype (); Console.WriteLine ("Type name is {0}", mytype.tostring ()); Methodinfo[] Methods= Mytype.getmethods (BindingFlags.Instance | BindingFlags.Public |bindingflags.static); Console.WriteLine ("Method number:{0}", methods.                Length); foreach(varMethodinchmethods) {                    //How do I invoke a method by reflection?                     if(method. Name = ="Notmainmethod") {Console.WriteLine ("Find method, name:{0}", method.                        Name); //There is no instance of the InvokeMember call directly with the type, you need to call the type's constructor and then call the target method (before prompting for a defined constructor), take the Bindingflags.createinstance //mytype.invokemember ("Notmainmethod", BindingFlags.Public |                        Bindingflags.createinstance, NULL, NULL, NULL); //when using an existing instance, specify the BindingFlags.InstanceMytype.invokemember ("Notmainmethod", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,NULL, Programinstance,NULL); //If you have an instance, use it directlyMethod. Invoke (Programinstance,NULL);        }}} Console.readkey (); }    }}

Simple reflex Exercise

Related Article

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.