Exemple #1
0
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                var instantiatableType = GetInstantiableType(destinationType);

                if (value is DictionaryElementCollection &&
                    instantiatableType != null)
                {
                    var    dictionary = (IDictionary)Activator.CreateInstance(instantiatableType);
                    Type[] generics   = instantiatableType.GetGenericArguments();

                    foreach (var item in (DictionaryElementCollection)value)
                    {
                        if (String.IsNullOrEmpty(item.Key))
                        {
                            throw new ConfigurationErrorsException("Key cannot be null in a dictionary element.");
                        }

                        var convertedKey   = TypeManipulation.ChangeToCompatibleType(item.Key, generics[0]);
                        var convertedValue = TypeManipulation.ChangeToCompatibleType(item.Value, generics[1]);

                        dictionary.Add(convertedKey, convertedValue);
                    }
                    return(dictionary);
                }

                return(base.ConvertTo(context, culture, value, destinationType));
            }
 /// <summary>
 /// Convert to the Autofac parameter type.
 /// </summary>
 /// <returns>The parameters represented by this collection.</returns>
 public IEnumerable <Parameter> ToParameters()
 {
     foreach (var parameter in this)
     {
         var localParameter = parameter;
         yield return(new ResolvedParameter(
                          (pi, c) => pi.Name == localParameter.Name,
                          (pi, c) => TypeManipulation.ChangeToCompatibleType(localParameter.CoerceValue(), pi.ParameterType)));
     }
 }
 /// <summary>
 /// Convert to the Autofac parameter type.
 /// </summary>
 /// <returns>The parameters represented by this collection.</returns>
 public IEnumerable <Parameter> ToParameters()
 {
     foreach (var parameter in this)
     {
         var localParameter = parameter;
         yield return(new ResolvedParameter(
                          (pi, c) =>
         {
             PropertyInfo prop;
             return pi.TryGetDeclaringProperty(out prop) &&
             prop.Name == localParameter.Name;
         },
                          (pi, c) => TypeManipulation.ChangeToCompatibleType(localParameter.CoerceValue(), pi.ParameterType)));
     }
 }
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                var instantiatableType = GetInstantiableType(destinationType);

                if (value is ListElementCollection &&
                    instantiatableType != null)
                {
                    Type[] generics = instantiatableType.GetGenericArguments();

                    var collection = (IList)Activator.CreateInstance(instantiatableType);
                    foreach (var item in (ListElementCollection)value)
                    {
                        collection.Add(TypeManipulation.ChangeToCompatibleType(item.Value, generics[0]));
                    }
                    return(collection);
                }

                return(base.ConvertTo(context, culture, value, destinationType));
            }
Exemple #5
0
        /// <summary>
        /// Override to add registrations to the container.
        /// </summary>
        /// <param name="builder">The builder.</param>
        protected override void Load(ContainerBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            Assembly defaultAssembly = null;

            if (!string.IsNullOrEmpty(_sectionHandler.DefaultAssembly))
            {
                defaultAssembly = Assembly.Load(_sectionHandler.DefaultAssembly);
            }

            foreach (ModuleElement moduleElement in _sectionHandler.Modules)
            {
                var moduleType      = LoadType(moduleElement.Type, defaultAssembly);
                var moduleActivator = new ReflectionActivator(
                    moduleType,
                    new BindingFlagsConstructorFinder(BindingFlags.Public),
                    new MostParametersConstructorSelector(),
                    moduleElement.Parameters.ToParameters(),
                    moduleElement.Properties.ToParameters());
                var module = (IModule)moduleActivator.ActivateInstance(Container.Empty, Enumerable.Empty <Parameter>());
                builder.RegisterModule(module);
            }

            foreach (AssemblyElement asm in _sectionHandler.Assemblies)
            {
                var path = asm.Assembly;
                var cdp  = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, asm.Assembly);
                if (File.Exists(cdp))
                {
                    path = cdp;
                }
                builder.RegisterAssemblyTypes(Assembly.LoadFrom(path));
            }

            foreach (ComponentElement component in _sectionHandler.Components)
            {
                var registrar = builder.RegisterType(LoadType(component.Type, defaultAssembly));

                IList <Service> services = new List <Service>();
                if (!string.IsNullOrEmpty(component.Service))
                {
                    var serviceType = LoadType(component.Service, defaultAssembly);
                    if (!string.IsNullOrEmpty(component.Name))
                    {
                        services.Add(new KeyedService(component.Name, serviceType));
                    }
                    else
                    {
                        services.Add(new TypedService(serviceType));
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(component.Name))
                    {
                        throw new ConfigurationErrorsException(string.Format(
                                                                   ConfigurationSettingsReaderResources.ServiceTypeMustBeSpecified, component.Name));
                    }
                }

                foreach (ServiceElement service in component.Services)
                {
                    var serviceType = LoadType(service.Type, defaultAssembly);
                    if (!string.IsNullOrEmpty(service.Name))
                    {
                        services.Add(new KeyedService(service.Name, serviceType));
                    }
                    else
                    {
                        services.Add(new TypedService(serviceType));
                    }
                }


                foreach (var service in services)
                {
                    registrar.As(service);
                }

                foreach (var param in component.Parameters.ToParameters())
                {
                    registrar.WithParameter(param);
                }

                foreach (var prop in component.Properties.ToParameters())
                {
                    registrar.WithProperty(prop);
                }

                foreach (var ep in component.Metadata)
                {
                    registrar.WithMetadata(
                        ep.Name, TypeManipulation.ChangeToCompatibleType(ep.Value, Type.GetType(ep.Type)));
                }

                if (!string.IsNullOrEmpty(component.MemberOf))
                {
                    registrar.MemberOf(component.MemberOf);
                }

                SetScope(component, registrar);
                SetOwnership(component, registrar);
                SetInjectProperties(component, registrar);
            }

            foreach (FileElement file in _sectionHandler.Files)
            {
                var section = DefaultSectionName;
                if (!string.IsNullOrEmpty(file.Section))
                {
                    section = file.Section;
                }

                var reader = new ConfigurationSettingsReader(section, file.Name);
                builder.RegisterModule(reader);
            }
        }