.NET MVC:在Web应用程序中的计数方法,行动 [英] .NET MVC: Counting Action methods in web application

查看:109
本文介绍了.NET MVC:在Web应用程序中的计数方法,行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经有了一个庞大的ASP.NET MVC 3应用程序。我们需要计算有多少公共方法在类,其中 currentClass是System.Web.Mvc.Controller

We've got a huge ASP.NET MVC 3 application. We need to count how many public methods we have in classes where currentClass is System.Web.Mvc.Controller.

这是符合这一标准将有AwesomeProduct.Web基地命名空间,但是不能保证除此之外什么名称空间这些类的任何类应属于,或如何深度的多层次存在。

Any class that meets this criteria will have a base namespace of 'AwesomeProduct.Web', but there's no guarantee beyond that what namespace these classes may fall under, or how many levels of depth there are.

什么是摸不着头脑的最好方法?

What's the best way to figure this out?

推荐答案

一些反思和LINQ喜欢这个应该这样做。

Some reflection and LINQ like this should do it

var actionCount = typeof(/*Some Controller Type In Your MVC App*/)
                    .Assembly.GetTypes()
                    .Where(t => typeof(Controller).IsAssignableFrom(t))
                    .Where(t => t.Namespace.StartsWith("AwesomeProduct.Web"))
                    .SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
                    .Where(m => typeof(ActionResult).IsAssignableFrom(m.ReturnType))
                    .Where(m => !m.IsAbstract)
                    .Where(m => m.GetCustomAttribute<NonActionAttribute>() == null)
                    .Count();

这并不满足异步控制器操作。

This does not cater for async controller actions.

这篇关于.NET MVC:在Web应用程序中的计数方法,行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆