C # Get classes and methods under classes (for ASP. NET MVC)

Source: Internet
Author: User

In the development of MVC project encountered problems, do permissions control, through the MVC filter to achieve, so in the allocation of permissions when you want to get all the Controller and action method, by looking for data, referring to the "ASP. NET MVC Framework Disclosure", the final realization.

In C #, the implementation of dynamic fetch classes and methods is mainly implemented by reflection, to refer to System.Reflection.

1      Publicactionresult getcontrollerandaction ()2List<type> controllertypes =NewList<type> ();//Create a list of controller types3      varAssembly = Assembly.Load ("Mysoft.ui");//Loading Assemblies4Controllertypes.addrange (assembly. GetTypes (). Where (type =typeof(IController). IsAssignableFrom (type) && type. name!="Errorcontroller"));//gets all the classes under the Assembly, inheriting all the types of the IController class through a LINQ filter5StringBuilder Jsonbuilder =NewStringBuilder ();//creating dynamic strings, stitching JSON data note: JSON types now pass data in a fashion that is more concise than XML6Jsonbuilder.append ("[");7      foreach(varControllerinchControllertypes)//traversing the controller class8      {9Jsonbuilder.append ("{\ "controllername\": \ "");Ten Jsonbuilder.append (Controller. Name); OneJsonbuilder.append ("\ ", \" controllerdesc\ ": \""); AJsonbuilder.append (Controller. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute) = =NULL?"": (Controller. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute). Description);//gets the description of the controller description -Jsonbuilder.append ("\ ", \" action\ ": ["); -           varactions = Controller. GetMethods (). Where (method = method). Returntype.name = ="ActionResult");//get all the return type ActionResult under the controller, the permissions control of MVC as long as the limit so the front and back of the interaction request line, unified for ActionResult the           foreach(varActioninchactions) -           { -Jsonbuilder.append ("{\ "actionname\": \ ""); - Jsonbuilder.append (action. Name); +Jsonbuilder.append ("\ ", \" actiondesc\ ": \""); -Jsonbuilder.append (action. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute) = =NULL?"": (Action. GetCustomAttribute (typeof(DescriptionAttribute)) asDescriptionAttribute). Description);//get a description of the action +Jsonbuilder.append ("\"},"); A           } atJsonbuilder.remove (Jsonbuilder.length-1,1); -Jsonbuilder.append ("]},"); -       } -Jsonbuilder.remove (Jsonbuilder.length-1,1); -Jsonbuilder.append ("]"); -       returnContent (Jsonbuilder.tostring (),"Json/text"); t"); 

C # Get classes and methods under classes (for ASP. NET MVC)

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.