Recently encapsulated a route that features very similar to MVC. The MVC routing mechanism finds the controller Action first
Reflection (Reflection) is an important mechanism in. NET that, through radiation, can get members of each type in. NET, including classes, structs, delegates, interfaces, and enumerations, including methods, properties, events, constructors, and so on, at run time. You can also get the names, qualifiers, parameters, and so on for each member. With reflection, you can get a sense of each type. If you get the information for the constructor, you can create the object directly, even if the type of the object is not known at compile time.
The first load is based on the DLL name, then the reflection finds the class based on the type, and finds the method based on the method name. Perhaps the author Caishuxueqian write is not very good, I hope you can all more comments. To help me with my ascension. Thank you!
The following code
1 //Get assembly Name if the configuration assembly name is empty, use the current assembly name2 varAssemblyName = configurationmanager.appsettings["Assename"] ==NULL? Assembly.getexecutingassembly (). GetName (). name:configurationmanager.appsettings["Assename"]. ToString ();3 varClassname="ClassName"; //class name 4 varMethodName ="MethodName"; //Method name 5 6 if(string. IsNullOrEmpty (AssemblyName))7 Throw NewException ("assembly name cannot be empty");8 if(string. IsNullOrEmpty (className))9 Throw NewException ("class name cannot be empty");Ten One //reflect the specified DLL, and look for the access modifier of the class class according to the class name must be public AType type =Assembly.Load (AssemblyName). Getexportedtypes (). FirstOrDefault ( -o = O.name.indexof (Domainresourceindex, StringComparison.OrdinalIgnoreCase) >=0); - the if(Type! =NULL) - { - //to create an instance of a class - Objectobj = (Object) activator.createinstance (type); + //depending on the method name passed in, the lookup method -MethodInfo Pmethod =type. GetMethod (methodName); + A if(Pmethod! =NULL) at { - //Execution -Pmethod.invoke (obj,New Object[] {request, response}); - } - Else - { in Throw NewException ("no method found"); - } to } + Else - { the Throw NewException ("No class found"); *}
Reprint Please specify http://www.cnblogs.com/cainiaodage/p/3924165.html