Example #1
0
        public void RegisteredComponentsExistAndAreTheCorrectTypes()
        {
            IPicoContainer pico = CreatePicoContainerWithTouchableAndDependsOnTouchable();

            Assert.IsNotNull(pico.GetComponentAdapter(typeof(ITouchable)));
            Assert.IsNotNull(pico.GetComponentAdapter(typeof(DependsOnTouchable)));
            Assert.IsTrue(pico.GetComponentInstance(typeof(ITouchable)) is ITouchable);
            Assert.IsTrue(pico.GetComponentInstance(typeof(DependsOnTouchable)) is DependsOnTouchable);
            Assert.IsNull(pico.GetComponentAdapter(typeof(ICollection)));
        }
Example #2
0
        public IComponentAdapter GetComponentAdapter(object componentKey)
        {
            IComponentAdapter adapter = (IComponentAdapter)componentKeyToAdapterMap[componentKey];

            if (adapter == null && parent != null)
            {
                adapter = parent.GetComponentAdapter(componentKey);
            }

            return(adapter);
        }
        private IComponentAdapter GetTargetAdapter(IPicoContainer container, Type expectedType,
                                                   IComponentAdapter excludeAdapter)
        {
            if (componentKey != null)
            {
                // key tells us where to look so we follow
                return container.GetComponentAdapter(componentKey);
            }
            else if (excludeAdapter == null)
            {
                return container.GetComponentAdapterOfType(expectedType);
            }
            else
            {
                Object excludeKey = excludeAdapter.ComponentKey;
                IComponentAdapter byKey = container.GetComponentAdapter(expectedType);
                if (byKey != null)
                {
                    if (byKey.ComponentKey.Equals(excludeKey))
                    {
                        return null;
                    }
                    return byKey;
                }
                IList found = container.GetComponentAdaptersOfType(expectedType);
                IComponentAdapter exclude = null;

                foreach (IComponentAdapter work in found)
                {
                    if (work.ComponentKey.Equals(excludeKey))
                    {
                        exclude = work;
                    }
                }

                found.Remove(exclude);
                if (found.Count == 0)
                {
                    if (container.Parent != null)
                    {
                        return container.Parent.GetComponentAdapterOfType(expectedType);
                    }
                    else
                    {
                        return null;
                    }
                }
                else if (found.Count == 1)
                {
                    return (IComponentAdapter) found[0];
                }
                else
                {
                    Type[] foundTypes = new Type[found.Count];
                    for (int i = 0; i < foundTypes.Length; i++)
                    {
                        foundTypes[i] = ((IComponentAdapter) found[i]).ComponentImplementation;
                    }
                    throw new AmbiguousComponentResolutionException(expectedType, foundTypes);
                }
            }
        }
Example #4
0
        private IComponentAdapter GetTargetAdapter(IPicoContainer container, Type expectedType,
                                                   IComponentAdapter excludeAdapter)
        {
            if (componentKey != null)
            {
                // key tells us where to look so we follow
                return(container.GetComponentAdapter(componentKey));
            }
            else if (excludeAdapter == null)
            {
                return(container.GetComponentAdapterOfType(expectedType));
            }
            else
            {
                Object            excludeKey = excludeAdapter.ComponentKey;
                IComponentAdapter byKey      = container.GetComponentAdapter(expectedType);
                if (byKey != null)
                {
                    if (byKey.ComponentKey.Equals(excludeKey))
                    {
                        return(null);
                    }
                    return(byKey);
                }
                IList             found   = container.GetComponentAdaptersOfType(expectedType);
                IComponentAdapter exclude = null;

                foreach (IComponentAdapter work in found)
                {
                    if (work.ComponentKey.Equals(excludeKey))
                    {
                        exclude = work;
                    }
                }

                found.Remove(exclude);
                if (found.Count == 0)
                {
                    if (container.Parent != null)
                    {
                        return(container.Parent.GetComponentAdapterOfType(expectedType));
                    }
                    else
                    {
                        return(null);
                    }
                }
                else if (found.Count == 1)
                {
                    return((IComponentAdapter)found[0]);
                }
                else
                {
                    Type[] foundTypes = new Type[found.Count];
                    for (int i = 0; i < foundTypes.Length; i++)
                    {
                        foundTypes[i] = ((IComponentAdapter)found[i]).ComponentImplementation;
                    }
                    throw new AmbiguousComponentResolutionException(expectedType, foundTypes);
                }
            }
        }