/// <summary>
        /// Update the datasource with the binding value.
        /// If the BindingDefinition to update is readonly, then return the currently loaded value
        /// Else return the value passed as a parameter.
        /// </summary>
        public override object UpdateDataSource(object dataSource, object data)
        {
            try
            {
                if (dataSource == null)
                {
                    return(null);
                }

                if (!IsReadOnly)
                {
                    Type type = BindingType;
                    if (data == null)
                    {
                        FieldInfo.SetValue(FieldInfo.IsStatic ? null : dataSource, type.IsValueType ? Activator.CreateInstance(type) : null);
                    }
                    else
                    {
                        data = SpecificConvertors.TryConvert(this, data);
                        FieldInfo.SetValue(FieldInfo.IsStatic ? null : dataSource, data);
                    }
                }
                return(ResolveBinding(dataSource));
            }
            catch (Exception ex)
            {
                log.LogFormat(LogType.Warn, "'UpdateDataSource' failed for BindingExpression '{0}', value '{1}': {2}", BindingExpression, data?.ToString() ?? string.Empty, ex.Message);
                return(ResolveBinding(dataSource));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Update the datasource.
        /// </summary>
        /// <param name="datasource">the Binding datasource to update.</param>
        /// <param name="data">the data to update the datasource.</param>
        /// <returns>
        /// If the BindingDefinition to update is readonly, then return the currently loaded value
        /// Else return the value passed as a parameter.
        /// </returns>
        public override object UpdateDataSource(object datasource, object data)
        {
            try
            {
                if (datasource == null)
                {
                    return(null);
                }

                if (!IsReadOnly)
                {
                    Type type = BindingType;
                    if (data == null)
                    {
                        SetMethod.Invoke(SetMethod.IsStatic ? null : datasource, new object[] { type.IsValueType?Activator.CreateInstance(type) : null });
                    }
                    else
                    {
                        data = SpecificConvertors.TryConvert(this, data);
                        SetMethod.Invoke(SetMethod.IsStatic ? null : datasource, new object[] { data });
                    }
                }
                object value = ResolveBinding(datasource);
                return(value is Enum ? ((Enum)value).ToString() : value);
            }
            catch (Exception ex)
            {
                log.LogFormat(LogType.Warn, "'UpdateDataSource' failed for BindingExpression '{0}', value '{1}': {2}", BindingExpression, data == null ? string.Empty: data.ToString(), ex.Message);
                return(ResolveBinding(datasource));
            }
        }