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 #