Beispiel #1
0
        public override K Convert <T, K>(T value)
        {
            Type inputType  = typeof(T);
            Type outputType = typeof(K);

            Converter converter = null;

            foreach (var itemConverter in this)
            {
                if (itemConverter.From == inputType && itemConverter.To == outputType)
                {
                    converter = itemConverter;
                    break;
                }
            }

            if (converter != null)
            {
                return((K)converter.Convert(value));
            }

            if (UseDefaultConverter)
            {
                if (inputType != outputType)
                {
                    throw new InvalidMapException(string.Format("No mapping function was found to map {0} to {1} and can no use defaul mapping with different types", inputType.Name, outputType.Name));
                }

                DefaultConverter defaultConverter = new DefaultConverter(inputType);
                return((K)defaultConverter.Convert(value));
            }

            //no mapping function
            throw new InvalidMapException(string.Format("No mapping function was found to map {0} to {1}.", inputType.Name, outputType.Name));
        }
        public override K Convert <T, K>(T value)
        {
            if (typeof(T) != typeof(K))
            {
                throw new InvalidMapException(string.Format("The argument type {0} and the result type {1} should match for DefaultConverterResolver", typeof(T), typeof(K)));
            }

            Converter converter = new DefaultConverter(typeof(T));

            K result = (K)converter.Convert(value);

            return(result);
        }