ASP.NET MVC 反射例子

風靈使發表於2018-06-05

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace reflection.Controllers
{
    [Describe(Describe = "這是home控制器", Name = "HomeController")]
    public class HomeController : Controller
    {
        [Describe(Describe = "這是About頁面", Name = "Index")]
        public ActionResult Index()
        {
            return View();
        }
        [Describe(Describe="這是About頁面",Name = "About")]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }
        [Describe(Describe = "這是Contact頁面", Name = "Contact")]
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        [Describe(Describe = "這是Reflection頁面", Name = "Reflection")]
        public ActionResult Reflection() {
            IEnumerable<Type> ts = Assembly.GetExecutingAssembly().GetTypes().Where(p => p.BaseType == typeof(Controller));

            return View(ts);
        }
    }
}

testController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace reflection.Controllers
{
    [Describe(Describe = "這是testController", Name = "testController")]

    public class testController : Controller
    {
        [Describe(Describe = "這是Index", Name = "Index")]

        // GET: test
        public ActionResult Index()
        {
            return View();
        }
    }
}

DescribeAttribute.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace reflection
{
    public class DescribeAttribute:Attribute
    {
        public string Name;
        public string Describe;
    }
    public static class Describe {
        public static DescribeAttribute GetAtrribute(Type a) {
            var ab = a.GetCustomAttributes(false).Where(p => p.GetType() == typeof(DescribeAttribute)).ToList();
            return ab.FirstOrDefault() as DescribeAttribute;
        }
        public static DescribeAttribute GetAtrribute(MethodInfo a)
        {
            var ab = a.GetCustomAttributes(false).Where(p => p.GetType() == typeof(DescribeAttribute)).ToList();
            return ab.FirstOrDefault() as DescribeAttribute;
        }
        public static DescribeAttribute GetAtrribute(ParameterInfo a)
        {
            var ab = a.GetCustomAttributes(false).Where(p => p.GetType() == typeof(DescribeAttribute)).ToList();
            return ab.FirstOrDefault() as DescribeAttribute;
        }
    }
}

Reflection.cshtml


@{
    ViewBag.Title = "Reflection";
}
@model IEnumerable<Type>
<h2>Reflection</h2>

<div class="panel panel-primary">

    <div class="list-group">
        @{
            foreach (var item in Model)
            {
                var des = item.GetCustomAttributes(false).Where(p => p.GetType() == typeof(DescribeAttribute));


                foreach (DescribeAttribute attr in des)
                {

                    <h4 class="list-group-item-text">@attr.Name</h4>
                        <p>@attr.Describe</p>

                }
                <ol>
                    @foreach (var method in item.GetMethods().Where(p => p.Module.Name == item.Module.Name && p.IsPublic))
                    {
                        <li>
                            @foreach (DescribeAttribute attr in method.GetCustomAttributes(false).Where(p => p.GetType() == typeof(DescribeAttribute)))
                            {
                                <h4 class="list-group-item-text">@attr.Name</h4>
                                <p>@attr.Describe</p>
                            }
                    </li>
                    }
                </ol>

                <hr />
            }
        }

    </div>

</div>

執行結果如圖:

這裡寫圖片描述

相關文章