Example #1
0
        /// <summary>
        /// Obtains an activator for the given <paramref name="context"/>.
        /// </summary>
        /// <param name="context">The <see cref="ITypeActivationContext"/> instance that describes the object to be created.</param>
        /// <returns>A method activator.</returns>
        public static ITypeActivator GetActivator(ITypeActivationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Use the static activator by default
            ITypeActivator currentActivator = _activator;

            // Use the activator attached to the target if it exists
            var host = context.Target as IActivatorHost;

            if (host != null && host.Activator != null)
            {
                currentActivator = host.Activator;
            }

            if (currentActivator != null && currentActivator.CanActivate(context))
            {
                return(currentActivator);
            }

            return(null);
        }