private void RegisterRequiredComponent(Type type) { var components = type.GetAttributes <RequiredComponentAttribute>(false); foreach (var component in components) { var name = component.GetFinalRegisterName(); var lifetimeStyle = LifeCycleAttribute.GetLifetimeStyle(type); var serviceType = component.ServiceType; if (lifetimeStyle == LifetimeStyle.Singleton) { var bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase; var filed = serviceType.GetField("Instance", bindingFlags); if (filed != null) { this.RegisterInstance(type, filed.GetValue(null), name); continue; } var property = serviceType.GetProperty("Instance", bindingFlags); if (property != null) { this.RegisterInstance(type, property.GetValue(null, null), name); continue; } if (component.CreateInstance) { var instance = component.ConstructorParameters == null || component.ConstructorParameters.Length == 0 ? Activator.CreateInstance(serviceType) : Activator.CreateInstance(serviceType, component.ConstructorParameters); this.RegisterInstance(type, instance, name); continue; } } if (serviceType == null) { this.RegisterType(type, lifetimeStyle, name); } else { this.RegisterType(type, serviceType, lifetimeStyle, name); } } }
private void RegisterComponent(Type type) { var components = type.GetAttributes <RegisteredComponentAttribute>(false); foreach (var component in components) { var name = component.GetFinalRegisterName(type); var lifetimeStyle = LifeCycleAttribute.GetLifetimeStyle(type); var serviceType = component.ServiceType; if (serviceType == null) { this.RegisterType(type, lifetimeStyle, name); } else { this.RegisterType(serviceType, type, lifetimeStyle, name); } } }
private void RegisterComponents(IEnumerable <Type> types) { var registionTypes = types.Where(p => p.IsDefined(typeof(RegisterAttribute), false)); foreach (var type in registionTypes) { var lifecycle = LifeCycleAttribute.GetLifecycle(type); var attribute = type.GetCustomAttribute <RegisterAttribute>(false); if (attribute != null) { var contractType = attribute.ContractType; var contractName = attribute.ContractName; if (attribute.ContractType == null) { this.SetDefault(type, contractName, lifecycle); } else { this.SetDefault(attribute.ContractType, type, contractName, lifecycle); } } } }