返回自定义类的所有属性名

        /// <summary>
        /// 返回类型的所有属性名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<string, Type> GetPropertyInfoArray<T>()
        {
            Dictionary<string, Type> propNameAndType = new Dictionary<string, Type>();

            List<string> propNames = new List<string>();
            PropertyInfo[] props = null;
            Type t = typeof(T);//获得该类的Type
            object obj = Activator.CreateInstance(t);
            props = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (props != null)
            {
                for (int i = 0; i < props.Length; i++)
                {
                    propNameAndType.Add(props[i].Name, props[i].PropertyType);
                }
            }
            return propNameAndType;
        }

 

posted @ 2023-11-28 16:43  剑小秀  阅读(5)  评论(0编辑  收藏  举报