C # implements a mechanism like MVC routing through Reflection

Source: Internet
Author: User

Recently encapsulated a route that features very similar to MVC. The MVC routing mechanism finds the controller Action first

    • What is reflection

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.

    • Implementation ideas

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

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.