/// <summary> /// Sets value by string property name. /// Overrides property value if exists with the same <paramref name="propertyName"/>. /// </summary> /// <param name="propertyContainer">MutablePropertyContainer.</param> /// <param name="propertyName">Property name.</param> /// <param name="value">Value to set.</param> /// <param name="valueSource">Value source.</param> /// <param name="valueType">Value type if value is null.</param> /// <returns><see cref="IPropertyValue"/> that holds value for property.</returns> public static IPropertyValue SetValueUntyped(this IMutablePropertyContainer propertyContainer, string propertyName, object value, ValueSource valueSource = default, Type valueType = null) { IPropertyValue propertyValue = propertyContainer.GetPropertyValueUntyped(Search .ByNameOrAlias(propertyName, true) .SearchInParent(false) .ReturnNull()); if (propertyValue != null) { IProperty existingProperty = propertyValue.PropertyUntyped; if (value != null && value.GetType() != existingProperty.Type) { throw new ArgumentException($"Existing property {existingProperty.Name} has type {existingProperty.Type} but value has type {value.GetType()}"); } if (value == null && existingProperty.Type.IsValueType) { throw new ArgumentException($"Existing property {existingProperty.Name} has type {existingProperty.Type} and null value is not allowed"); } return(propertyContainer.SetValueUntyped(existingProperty, value, valueSource)); } else { if (value == null && valueType == null) { throw new InvalidOperationException($"Unable to define property type for {propertyName} because value is null"); } Type propertyTypeByValue = value?.GetType() ?? valueType; IProperty newProperty = Property.Create(propertyTypeByValue, propertyName); return(propertyContainer.SetValueUntyped(newProperty, value, valueSource)); } }
/// <summary> /// Sets property value and returns the same container. /// </summary> /// <param name="propertyContainer">MutablePropertyContainer.</param> /// <param name="property">Property to set.</param> /// <param name="value">Value to set.</param> /// <param name="valueSource">Value source.</param> /// <returns>The same container with changed property.</returns> public static IMutablePropertyContainer WithValueUntyped(this IMutablePropertyContainer propertyContainer, IProperty property, object?value, ValueSource?valueSource = default) { propertyContainer.AssertArgumentNotNull(nameof(propertyContainer)); property.AssertArgumentNotNull(nameof(property)); propertyContainer.SetValueUntyped(property, value, valueSource); return(propertyContainer); }
/// <summary> /// Sets property value and returns the same container. /// </summary> /// <param name="propertyContainer">MutablePropertyContainer.</param> /// <param name="property">Property to set.</param> /// <param name="value">Value to set.</param> /// <param name="valueSource">Value source.</param> /// <returns>The same container with changed property.</returns> public static IMutablePropertyContainer WithValueUntyped(this IMutablePropertyContainer propertyContainer, IProperty property, object value, ValueSource valueSource = default) { propertyContainer.SetValueUntyped(property, value, valueSource); return(propertyContainer); }