Exemple #1
0
        /// <summary>
        /// Returns an object of a specified type whose value is equivalent to a specified object.
        /// </summary>
        /// <param name="value">An object to convert.</param>
        /// <param name="conversionType">The type of object to return.</param>
        /// <param name="mappingSchema">A mapping schema that defines custom converters.</param>
        /// <returns>An object whose type is <i>conversionType</i> and whose value is equivalent to <i>value</i>.</returns>
        public static object ChangeType([CanBeNull] object value, [NotNull] Type conversionType, MappingSchema mappingSchema = null)
        {
            Code.NotNull(conversionType, nameof(conversionType));

            if (value == null || value is DBNull)
            {
                return(mappingSchema == null?
                       DefaultValue.GetValue(conversionType) :
                           mappingSchema.GetDefaultValue(conversionType));
            }

            if (value.GetType() == conversionType)
            {
                return(value);
            }

            var from = value.GetType();
            var to   = conversionType;
            var key  = new { from, to };

            var converters = mappingSchema == null ? _converters : mappingSchema.Converters;

            if (!converters.TryGetValue(key, out var l))
            {
                var li =
                    ConvertInfo.Default.Get(value.GetType(), to) ??
                    ConvertInfo.Default.Create(mappingSchema, value.GetType(), to);

                var b  = li.CheckNullLambda.Body;
                var ps = li.CheckNullLambda.Parameters;

                var p  = Expression.Parameter(typeof(object), "p");
                var ex = Expression.Lambda <Func <object, object> >(
                    Expression.Convert(
                        b.Transform(e =>
                                    e == ps[0] ?
                                    Expression.Convert(p, e.Type) :
                                    IsDefaultValuePlaceHolder(e) ?
                                    new DefaultValueExpression(mappingSchema, e.Type) :
                                    e),
                        typeof(object)),
                    p);

                l = ex.Compile();

                converters[key] = l;
            }

            return(l(value));
        }
Exemple #2
0
        public object GetDefaultValue([NotNull] Type type)
        {
            Code.NotNull(type, nameof(type));

            foreach (var info in Schemas)
            {
                var o = info.GetDefaultValue(type);
                if (o.HasValue)
                {
                    return(o.Value);
                }
            }

            if (type.IsEnum)
            {
                var mapValues = GetMapValues(type);

                if (mapValues != null)
                {
                    var fields =
                        from f in mapValues
                        where f.MapValues.Any(a => a.Value == null)
                        select f.OrigValue;

                    var value = fields.FirstOrDefault();

                    if (value != null)
                    {
                        SetDefaultValue(type, value);
                        return(value);
                    }
                }
            }

            return(DefaultValue.GetValue(type, this));
        }
Exemple #3
0
 public override Expression Reduce()
 => Constant(
     _mappingSchema == null ?
     DefaultValue.GetValue(Type) :
     _mappingSchema.GetDefaultValue(Type),
     Type);