コード例 #1
0
        /// <inheritdoc />
        public ComponentHandle ResolveHandleByComponentId(string componentId)
        {
            if (componentId == null)
            {
                throw new ArgumentNullException("componentId");
            }

            IComponentDescriptor descriptor = ResolveNonDisabledDescriptorByComponentId(componentId);

            return(ComponentHandle.CreateInstance(descriptor));
        }
コード例 #2
0
        /// <inheritdoc />
        public IList <ComponentHandle <TService, TTraits> > ResolveAllHandles <TService, TTraits>() where TTraits : Traits
        {
            var result = new List <ComponentHandle <TService, TTraits> >();

            foreach (IComponentDescriptor descriptor in ResolveAllNonDisabledDescriptors(typeof(TService)))
            {
                result.Add(ComponentHandle.CreateInstance <TService, TTraits>(descriptor));
            }

            return(result);
        }
コード例 #3
0
        /// <inheritdoc />
        public ComponentHandle ResolveHandle(Type serviceType)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            IComponentDescriptor descriptor = ResolveNonDisabledDescriptor(serviceType);

            return(ComponentHandle.CreateInstance(descriptor));
        }
コード例 #4
0
        private DependencyResolution ResolveDependencyByServiceLocation(string parameterName, Type parameterType)
        {
            if (parameterType.IsArray)
            {
                Type elementType = parameterType.GetElementType();

                if (ComponentHandle.IsComponentHandleType(elementType))
                {
                    Type serviceType = GetServiceTypeFromComponentHandleType(elementType);
                    if (serviceLocator.HasService(serviceType))
                    {
                        IList <ComponentHandle> componentHandles = serviceLocator.ResolveAllHandles(serviceType);
                        return(DependencyResolution.Satisfied(CreateArray(elementType, componentHandles)));
                    }
                }
                else
                {
                    Type serviceType = elementType;
                    if (serviceLocator.HasService(serviceType))
                    {
                        IList <object> components = serviceLocator.ResolveAll(serviceType);
                        return(DependencyResolution.Satisfied(CreateArray(serviceType, components)));
                    }
                }
            }
            else
            {
                if (ComponentHandle.IsComponentHandleType(parameterType))
                {
                    Type serviceType = GetServiceTypeFromComponentHandleType(parameterType);
                    if (serviceLocator.HasService(serviceType))
                    {
                        ComponentHandle componentHandle = serviceLocator.ResolveHandle(serviceType);
                        return(DependencyResolution.Satisfied(componentHandle));
                    }
                }
                else
                {
                    Type serviceType = parameterType;
                    if (serviceLocator.HasService(serviceType))
                    {
                        object component = serviceLocator.Resolve(parameterType);
                        return(DependencyResolution.Satisfied(component));
                    }
                }
            }

            return(DependencyResolution.Unsatisfied());
        }
コード例 #5
0
        /// <inheritdoc />
        public IList <ComponentHandle> ResolveAllHandles(Type serviceType)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            var result = new List <ComponentHandle>();

            foreach (IComponentDescriptor descriptor in ResolveAllNonDisabledDescriptors(serviceType))
            {
                result.Add(ComponentHandle.CreateInstance(descriptor));
            }

            return(result);
        }
コード例 #6
0
        /// <inheritdoc />
        public ComponentHandle <TService, TTraits> ResolveHandle <TService, TTraits>() where TTraits : Traits
        {
            IComponentDescriptor descriptor = ResolveNonDisabledDescriptor(typeof(TService));

            return(ComponentHandle.CreateInstance <TService, TTraits>(descriptor));
        }
コード例 #7
0
        // TODO: Refactor me.
        private object ConvertConfigurationArgumentToType(Type type, string value)
        {
            if (type == typeof(string))
            {
                return(value);
            }

            if (type.IsEnum)
            {
                return(Enum.Parse(type, value, true));
            }

            if (type == typeof(Version))
            {
                return(new Version(value));
            }

            if (type == typeof(Guid))
            {
                return(new Guid(value));
            }

            if (type == typeof(Condition))
            {
                return(Condition.Parse(value));
            }

            if (type == typeof(Image))
            {
                return(Image.FromFile(ResolveResourcePath(value)));
            }

            if (type == typeof(Icon))
            {
                return(new Icon(ResolveResourcePath(value)));
            }

            if (type == typeof(FileInfo))
            {
                return(new FileInfo(ResolveResourcePath(value)));
            }

            if (type == typeof(DirectoryInfo))
            {
                return(new DirectoryInfo(ResolveResourcePath(value)));
            }

            if (type == typeof(AssemblyName))
            {
                return(new AssemblyName(value));
            }

            if (type == typeof(AssemblySignature))
            {
                return(AssemblySignature.Parse(value));
            }

            if (type == typeof(Assembly))
            {
                return(Assembly.Load(value));
            }

            if (type == typeof(Type))
            {
                return(Type.GetType(value));
            }

            if (value.StartsWith("${") && value.EndsWith("}"))
            {
                string componentId = value.Substring(2, value.Length - 3);

                object componentOrHandle;
                if (ComponentHandle.IsComponentHandleType(type))
                {
                    componentOrHandle = serviceLocator.ResolveHandleByComponentId(componentId);
                }
                else
                {
                    componentOrHandle = serviceLocator.ResolveByComponentId(componentId);
                }

                if (!type.IsInstanceOfType(componentOrHandle))
                {
                    throw new RuntimeException(string.Format("Could not inject component with id '{0}' into a dependency of type '{1}' because it is of the wrong type even though the component was explicitly specified using the '${{component.id}}' property value syntax.",
                                                             componentId, type));
                }

                return(componentOrHandle);
            }

            return(Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
        }