예제 #1
0
        private object[] CreateArguments <T>(BindingFlags bindingFlags) where T : class
        {
            var ctor      = constructorSelector.SelectFor(typeof(T), typeMap.Keys.ToArray(), bindingFlags);
            var arguments = ctor.GetParameters().Select(x => GetObjectFor(x.ParameterType)).ToArray();

            return(arguments);
        }
예제 #2
0
        /// <summary>
        /// Constructs an instance from known services. Any dependancies (constructor arguments)
        /// are fulfilled by searching the container or, if not found, automatically generating
        /// mocks.
        /// </summary>
        /// <typeparam name="T">A concrete type</typeparam>
        /// <param name="enablePrivate">When true, private constructors will also be used to
        /// create mocks.</param>
        /// <returns>An instance of T with all constructor arguments derrived from services
        /// setup in the container.</returns>
        public T CreateInstance <T>(bool enablePrivate) where T : class
        {
            var bindingFlags = GetBindingFlags(enablePrivate);
            var arguments    = CreateArguments <T>(bindingFlags);

            try
            {
                var ctor = ConstructorSelector.SelectFor(typeof(T), typeMap.Keys.ToArray(), bindingFlags);
                return((T)ctor.Invoke(arguments));
            }
            catch (TargetInvocationException e)
            {
                ExceptionDispatchInfo.Capture(e.InnerException).Throw();
                throw;  //Not really reachable either way, but I like this better than return default(T)
            }
        }