/// <summary>
        /// Create objects of type T. If the constructor has dependencies they will be matched from createdServices list.
        /// </summary>
        /// <typeparam name="T">Type of objects to create</typeparam>
        /// <returns>Created object</returns>
        public IEnumerable <T> ResolveAll <T>() where T : class
        {
            var assemblies = new List <Assembly> {
                Assembly.GetExecutingAssembly()
            };

            if (!assemblies.Contains(typeof(T).Assembly))
            {
                assemblies.Add(typeof(T).Assembly);
            }
            var types = assemblies.SelectMany(a => a.GetTypes()).Where(t => !t.IsAbstract && (t.IsSubclassOf(typeof(T)) || typeof(T).IsAssignableFrom(t)));

            foreach (var type in types)
            {
                var constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
                if (!constructors.Any())
                {
                    yield return(ExpressionActivator.CreateInstance <T>(type));
                }
                foreach (var constructor in constructors)
                {
                    object[] matchedParameters;
                    if (TryBindParameters(constructor, out matchedParameters))
                    {
                        yield return(Activator.CreateInstance(type, matchedParameters) as T);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Create objects of type T. If the constructor has dependencies they will be matched from createdServices list.
        /// </summary>
        /// <typeparam name="T">Type of objects to create</typeparam>
        /// <returns>Created object</returns>
        public IEnumerable <T> ResolveAll <T>() where T : class
        {
#if CSHTML5
            var assemblies = new List <Assembly> {
                GetType().Assembly
            };
            if (!assemblies.Contains(typeof(T).Assembly))
            {
                assemblies.Add(typeof(T).Assembly);
            }
            var types = assemblies.SelectMany(a => a.GetTypes()).Where(t => !t.IsAbstract && (t.IsSubclassOf(typeof(T)) || typeof(T).IsAssignableFrom(t)));
            foreach (var type in types)
            {
                var constructors = type.GetConstructors();
                if (!constructors.Any())
                {
                    yield return(ExpressionActivator.CreateInstance <T>(type));
                }

                foreach (var constructor in constructors)
                {
                    object[] matchedParameters;

                    if (TryBindParameters(constructor, out matchedParameters))
                    {
                        yield return(Activator.CreateInstance(type, matchedParameters) as T);
                    }
                }
            }
#else
            var assemblies = new List <Assembly> {
                GetType().GetTypeInfo().Assembly
            };
            if (!assemblies.Contains(typeof(T).GetTypeInfo().Assembly))
            {
                assemblies.Add(typeof(T).GetTypeInfo().Assembly);
            }
            var types = assemblies.SelectMany(a => a.DefinedTypes).Where(t => !t.IsAbstract && (t.IsSubclassOf(typeof(T)) || typeof(T).GetTypeInfo().IsAssignableFrom(t)));
            foreach (var type in types)
            {
                var constructors = type.DeclaredConstructors;

                if (!constructors.Any())
                {
                    yield return(ExpressionActivator.CreateInstance <T>(type.AsType()));
                }

                foreach (var constructor in constructors)
                {
                    object[] matchedParameters;

                    if (TryBindParameters(constructor, out matchedParameters))
                    {
                        yield return(Activator.CreateInstance(type.AsType(), matchedParameters) as T);
                    }
                }
            }
#endif
        }