获取类中属性的值

/// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static object GetValue(this Attendee entity, string name)
        {
            try
            {
                PropertyInfo p = _type.GetProperty(name);
                if (p != null)
                {
                    return p.GetValue(entity);
                }
                return null;
            }
            catch 
            {
                return null;
            }
            
        }

调用:

var attendee = new Attendee(attendeeId, request.BventId, "", attendeeRole: (int)AttendeeRoleEnum.EssayExpert, AttendeeStatus.Unregistered, checkinCode);
var value = attendee.GetValue(fieldName);

 

posted @ 2019-07-12 10:53  月下之神  阅读(741)  评论(0)    收藏  举报