/// <summary>
        /// Invoke the constructor with the parameter bindings.
        /// </summary>
        /// <returns>The constructed instance.</returns>
        public object Instantiate()
        {
            if (!CanInstantiate)
            {
                throw new InvalidOperationException(ConstructorParameterBindingResources.CannotInstantitate);
            }

            var values = new object[_valueRetrievers.Length];

            for (var i = 0; i < _valueRetrievers.Length; ++i)
            {
                values[i] = _valueRetrievers[i].Invoke();
            }

#if !(SILVERLIGHT || NET35)
            ConstructorInvoker constructorInvoker;
            if (!_constructorInvokers.TryGetValue(TargetConstructor, out constructorInvoker))
            {
                constructorInvoker = GetConstructorInvoker(TargetConstructor);
                _constructorInvokers.TryAdd(TargetConstructor, constructorInvoker);
            }
#endif

            try
            {
#if !(SILVERLIGHT || NET35)
                return(constructorInvoker(values));
#else
                return(TargetConstructor.Invoke(values));
#endif
            }
            catch (TargetInvocationException ex)
            {
                throw new DependencyResolutionException(
                          string.Format(ConstructorParameterBindingResources.ExceptionDuringInstantiation, TargetConstructor, TargetConstructor.DeclaringType.Name), ex.InnerException);
            }
            catch (Exception ex)
            {
                throw new DependencyResolutionException(
                          string.Format(ConstructorParameterBindingResources.ExceptionDuringInstantiation, TargetConstructor, TargetConstructor.DeclaringType.Name), ex);
            }
        }
Example #2
0
 /// <summary>
 /// Define a custom constructor for the specified type
 /// </summary>
 /// <typeparam name="T">Type for which constructor is defining</typeparam>
 /// <param name="constructor">Custom constructor</param>
 /// <returns></returns>
 public TDerived ConstructBy <T>(TargetConstructor <T> constructor)
 {
     return((TDerived)base.ConstructBy <T>(constructor));
 }
 /// <summary>
 /// Define a custom constructor for the specified type
 /// </summary>
 /// <typeparam name="T">Type for which constructor is defining</typeparam>
 /// <param name="constructor">Custom constructor</param>
 /// <returns></returns>
 public IMappingConfigurator ConstructBy <T>(TargetConstructor <T> constructor)
 {
     _customConstructors.Add(new[] { typeof(T) }, constructor);
     return(this);
 }
Example #4
0
        public MappingStrategy(MappingInfo mappingInfo, IMappingDescriptor descriptor)
        {
            Descriptor        = descriptor;
            Source            = mappingInfo.MappingSourceType;
            Target            = mappingInfo.MappingTargetType;
            HasTargetInstance = mappingInfo.MapIntoExistingTargetInstance;
            try
            {
                TargetConstructor = Target.GetConstructors().Single();
                ConstructorParameterMappingSteps = new OrderedKeyedCollection <ParameterInfo, MappingStep>(TargetConstructor.GetParameters());
            }
            catch (InvalidOperationException)
            {
                throw new ArgumentException("Target type must have single public constructor. This is the only scenario supported at the moment.", "target");
            }

            ContextExpression = Expression.Parameter(typeof(MappingContext), "context");
            SourceExpression  = Expression.Variable(Source, "source");
            TargetExpression  = Expression.Variable(Target, "target");
            MapperExpression  = Expression.Property(ContextExpression, MappingContextMeta.Mapper);
        }