极客神殿

坚持,自由,信仰

ASP.NET MVC 反射例子

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>

运行结果如图:

这里写图片描述

阅读更多
个人分类: ASP.NET MVC4|MVC5
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭