Example #1
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());
        }
Example #2
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));
        }