Catel.IoC.ServiceLocator.ResolveTypes C# (CSharp) Method

ResolveTypes() public method

Resolves all instances of the type registered on the service.
Note that the actual implementation lays in the hands of the IoC technique being used.
The is null.
public ResolveTypes ( Type serviceType ) : IEnumerable
serviceType System.Type The type of the service.
return IEnumerable
        public IEnumerable<object> ResolveTypes(Type serviceType)
        {
            Argument.IsNotNull("serviceType", serviceType);

            var resolvedInstances = new List<object>();

            lock (_lockObject)
            {
                for (int i = 0; i < _registeredTypes.Keys.Count; i++)
                {
                    var serviceInfo = _registeredTypes.Keys.ElementAt(i);
                    if (serviceInfo.Type == serviceType)
                    {
                        try
                        {
                            resolvedInstances.Add(ResolveType(serviceInfo.Type, serviceInfo.Tag));
                        }
                        catch (TypeNotRegisteredException ex)
                        {
                            Log.Debug(ex, "Failed to resolve type '{0}', returning null", ex.RequestedType.GetSafeFullName(false));
                        }
                    }
                }
            }

            return resolvedInstances;
        }

Usage Example

コード例 #1
0
ファイル: ServiceLocatorFacts.cs プロジェクト: pars87/Catel
 public void ReturnsAllAvaliableInstances()
 {
     var serviceLocator = new ServiceLocator { AutoRegisterTypesViaAttributes = true };
     serviceLocator.RegisterInstance(typeof(IFooService), new FooService2(), "FooService3");
     Assert.AreEqual(3, serviceLocator.ResolveTypes<IFooService>().Count());
 }