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); } } }