从字符串(string)来获取是否是对象的属性。

来源:互联网 发布:网络舆情的特点包括 编辑:程序博客网 时间:2024/06/05 14:08

很多时候会遇到这样一个问题,就是你想判断一个字符串是否是某个对象的属性,如果是,则返回该属性的值,否则返回空。

        public string GetEntityProperty(string propertyName, object objInit)        {            PropertyInfo info = objInit.GetType().GetProperty(propertyName);            if (info != null)            {                return info.GetValue(objInit, null).ToString();                //return true;            }            else                return null;        }



原创粉丝点击