/// <summary>
        /// 获取Object的属性值,2013-9-1 11:15:55,Xihan
        /// </summary>
        /// <param name="GetObject">要获取属性值的object</param>
        /// <param name="Property">要获取的属性</param>
        /// <returns>属性值</returns>
        public static object getPropertyValue(this object GetObject, string Property)
        {
            object PropertyResult = null;
            try
            {
                string[] Propertys = Property.Split('.');//获取属性层级
                int iCount = 0;
                object objectProperty = GetObject;//设置对象
                var type = GetObject.GetType();//设置对象类型
                PropertyInfo ProInfo;//属性类型
                foreach (string Proper in Propertys)
                {
                    ProInfo = type.GetProperty(Proper);//获取要设置的属性
                    iCount++;
                    if (ProInfo != null)
                    {
                        objectProperty = ProInfo.GetValue(objectProperty, null);//获取属性值
                        if (iCount == Propertys.Length)
                        {
                            PropertyResult = objectProperty;
                            break;
                        }
                        if (objectProperty == null) { break; }//属性值获取失败不再设置
                        type = objectProperty.GetType();//获取属性值的类型,准备进入下一层及设置
                    }
                    else
                    {
                        break;//属性获取失败就不再做设置
                    }
                }
                return PropertyResult;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 为object的属性设置值,2013-9-1 11:11:06,Xihan
        /// </summary>
        /// <param name="SetObject">要设置属性值的object</param>
        /// <param name="Property">要设置的属性</param>
        /// <param name="setValue">属性值</param>
        /// <returns>是否设置成功</returns>
        public static bool SetPropertyValue(this object SetObject, string Property, object setValue)
        {
            try
            {
                bool bResult = false;
                string[] Propertys = Property.Split('.');//获取属性层级
                int iCount = 0;
                object objectProperty = SetObject;//设置对象
                var type = SetObject.GetType();//设置对象类型
                PropertyInfo ProInfo;//属性类型

                foreach (string Proper in Propertys)
                {
                    ProInfo = type.GetProperty(Proper);//获取要设置的属性
                    iCount++;
                    if (ProInfo != null)
                    {
                        //检查是否到达要设置的层级
                        if (iCount == Propertys.Length)
                        {
                            ProInfo.SetValue(objectProperty, setValue, null);//设置要设置的属性
                            bResult = true;
                            break;
                        }
                        else
                        {
                            objectProperty = ProInfo.GetValue(objectProperty, null);//获取属性值
                            if (objectProperty == null) { break; }//属性值获取失败不再设置
                            type = objectProperty.GetType();//获取属性值的类型,准备进入下一层及设置
                        }
                    }
                    else
                    {
                        break;//属性获取失败就不再做设置
                    }
                }
                return bResult;
            }
            catch
            {
                return false;
            }
        }

posted on 2014-10-08 13:22  nicvscs  阅读(887)  评论(0)    收藏  举报