• C# 反射获取所有视图


    原地址:忘了

    controller 的 action 加上属性 [System.ComponentModel.Description("菜单列表")]  且  返回值为 System.Web.Mvc.ActionResult 类型,就可以获取到了

     [System.ComponentModel.Description("菜单")]
            public ActionResult Index()
            {
                return View(DbContext.Menu.Where(c=>!c.IsDelete).OrderBy(c => c.Sort).ToList());
            }
    View Code
     /// <summary>
            /// 反射 获取方法
            /// </summary>
            /// <returns></returns>
            public string GetMethodByReflect()
            {
                string strHtml = "";
                var asm = System.Reflection.Assembly.GetExecutingAssembly();
                System.Collections.Generic.List<Type> typeList = new List<Type>();
                var types = asm.GetTypes();
                foreach (Type type in types)
                {
                    if (type.FullName.StartsWith("MyMVC.Controllers.") && type.FullName.EndsWith("Controller"))
                    {
                        typeList.Add(type);                   
                    }
                }
                typeList.Sort(delegate (Type type1, Type type2) { return type1.FullName.CompareTo(type2.FullName); });
                foreach (Type type in typeList)
                {
                    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;
    
                        //str += $"{m.Name} - {type.FullName} - {m.DeclaringType.Attributes.ToString()}<br>";
                        MethodInfo method = type.GetMethod(m.Name);
                        if ((method != null) && (method.ReturnType.ToString() == "System.Web.Mvc.ActionResult"))
                        {
                            var DescAttr = (System.ComponentModel.DescriptionAttribute)Attribute.GetCustomAttribute(m, typeof(System.ComponentModel.DescriptionAttribute));
                            if (DescAttr != null)
                            {
                                strHtml += $"{m.Name} - {DescAttr.Description} - {method.ReturnType.ToString()} - <br>";
                            }
                        }
                    }
                }            
                return strHtml;
            }
    View Code
  • 相关阅读:
    Loadrunner系列学习--Loadrunner架构(1)
    Loadrunner学习---脚本编写(1)
    loadrunner学习系列---脚本编写(2)
    LoadRunner学习---脚本编写(4)(比较重要)
    LoadRunner内部结构(1)
    pat 1142
    pat 1025
    pat 1140
    c/c++ 常用函数/方法
    pat 1136
  • 原文地址:https://www.cnblogs.com/guxingy/p/9466948.html
Copyright © 2020-2023  润新知