using System.Collections.Generic; using System.Reflection; namespace CommonTool { public class MvcActionHelper { // ReSharper disable once StaticMemberInitializerReferesToMemberBelow public static MvcActionHelper Instance { get; } = new MvcActionHelper(AssemblyName); public MvcActionHelper(string assemblyName) { AssemblyName = assemblyName; ActionPermissions = GetAllActionByAssembly(assemblyName); } public static string AssemblyName { get; set; } public IList ActionPermissions { get; set; } public IList GetAllActionByAssembly(string assemblyName) { var result = new List(); var types = Assembly.Load(assemblyName).GetTypes(); foreach (var type in types) { if (type.BaseType != null && type.BaseType.Name == "BaseController")//如果是Controller { var members = type.GetMethods(); foreach (var member in members) { if (member.ReturnType.Name == "ActionResult")//如果是Action { if (member.DeclaringType != null) { ActionPermission ap = new ActionPermission { ActionName = member.Name, ControllerName = member.DeclaringType.Name.Substring(0, member.DeclaringType.Name.Length - 10) }; // 去掉“Controller”后缀 object[] attrs = member.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true); if (attrs.Length > 0) { var descriptionAttribute = attrs[0] as System.ComponentModel.DescriptionAttribute; if (descriptionAttribute != null) ap.Description = descriptionAttribute.Description; } result.Add(ap); } } } } } return result; } /// /// 针对MVC的Action的权限 /// public class ActionPermission { /// /// 请求地址 /// public virtual string ActionName { get; set; } /// /// 请求地址 /// public virtual string ControllerName { get; set; } /// /// 描述 /// public virtual string Description { get; set; } ///// ///// 角色列表 ///// //public virtual ISet Roles { get; set; } } } }