示例#1
0
 /// <summary>
 /// Attempt to automatically register all non-generic classes and interfaces in the current app domain.
 /// Types will only be registered if they pass the supplied registration predicate.
 /// </summary>
 /// <param name="duplicateAction">What action to take when encountering duplicate implementations of an interface/base class.</param>
 /// <param name="registrationPredicate">Predicate to determine if a particular type should be registered.</param>
 public void AutoRegister(
     DependencyContainerDuplicateImplementationAction duplicateAction =
     DependencyContainerDuplicateImplementationAction.RegisterSingle,
     Func <Type, bool>?registrationPredicate = null)
 {
     AutoRegister(
         AppDomain.CurrentDomain.GetAssemblies().Where(a => !IsIgnoredAssembly(a)),
         duplicateAction,
         registrationPredicate);
 }
示例#2
0
        /// <summary>
        /// Attempt to automatically register all non-generic classes and interfaces in the specified assemblies
        /// Types will only be registered if they pass the supplied registration predicate.
        /// </summary>
        /// <param name="assemblies">Assemblies to process.</param>
        /// <param name="duplicateAction">What action to take when encountering duplicate implementations of an interface/base class.</param>
        /// <param name="registrationPredicate">Predicate to determine if a particular type should be registered.</param>
        public void AutoRegister(
            IEnumerable <Assembly> assemblies,
            DependencyContainerDuplicateImplementationAction duplicateAction =
            DependencyContainerDuplicateImplementationAction.RegisterSingle,
            Func <Type, bool>?registrationPredicate = null)
        {
            lock (_autoRegisterLock)
            {
                var types = assemblies
                            .SelectMany(a => a.GetAllTypes())
                            .Where(t => !IsIgnoredType(t, registrationPredicate))
                            .ToList();

                var concreteTypes = types
                                    .Where(type =>
                                           type.IsClass && !type.IsAbstract &&
                                           (type != GetType() && (type.DeclaringType != GetType()) && !type.IsGenericTypeDefinition))
                                    .ToList();

                foreach (var type in concreteTypes)
                {
                    try
                    {
                        RegisteredTypes.Register(type, string.Empty, GetDefaultObjectFactory(type, type));
                    }
                    catch (MethodAccessException)
                    {
                        // Ignore methods we can't access - added for Silverlight
                    }
                }

                var abstractInterfaceTypes = types.Where(
                    type =>
                    ((type.IsInterface || type.IsAbstract) && (type.DeclaringType != GetType()) &&
                     (!type.IsGenericTypeDefinition)));

                foreach (var type in abstractInterfaceTypes)
                {
                    var localType       = type;
                    var implementations = concreteTypes
                                          .Where(implementationType => localType.IsAssignableFrom(implementationType)).ToList();

                    if (implementations.Skip(1).Any())
                    {
                        if (duplicateAction == DependencyContainerDuplicateImplementationAction.Fail)
                        {
                            throw new DependencyContainerRegistrationException(type, implementations);
                        }

                        if (duplicateAction == DependencyContainerDuplicateImplementationAction.RegisterMultiple)
                        {
                            RegisterMultiple(type, implementations);
                        }
                    }

                    var firstImplementation = implementations.FirstOrDefault();

                    if (firstImplementation == null)
                    {
                        continue;
                    }

                    try
                    {
                        RegisteredTypes.Register(type, string.Empty, GetDefaultObjectFactory(type, firstImplementation));
                    }
                    catch (MethodAccessException)
                    {
                        // Ignore methods we can't access - added for Silverlight
                    }
                }
            }
        }