码迷,mamicode.com
首页 > 其他好文 > 详细

获取一个类指定的属性值

时间:2018-11-24 20:58:25      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:catch   获取   public   property   属性   str   ati   inf   ram   

        /// <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; 
            }
        }

 

获取一个类指定的属性值

标签:catch   获取   public   property   属性   str   ati   inf   ram   

原文地址:https://www.cnblogs.com/zzlblog/p/10013193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!