public MemberInjectionDescriptor(MemberInfo member) { _member = member; var serviceType = member switch { PropertyInfo property => property.PropertyType, FieldInfo field => field.FieldType, _ => throw new ArgumentException("Invalid member type."), }; if (!serviceType.IsGenericType || serviceType.GetGenericTypeDefinition() != typeof(IOptions <>)) { var optionType = typeof(IOptions <>).MakeGenericType(serviceType); _valueFactory = (provider, target) => { var property = _options.GetOrAdd(optionType, t => t.GetProperty("Value", BindingFlags.Public | BindingFlags.Instance)); var instance = provider.GetService(optionType); return(instance == null ? null : Reflection.Reflector.GetValue(property, ref instance)); }; } else { _valueFactory = (provider, target) => provider.GetService(ServiceModular.TryGetContract(target, serviceType, out var contract) ? contract : serviceType); } }
private static IApplicationModule GetApplicationModule(Type type) { var moduleName = ServiceModular.GetModuleName(type); if (!string.IsNullOrEmpty(moduleName) && ApplicationContext.Current.Modules.TryGet(moduleName, out var module)) { return(module); } return(ApplicationContext.Current); }
public object GetService(Type serviceType) { if (string.IsNullOrEmpty(_name)) { return(_provider.GetService(serviceType)); } if (serviceType.IsAbstract && ServiceModular.TryGetContract(_name, serviceType, out var contract)) { return(_provider.GetService(contract)); } return(_provider.GetService(serviceType)); }
public MemberInjectionDescriptor(MemberInfo member, ServiceDependencyAttribute attribute) { _member = member; var serviceType = attribute.ServiceType ?? member switch { PropertyInfo property => property.PropertyType, FieldInfo field => field.FieldType, _ => throw new ArgumentException("Invalid member type."), }; if (attribute.IsRequired) { if (string.IsNullOrEmpty(attribute.Provider)) { _valueFactory = (provider, target) => { if (ServiceModular.TryGetContract(target, serviceType, out var contract)) { return(provider.GetRequiredService(contract)); } else { return(provider.GetRequiredService(serviceType)); } } } ; else { _valueFactory = (provider, target) => { if (ServiceModular.TryGetContract(attribute.Provider, serviceType, out var contract)) { return(provider.GetRequiredService(contract)); } else { return(provider.GetRequiredService(serviceType)); } } }; } else { if (string.IsNullOrEmpty(attribute.Provider)) { _valueFactory = (provider, target) => { if (ServiceModular.TryGetContract(target, serviceType, out var contract)) { return(provider.GetService(contract)); } else { return(provider.GetService(serviceType)); } } } ; else { _valueFactory = (provider, target) => { if (ServiceModular.TryGetContract(attribute.Provider, serviceType, out var contract)) { return(provider.GetService(contract)); } else { return(provider.GetService(serviceType)); } } }; } }
public MemberInjectionDescriptor(MemberInfo member, ServiceDependencyAttribute attribute) { _member = member; var serviceType = attribute.ServiceType ?? member switch { PropertyInfo property => property.PropertyType, FieldInfo field => field.FieldType, _ => throw new ArgumentException("Invalid member type."), }; if (IsServiceAccessor(serviceType, out var accessorType)) { _valueFactory = (provider, target) => ActivatorUtilities.CreateInstance(provider, accessorType, new object[] { GetApplicationModule(member.ReflectedType) }); return; } if (attribute.IsRequired) { if (string.IsNullOrEmpty(attribute.Provider)) { _valueFactory = (provider, target) => { if (ServiceModular.TryGetContract(target, serviceType, out var contract)) { return(provider.GetRequiredService(contract)); } else { return(provider.GetRequiredService(serviceType)); } } } ; else { _valueFactory = (provider, target) => { if (ServiceModular.TryGetContract(attribute.Provider, serviceType, out var contract)) { return(provider.GetRequiredService(contract)); } else { return(provider.GetRequiredService(serviceType)); } } }; } else { if (string.IsNullOrEmpty(attribute.Provider)) { _valueFactory = (provider, target) => { if (ServiceModular.TryGetContract(target, serviceType, out var contract)) { return(provider.GetService(contract)); } else { return(provider.GetService(serviceType)); } } } ; else { _valueFactory = (provider, target) => { if (ServiceModular.TryGetContract(attribute.Provider, serviceType, out var contract)) { return(provider.GetService(contract)); } else { return(provider.GetService(serviceType)); } } }; } }