Ejemplo n.º 1
0
        static object?SetProperty <T>(T target, PropertyMetadata property, object?value, OrdinalMappedProperty <T>?mapper, MaterializerTypeConverter converter)
            where T : class
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target), $"{nameof(target)} is null.");
            }

            if (property == null)
            {
                throw new ArgumentNullException(nameof(property), $"{nameof(property)} is null.");
            }

            var targetType = property.PropertyType;

            if (value != null && targetType != value.GetType())
            {
                if (!converter.TryConvertType(targetType, ref value, out var conversionException))
                {
                    throw new MappingException($"Cannot map value of type {value!.GetType().FullName} to property {property.Name} of type {targetType.Name}.", conversionException);
                }
            }

            if (mapper == null || value == null)
            {
                property.InvokeSet(target, value);
            }
            else
            {
                mapper.InvokeSet(target, value);
            }

            return(value);
        }
Ejemplo n.º 2
0
        private static object SetProperty <T>(T target, PropertyMetadata property, object value, OrdinalMappedProperty <T> mapper)
        {
            var targetType = property.PropertyType;

            if (value != null && targetType != value.GetType())
            {
                var targetTypeInfo = targetType.GetTypeInfo();
                //var isNullable = !targetTypeInfo.IsValueType;

                //For Nullable<T>, we only care about the type parameter
                if (targetType.Name == "Nullable`1" && targetTypeInfo.IsGenericType)
                {
                    //isNullable = true;
                    targetType     = targetType.GenericTypeArguments[0];
                    targetTypeInfo = targetType.GetTypeInfo();
                }

                //some database return strings when we want strong types
                if (value is string)
                {
                    if (targetType == typeof(XElement))
                    {
                        value = XElement.Parse((string)value);
                    }
                    else if (targetType == typeof(XDocument))
                    {
                        value = XDocument.Parse((string)value);
                    }
                    else if (targetTypeInfo.IsEnum)
                    {
                        value = Enum.Parse(targetType, (string)value);
                    }

                    else if (targetType == typeof(bool))
                    {
                        value = bool.Parse((string)value);
                    }
                    else if (targetType == typeof(short))
                    {
                        value = short.Parse((string)value);
                    }
                    else if (targetType == typeof(int))
                    {
                        value = int.Parse((string)value);
                    }
                    else if (targetType == typeof(long))
                    {
                        value = long.Parse((string)value);
                    }
                    else if (targetType == typeof(float))
                    {
                        value = float.Parse((string)value);
                    }
                    else if (targetType == typeof(double))
                    {
                        value = double.Parse((string)value);
                    }
                    else if (targetType == typeof(decimal))
                    {
                        value = decimal.Parse((string)value);
                    }

                    else if (targetType == typeof(DateTime))
                    {
                        value = DateTime.Parse((string)value);
                    }
                    else if (targetType == typeof(DateTimeOffset))
                    {
                        value = DateTimeOffset.Parse((string)value);
                    }
                }
                else
                {
                    if (targetTypeInfo.IsEnum)
                    {
                        value = Enum.ToObject(targetType, value);
                    }
                }

                //this will handle numeric conversions
                if (value != null && targetType != value.GetType())
                {
                    try
                    {
                        value = Convert.ChangeType(value, targetType);
                    }
                    catch (Exception ex)
                    {
                        throw new MappingException($"Cannot map value of type {value.GetType().FullName} to property {property.Name} of type {targetType.Name}.", ex);
                    }
                }
            }

            if (mapper == null || value == null)
            {
                property.InvokeSet(target, value);
            }
            else
            {
                mapper.InvokeSet(target, value);
            }

            return(value);
        }