码迷,mamicode.com
首页 > Web开发 > 详细

mvc通过反射获取action方法(适用于权限控制)

时间:2015-08-20 12:57:35      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

public static List<string> GetALLPageByReflection()
{
List<string> actions = new List<string>();
var asm = System.Reflection.Assembly.GetExecutingAssembly();
System.Collections.Generic.List<Type> typeList = new List<Type>();
var types = asm.GetTypes();
foreach (Type type in types)
{
string s = type.FullName.ToLower();
if (type.Name.StartsWith("AccountCont")) continue;
if (s.StartsWith("haoliangcaptial.controllers.admin.") && s.EndsWith("controller"))
typeList.Add(type);
}
typeList.Sort(delegate(Type type1, Type type2) { return type1.FullName.CompareTo(type2.FullName); });
foreach (Type type in typeList)
{
//Response.Write(type.Name.Replace("Controller","") + "<br/>\n");
System.Reflection.MemberInfo[] members = type.FindMembers(System.Reflection.MemberTypes.Method,
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.NonPublic | //【位屏蔽】
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.DeclaredOnly,
Type.FilterName, "*");
foreach (var m in members)
{
if (m.DeclaringType.Attributes.HasFlag(System.Reflection.TypeAttributes.Public) != true)
continue;
string controller = type.Name.Replace("Controller", "");
string action = m.Name;
string url = "/" + controller + "/" + action;
if (!action.Contains("<"))
{
actions.Add(url);
}
}
}
return actions;
}

来自小聪ssc

mvc通过反射获取action方法(适用于权限控制)

标签:

原文地址:http://www.cnblogs.com/jianghaidong/p/4744495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!