| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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<ActionPermission> ActionPermissions { get; set; }
- public IList<ActionPermission> GetAllActionByAssembly(string assemblyName)
- {
- var result = new List<ActionPermission>();
- 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;
- }
- /// <summary>
- /// 针对MVC的Action的权限
- /// </summary>
- public class ActionPermission
- {
- /// <summary>
- /// 请求地址
- /// </summary>
- public virtual string ActionName { get; set; }
- /// <summary>
- /// 请求地址
- /// </summary>
- public virtual string ControllerName { get; set; }
- /// <summary>
- /// 描述
- /// </summary>
- public virtual string Description { get; set; }
- ///// <summary>
- ///// 角色列表
- ///// </summary>
- //public virtual ISet<Role> Roles { get; set; }
- }
- }
- }
|