/// <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 if property is not set. /// </summary> /// <typeparam name="T">Property type.</typeparam> /// <param name="propertyContainer">Property container.</param> /// <param name="property">Property to set.</param> /// <param name="value">Value to set.</param> public static void SetValueIfNotSet <T>(this IMutablePropertyContainer propertyContainer, IProperty <T> property, [AllowNull] T value) { propertyContainer.AssertArgumentNotNull(nameof(propertyContainer)); property.AssertArgumentNotNull(nameof(property)); var propertyValue = propertyContainer.GetPropertyValueUntyped(property, SearchOptions.ExistingOnly); if (propertyValue.IsNullOrNotDefined()) { propertyContainer.SetValue(property, value !); } }
/// <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 = null, Type?valueType = null) { propertyContainer.AssertArgumentNotNull(nameof(propertyContainer)); propertyName.AssertArgumentNotNull(nameof(propertyName)); 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 value by string property name. /// Overrides property value if exists with the same <paramref name="propertyName"/>. /// </summary> /// <typeparam name="T">Property type.</typeparam> /// <param name="propertyContainer">MutablePropertyContainer.</param> /// <param name="propertyName">Property name.</param> /// <param name="value">Value to set.</param> /// <param name="valueSource">Value source.</param> /// <returns><see cref="IPropertyValue{T}"/> that holds value for property.</returns> public static IPropertyValue <T> SetValue <T>(this IMutablePropertyContainer propertyContainer, string propertyName, T value, ValueSource?valueSource = default) { propertyContainer.AssertArgumentNotNull(nameof(propertyContainer)); propertyName.AssertArgumentNotNull(nameof(propertyName)); IPropertyValue?propertyValue = propertyContainer.GetPropertyValueUntyped(Search .ByNameOrAlias <T>(propertyName, true) .SearchInParent(false) .ReturnNull()); if (propertyValue != null) { Type valueType = typeof(T); IProperty existingProperty = propertyValue.PropertyUntyped; if (existingProperty.Type != valueType) { throw new ArgumentException($"Existing property {existingProperty.Name} has type {existingProperty.Type} but value has type {valueType}"); } return(propertyContainer.SetValue((IProperty <T>)existingProperty, value, valueSource)); } return(propertyContainer.SetValue(new Property <T>(propertyName), value, valueSource)); }