Example #1
0
        public TResult Map <TSource, TResult>(TSource source) where TResult : new()
        {
            Type       typeSource = typeof(TSource);
            Type       typeResult = typeof(TResult);
            ComplexKey key        = new ComplexKey(typeSource, typeResult);

            Delegate convertFunction;

            if (!_convertFunctions.TryGetValue(key, out convertFunction))
            {
                string text = $"Pair of types '{typeSource.FullName}', '{typeResult.FullName}' not registered. You should register this pair of types by method {nameof(Register)}().";
                throw new Exception(text);
            }

            return(((Func <TSource, TResult>)convertFunction)(source));
        }
Example #2
0
        public void Register <TSource, TResult>(Action <TSource, TResult> postAction = null) where TResult : new()
        {
            Type       typeSource = typeof(TSource);
            Type       typeResult = typeof(TResult);
            ComplexKey key        = new ComplexKey(typeSource, typeResult);

            Func <TSource, TResult> convertFunction = CreateConvertFunction <TSource, TResult>();

            if (postAction != null)
            {
                _convertFunctions[key] = (Func <TSource, TResult>)(source =>
                {
                    TResult result = convertFunction(source);
                    postAction(source, result);
                    return(result);
                });
            }
            else
            {
                _convertFunctions[key] = convertFunction;
            }
        }