示例#1
0
        /// <summary>
        /// Converts enum to its map value.
        /// </summary>
        /// <param name="value">Value to convert.</param>
        /// <returns>Mapped value.</returns>
        public object EnumToValue([NotNull] Enum value)
        {
            Code.NotNull(value, nameof(value));
            var toType = ConvertBuilder.GetDefaultMappingFromEnumType(this, value.GetType());

            return(Converter.ChangeType(value, toType, this));
        }
示例#2
0
		public void ConvertNullableEnum()
		{
			var schema = new MappingSchema("2");
			Assert.That(schema.GetDefaultValue(typeof(Enum1?)), Is.Null);
			var mapType = ConvertBuilder.GetDefaultMappingFromEnumType(schema, typeof(Enum1?));
			Assert.That(mapType, Is.EqualTo(typeof(int?)));
			var convertedValue = Converter.ChangeType(null, mapType, schema);
			Assert.IsNull(convertedValue);
		}
示例#3
0
        private static void Init()
        {
            var expr = ConvertBuilder.GetConverter(null, typeof(TFrom), typeof(TTo));

            _expression = (Expression <Func <TFrom, TTo> >)expr.Item1;

            var rexpr = (Expression <Func <TFrom, TTo> >)expr.Item1.Transform(e => e is DefaultValueExpression ? e.Reduce() : e);

            _lambda = rexpr.Compile();
        }
示例#4
0
        public LambdaInfo Create([CanBeNull] MappingSchema mappingSchema, [NotNull] Type from, [NotNull] Type to)
        {
            var ex  = ConvertBuilder.GetConverter(mappingSchema, from, to);
            var lm  = ex.Item1.Compile();
            var ret = new LambdaInfo(ex.Item1, ex.Item2, lm, ex.Item3);

            Set(_expressions, from, to, ret);

            return(ret);
        }
示例#5
0
        /// <summary>
        /// Converts enum to its map value.
        /// </summary>
        /// <param name="value">Value to convert.</param>
        /// <returns>Mapped value.</returns>
        public object EnumToValue(Enum value)
        {
            var toType = ConvertBuilder.GetDefaultMappingFromEnumType(this, value.GetType());

            return(Converter.ChangeType(value, toType, this));
        }