public void SourceBaseType()
        {
            var registry = new RegistryTypeConverter();

            registry.Register <Enum, string>(LambdaTypeConverter.Create(x => x.ToString()));

            var result = registry.ConvertTo <string>(TypeConversionContext.None, TestEnum.Value1);

            Assert.AreEqual("Value1", result);
        }
        public void TargetBaseType()
        {
            var registry = new RegistryTypeConverter();

            registry.Register <string, Enum>(LambdaTypeConverter.Create((x, convertTo) => Enum.Parse(convertTo, (string)x)));

            var result = registry.ConvertTo <TestEnum>(TypeConversionContext.None, "Value1");

            Assert.AreEqual(TestEnum.Value1, result);
        }
        public void DirectConversion()
        {
            var registry = new RegistryTypeConverter();

            registry.Register(typeof(string), typeof(int), LambdaTypeConverter.Create(x => int.Parse((string)x)));

            var result = registry.ConvertTo <int>(TypeConversionContext.None, "5");

            Assert.AreEqual(5, result);
        }
        public void ArrayToBaseElementType()
        {
            var registry = new RegistryTypeConverter();

            registry.Register <Array, string[]>(LambdaTypeConverter.Create(value => ((Array)value).Cast <object>().Select(x => x.ToString()).ToArray()));

            var result = registry.ConvertTo <string[]>(TypeConversionContext.None, new[] { 2, 3 });

            Assert.AreEqual("2", result[0]);
            Assert.AreEqual("3", result[1]);
        }
Exemple #5
0
        public void FirstChoice()
        {
            var typeConverter = new CombinedTypeConverter(
                LambdaTypeConverter.Create(x =>
            {
                int result;
                return(new TypeConverterResult <int>(int.TryParse((string)x, out result), result));
            }),
                LambdaTypeConverter.Create(x =>
            {
                double result;
                return(new TypeConverterResult <double>(double.TryParse((string)x, out result), result));
            }));

            var value = typeConverter.ConvertTo <int>(TypeConversionContext.None, "5");

            Assert.AreEqual(5, value);
        }