/// <summary> /// 设置属性值 /// </summary> /// <param name="instance">对象实例</param> /// <param name="propertyString">属性字符串</param> /// <param name="propertyValue">属性值</param> public static void SetObjectProperty(object instance, string propertyString, object propertyValue) { if (propertyString.Length <= 0) { return; } string[] propertys = propertyString.Split('.'); object tempObject = instance; for (int i = 0; i < propertys.Length; i++) { string propName = propertys[i]; Type objectType = tempObject.GetType(); FastPropertyInfo fastProperty = null; if (i == propertys.Length - 1) { fastProperty = GetFastPropertyInfo(objectType, propName); if (null != fastProperty && fastProperty.Property.CanWrite) { fastProperty.Set(tempObject, ObjectHelper.GetObjectFromString(propertyValue.ToString(), fastProperty.Property.PropertyType)); } } else { fastProperty = GetFastPropertyInfo(objectType, propName); if (null != fastProperty) { tempObject = fastProperty.Get(tempObject); } } } }
/// <summary> /// 设置属性值 /// </summary> /// <param name="entity">实体对象</param> /// <param name="propertyName">属性名称</param> /// <param name="propertyValue">属性值</param> public static void SetPropertyValue(object entity, string propertyName, object propertyValue) { FastPropertyInfo fastProerty = GetFastProperty(entity.GetType(), propertyName); if (fastProerty != null && fastProerty.Property != null) { fastProerty.Set(entity, ObjectHelper.ConvertObjectValue(propertyValue, fastProerty.Property.PropertyType)); } }