获取一个类指定的属性值

        /// <summary>
        /// 获取一个类指定的属性值
        /// </summary>
        /// <param name="info">object对象</param>
        /// <param name="field">属性名称</param>
        /// <returns></returns>
        public static object GetPropertyValue(object info, string field)
        {
            try
            {
                if (info == null) return null;
                Type t = info.GetType();
                IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
                return property.First().GetValue(info, null);
            }
            catch (Exception ex)
            {
                string msg = ex.Message.ToString();
                return null; 
            }
        }

 

posted on 2018-11-24 19:41  粗狂的_蜗牛  阅读(746)  评论(0编辑  收藏  举报