The reflection mechanism of C #

Source: Internet
Author: User

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Reflection;namespacecodetest{classProgram {Static voidMain (string[] args) {            stringstr ="Hello"; //System.Type-based reflection in C #Type T =Str.            GetType ();            Console.WriteLine (T.fullname); Type T2= Type.GetType ("System.String",false,true); Console.WriteLine (T2.            FullName); Type T3=typeof(string); Console.WriteLine (T3.            FullName); //reflection of methods, properties, and so on in C #//getmethods (t, BindingFlags.Public | bindingflags.instance);Console.WriteLine ("{0}", T.getmethod ("Copy"));            T.getproperties ();            T.getfields (); //dynamic loading and deferred binding in C #//Get AssemblyAssembly Assembly = Assembly.Load ("codetest"); //Assembly assembly2 = Assembly.LoadFrom ("CodeTest.dll"); //Gets the specified object type from the assembly;Type type = assembly. GetType ("Codetest.user"); //create an instance using Activator (parameterless constructor)            varUser1 =activator.createinstance (type); //creating an instance with activator (with parametric constructors)            varUser2 = Activator.CreateInstance (Type,"Dusk"); //create an instance using assembly (parameterless constructor)            varUser3 = assembly. CreateInstance ("Codetest.user"); //Reflection parameterless ConstructorConstructorInfo Constructor1 = type. GetConstructor (Newtype[] {}); varUser4 = Constructor1. Invoke (New Object[] { }); //reflection with parametric constructorsConstructorInfo Constructor2 = type. GetConstructor (NewType[] {typeof(string) }); varUser5 = Constructor2. Invoke (New Object[] {"Dusk" }); //calling the public function (no arguments)Type. InvokeMember ("Publicshow", BindingFlags.InvokeMethod| BindingFlags.Public | BindingFlags.Instance,NULL, User1,NULL); //calling the public function (with parameters)            stringReturnValue =type. InvokeMember ("GetString", BindingFlags.InvokeMethod| Bindingflags.optionalparambinding,NULL, User1,New Object[] {"Dusk"}) as string; //calling a static method            stringReturnValue2 =type. InvokeMember ("Staticmethod", BindingFlags.InvokeMethod | BindingFlags.Public |BindingFlags.Static,NULL,NULL,New Object[] { }) as string; //call the Private method. Type. InvokeMember ("Privateshow", BindingFlags.InvokeMethod| BindingFlags.NonPublic | BindingFlags.Instance,NULL, User1,New Object[] { }); //Reflection Properties            varName =type. InvokeMember ("Name", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance,NULL, User1,New Object[] { }) as string; //Set the property (set the Name property to "new property")Type. InvokeMember ("Name", BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance,NULL, User1,New Object[] {"New Properties" }); //Reflection Fields            stringField =type. InvokeMember ("Field", Bindingflags.getfield | BindingFlags.Public | BindingFlags.Instance,NULL, User1,New Object[] { }) as string; //Set fields (Set Field field to "new field")Type. InvokeMember ("Field", Bindingflags.setfield | BindingFlags.Public | BindingFlags.Instance,NULL, User1,New Object[] {"new Field" });        Console.ReadLine (); }         Public Static voidGetMethods (Type T, BindingFlags f) {methodinfo[] info=t.getmethods (f); foreach(varIteminchinfo) {Console.WriteLine ("{0}", item.            Name); }        }    }     Public classUser {//Field         Public stringField; //Properties         Public stringName {Get;Set; } //constructor Function         PublicUser () { This. Name ="non-parametric construction"; }         PublicUser (stringname) {             This. Name =name; }        //Public function         Public voidPublicshow () {Console.WriteLine (string. Format ("Reflection calls a public method")); }        //Private Function        Private voidPrivateshow () {Console.WriteLine (string. Format ("reflection calls a private method")); }        //static function         Public Static stringStaticmethod () {return "reflection calls a static method"; }        //with parameter return value function         Public stringGetString (stringname) {            return string. Format ("Hello everyone, my name is: {0}!", name); }        //Events         Public EventEventHandler EventHandler; //event handler function         Public voiddoevent () {if(EventHandler! =NULL) EventHandler (NULL, Eventargs.empty); }    }}

The reflection mechanism of C #

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.