Exemple #1
0
        /// <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));
            }
        }
Exemple #2
0
        /// <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, T value)
        {
            var propertyValue = propertyContainer.GetPropertyValueUntyped(property, SearchOptions.ExistingOnly);

            if (propertyValue.IsNullOrNotDefined())
            {
                propertyContainer.SetValue(property, value);
            }
        }
        /// <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 !);
            }
        }
Exemple #4
0
        /// <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)
        {
            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));
        }