.NET MVC:计算Web应用程序中的Action方法

cor*_*ore 4 asp.net-mvc asp.net-mvc-3

我们有一个巨大的ASP.NET MVC 3应用程序.我们需要计算我们在课程中有多少公共方法currentClass is System.Web.Mvc.Controller.

任何符合此条件的类都将具有"AwesomeProduct.Web"的基本命名空间,但除了这些类可能属于哪个命名空间或者有多少深度级别之外,无法保证.

想出这个的最佳方法是什么?

Rus*_*Cam 10

像这样的一些反射和LINQ应该这样做

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();
Run Code Online (Sandbox Code Playgroud)

这不适合异步控制器操作.