/// <summary> /// Set an object's property value using reflection. If <paramref name="value" /> is /// not of the same type as the property then a type conversion is attempted. /// </summary> /// <param name="source">The object to set the property on.</param> /// <param name="pi">Property name.</param> /// <param name="value">Property value.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> /// <exception cref="T:System.ArgumentNullException"><paramref name="pi" /> is null.</exception> /// <exception cref="T:System.InvalidOperationException">Property's type cannot be set to null.</exception> /// <exception cref="T:System.InvalidOperationException">Property is not writable.</exception> public static void SetPropertyValue(this object source, PropertyInfo pi, object value) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (pi == null) { throw new ArgumentNullException(nameof(pi)); } if (!pi.CanWrite) { ExceptionThrower.ThrowPropertyIsNotWriteable(source.GetType(), pi.Name); } if (value == null) { if (pi.PropertyType.IsNullable()) { pi.SetValue(source, null); } else { throw new InvalidOperationException($"Property's type '{pi.PropertyType}' cannot be set to null."); } } else if (pi.PropertyType == value.GetType()) { pi.SetValue(source, value); } else if (pi.PropertyType == typeof(string)) { pi.SetValue(source, value.ToString()); } else if (pi.PropertyType.IsEnum) { pi.SetValue(source, Enum.Parse(pi.PropertyType, value.ToString())); } else { pi.SetValue(source, Convert.ChangeType(value, pi.PropertyType)); } }
/// <summary> /// Set an object's readonly property value using reflection. /// </summary> /// <param name="source">The object to set the property on.</param> /// <param name="propertyName">Property name.</param> /// <param name="value">Property value.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> /// <exception cref="T:System.ArgumentException"><paramref name="propertyName" /> is null or empty.</exception> /// <exception cref="T:System.InvalidOperationException">Property does not exist.</exception> public static void SetPropertyReadOnlyValue(this object source, string propertyName, object value) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentException("Property name is null or empty.", nameof(propertyName)); } var fieldInfo = source.GetType().GetBackingField(propertyName); if (fieldInfo == null) { ExceptionThrower.ThrowPropertyDoesNotExist(source.GetType(), propertyName); } fieldInfo.SetValue(source, value); }
public static PropertyInfo GetPropertyInfoOrThrow(Type source, string propertyName, bool ignoreCase, BindingFlags flags) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentException("Property name is null or empty.", nameof(propertyName)); } var pi = ignoreCase ? source.GetProperty(propertyName, flags | BindingFlags.IgnoreCase) : source.GetProperty(propertyName, flags); if (pi == null) { ExceptionThrower.ThrowPropertyDoesNotExist(source, propertyName); } return(pi); }