zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    【Demo 0011】多媒体播放器
    【Demo 0010】事件响应链
    【Demo 0009】表视图控制器
    【Demo 0008】标签控制器
    【Demo 0007】导航控制器
    【Demo 0006】iOS常用控件
    【Demo 0005】视图控制器
    【Demo 0004】屏幕、窗体及视图基础知识
    2019.7.16考试反思
    内网 可怜与超市题解 树形dp+合并
  • 原文地址:https://www.cnblogs.com/guxingy/p/9466948.html
Copyright © 2011-2022 走看看