Asp.NetMVCで反射によりControllerに関する情報を取得

5322 ワード

ASP.NET MVCの开発では、通常、Controller、Actionがたくさんあります.私たちは外から反射して、Linqの文法でいくつかの情报を得ることができます.Linq文法はコードの可読性を高めます.UnitTestを见てください.
    /// <summary>
    /// Tests the get info from controller.
    /// </summary>
    /// <remarks>http://wintersun.cnblogs.com </remarks>
    [TestMethod]
    public void TestGetInfoFromController()
    {
        var controllers =
from t in GetAllControllerTypes()
where typeof(Controller).IsAssignableFrom(t) && !t.IsAbstract
orderby t.FullName
from m in t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
where !m.IsSpecialName
select new { ControllerName = FormatControllerName(t.FullName), ActionName = m.Name, Params = m.GetParameters() };


        controllers.ToList().ForEach(c => Debug.WriteLine(string.Format("Controller: {0}, Action: {1}({2})",
                                                                        c.ControllerName, c.ActionName,
                                                                        string.Join(", ",
                                                                                    c.Params.Select(p => p.Name).
                                                                                        ToArray()))));
        Debug.WriteLine(string.Format("Controller/action count: {0}", controllers.Count()));
        Debug.WriteLine(string.Format("Controller count: {0}", controllers.GroupBy(c => c.ControllerName).Count()));
      
    }

    /// <summary>
    /// Gets all controller types.
    /// </summary>
    /// <returns>all types in an assembly where my controllers can be found</returns>
    private static Type[] GetAllControllerTypes()
    {
        return typeof(ProductManageController).Assembly.GetTypes();
    }

    /// <summary>
    /// Formats the name of the controller,remove all of the namespace information from the controller names
    /// </summary>
    /// <param name="typeName">Name of the type.</param>
    /// <returns></returns>
    private static string FormatControllerName(string typeName)
    {
        return typeName.Replace("Demo1Web.", string.Empty).Replace("Controllers.", string.Empty);
    }

上のコードが出力されます.
Controller: HomeController, Action: Index()Controller: HomeController, Action: test()Controller: HomeController, Action: ThisActionHasProblem()Controller: HomeController, Action: Category(form)Controller: ProductManageController, Action: Delete(ProductId)Controller: ProductManageController, Action: DeleteSome(form)Controller: ProductManageController, Action: EditProduct(Id)Controller: ProductManageController, Action: ProductList(id)Controller: ProductManageController, Action: QueryAllProducts(id, form)Controller: ProductManageController, Action: SaveByBinder(productId, product)Controller: ProductManageController, Action: Save(ProductId, form)Controller: ProductManageController, Action: ViewProduct(Id)Controller/action count: 12Controller count: 2
1 passed, 0 failed, 0 skipped, took 0.97 seconds (NUnit_VSTS).
はっきりしたリスト、おもしろいでしょう.
 
Author: Petter Liu    http://wintersun.cnblogs.com