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")]
publicclass 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")]
publicclass testController : Controller
{
[Describe(Describe = "這是Index", Name = "Index")]
// GET: testpublic ActionResult Index()
{
return View();
}
}
}
DescribeAttribute.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
namespace reflection
{
publicclass DescribeAttribute:Attribute
{
publicstring Name;
publicstring Describe;
}
publicstaticclass Describe {
publicstatic DescribeAttribute GetAtrribute(Type a) {
var ab = a.GetCustomAttributes(false).Where(p => p.GetType() == typeof(DescribeAttribute)).ToList();
return ab.FirstOrDefault() as DescribeAttribute;
}
publicstatic DescribeAttribute GetAtrribute(MethodInfo a)
{
var ab = a.GetCustomAttributes(false).Where(p => p.GetType() == typeof(DescribeAttribute)).ToList();
return ab.FirstOrDefault() as DescribeAttribute;
}
publicstatic DescribeAttribute GetAtrribute(ParameterInfo a)
{
var ab = a.GetCustomAttributes(false).Where(p => p.GetType() == typeof(DescribeAttribute)).ToList();
return ab.FirstOrDefault() as DescribeAttribute;
}
}
}