/// <summary> /// Does its best to convert whatever the value is into the destination /// type. Null in yields null out for value types and the default(T) /// for value types (this may change.) /// </summary> /// <param name="value">The value.</param> /// <param name="destinationType">Type of the destination.</param> /// <returns>An object of the destination type.</returns> public static object ChangeToCompatibleType(object value, Type destinationType) { Enforce.ArgumentNotNull(destinationType, "destinationType"); if (value == null) { if (destinationType.IsValueType) { return(Activator.CreateInstance(destinationType)); } return(null); } if (destinationType.IsAssignableFrom(value.GetType())) { return(value); } return(TypeDescriptor.GetConverter(destinationType).ConvertFrom(value)); }
/// <summary> /// Override to add registrations to the container. /// </summary> /// <param name="builder">The builder.</param> protected override void Load(ContainerBuilder builder) { Enforce.ArgumentNotNull(builder, "builder"); Assembly defaultAssembly = null; if (!string.IsNullOrEmpty(_serviceConfig.DefaultAssembly)) { defaultAssembly = Assembly.Load(_serviceConfig.DefaultAssembly); } foreach (ModuleElement moduleElement in _serviceConfig.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 (ComponentElement component in _serviceConfig.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( "Service Type Must Be Specified:{0}", 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 _serviceConfig.Files) //{ // var section = DefaultSectionName; // if (!string.IsNullOrEmpty(file.Section)) // section = file.Section; // var reader = new ConfigurationSettingsReader(section, file.Name); // builder.RegisterModule(reader); //} }