Exemplo n.º 1
0
        /// <summary>
        /// Executes initialization for the action.
        /// </summary>
        /// <param name="action">The action.</param>
        public virtual void ExecuteInitializationForAction(InitializationAction action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            InitializationHandler initializationHandler = action.State as InitializationHandler;

            if (initializationHandler == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Action in not in a proper state. It's 'State' property should be of type '{0}' and should not be null.", typeof(InitializationHandler)));
            }

            Type[] argumentTypes = new Type[initializationHandler.Arguments.Length];

            for (int index = 0; index < initializationHandler.Arguments.Length; ++index)
            {
                argumentTypes[index] = initializationHandler.Arguments[index].GetType();
            }

            ConstructorInfo constructor = initializationHandler.Type.GetConstructor(argumentTypes);

            if (constructor == null)
            {
                constructor = initializationHandler.Type.GetConstructor(new[] { typeof(object[]) });

                if (constructor == null)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Failed to create an instance of '{0}' type. No constructor found that matches the list of parameters.", initializationHandler.Type.AssemblyQualifiedName));
                }

                action.State = constructor.Invoke(new object[] { initializationHandler.Arguments });
            }
            else
            {
                action.State = constructor.Invoke(initializationHandler.Arguments);
            }

            IInitializationContextAware contextAwareInitializer = action.State as IInitializationContextAware;

            if (contextAwareInitializer != null)
            {
                contextAwareInitializer.SetInitializationContext(action.Context);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="InitializationHandlerAttribute"/> class.
 /// </summary>
 /// <param name="initializationHandlerType">The initialization handler type. The initialization handler should perform initialization in its constructor and cleanup in its <see cref="System.IDisposable.Dispose"/> method implementation.</param>
 /// <param name="arguments">The arguments to provide to initialization handler.</param>
 public InitializationHandlerAttribute(Type initializationHandlerType, params object[] arguments)
 {
   this.initializationHandler = new InitializationHandler(initializationHandlerType, arguments);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InitializationHandlerAttribute"/> class.
 /// </summary>
 /// <param name="initializationHandlerType">The initialization handler type. The initialization handler should perform initialization in its constructor and cleanup in its <see cref="System.IDisposable.Dispose"/> method implementation.</param>
 /// <param name="arguments">The arguments to provide to initialization handler.</param>
 public InitializationHandlerAttribute(Type initializationHandlerType, params object[] arguments)
 {
     this.initializationHandler = new InitializationHandler(initializationHandlerType, arguments);
 }