private void AddModel(Type fragmentClass, FieldInfo field, InjectionAttribute injectionAttribute) { // bool optional = field.HasAttribute<OptionalAttribute>() || injectionAttribute.IsOptional(); // var dependencyModel = new DependencyModel(injectionAttribute, field.FieldType, fragmentClass, optional); var injectedFieldModel = new InjectedFieldModel(field, injectionAttribute); this.fields.Add(injectedFieldModel); }
protected void inject(MethodInfo methodInfo, object instance, IDependencyResolver dependencyResolver, IDependencyResolver explicitDependencies) { ParameterInfo[] parameterInfos = methodInfo.GetParameters(); object[] parameterValues = new object[parameterInfos.Length]; InjectionAttribute atr = (InjectionAttribute)methodInfo.GetCustomAttributes(typeof(InjectionAttribute), true)[0]; ResolutionOrder order = atr.resolution; bool soft = atr.soft; for (int i = 0; i < parameterInfos.Length; i++) { ParameterInfo parameter = parameterInfos[i]; object value = ResolveDependency(order, soft, instance, parameter.ParameterType, parameter.Name, dependencyResolver, explicitDependencies); if (value != null) { ControlRemoteObject(instance, value, methodInfo, parameter); } parameterValues[i] = value; } methodInfo.Invoke(instance, parameterValues); }
private void OnRegistering(object sender, ServiceRegisterEventArgs e) { if (e.ServiceType != typeof(PipelineManager)) { ((AutofacServiceRegisterEventArgs)e).Registration.EnableClassInterceptors(new ProxyGenerationOptions { Selector = new ServiceInterceptorSelector((IServiceContainer)sender) }).FindConstructorsWith(type => { #if NetCore // Fix the constructor injection in the interception mechanism. if (type.IsAssignableTo <IProxyTargetAccessor>()) { var constructors = InjectionAttribute.GetConstructors(type.GetTypeInfo().BaseType).ToArray(); if (constructors.Length > 0) { return(constructors.Select(ctor => type.GetTypeInfo().GetConstructor(new[] { typeof(IInterceptor[]), typeof(IInterceptorSelector) }.Concat(ctor.GetParameters().Select(parameter => parameter.ParameterType)).ToArray())).ToArray()); } } return(type.GetConstructors()); #else // Fix the constructor injection in the interception mechanism. if (type.IsAssignableTo <IProxyTargetAccessor>()) { var constructors = InjectionAttribute.GetConstructors(type.BaseType).ToArray(); if (constructors.Length > 0) { return(constructors.Select(ctor => type.GetConstructor(new[] { typeof(IInterceptor[]), typeof(IInterceptorSelector) }.Concat(ctor.GetParameters().Select(parameter => parameter.ParameterType)).ToArray())).ToArray()); } } return(type.GetConstructors()); #endif }); } }
/// <summary> /// Registers the type mapping. /// </summary> /// <param name="serviceType"><see cref="Type"/> that will be requested.</param> /// <param name="implementationType"><see cref="Type"/> that will actually be returned.</param> /// <param name="serviceName">Name to use for registration, null if a default registration.</param> /// <param name="lifetime">The lifetime strategy of the resolved instances.</param> protected override void DoRegister(Type serviceType, Type implementationType, string serviceName, ServiceLifetime lifetime) { if (_container == null) { throw new ObjectDisposedException("container"); } var registration = Component.For(serviceType) .ImplementedBy(implementationType) .Named(GetServiceName(serviceType, serviceName)) // Enable method injection. .AddDescriptor(new InjectionComponentDescriptor(this, implementationType)) // Enable property injection .PropertiesIgnore(property => !InjectionAttribute.Matches(property)); OnRegistering(new WindsorServiceRegisterEventArgs(serviceType, implementationType, serviceName, lifetime, registration)); switch (lifetime) { case ServiceLifetime.Singleton: registration.LifestyleSingleton(); break; case ServiceLifetime.Transient: registration.LifestyleTransient(); break; case ServiceLifetime.PerThread: registration.LifestylePerThread(); break; case ServiceLifetime.PerRequest: registration.LifestylePerWebRequest(); break; } _container.Register(registration); }
public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { if (fieldType == typeof(object)) { return context.CompositeInstance.Proxy; } return context.CompositeInstance.NewProxy(fieldType); }
public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { if (fieldType == typeof(object)) { return(context.CompositeInstance.Proxy); } return(context.CompositeInstance.NewProxy(fieldType)); }
// Binding //private InjectionProvider injectionProvider; public DependencyModel(InjectionAttribute attribute, Type injectionType, Type injectedType, bool optional) { this.injectionAnnotation = this.injectionAnnotation; this.injectedClass = injectedType; this.injectionType = injectionType; this.optional = optional; this.rawInjectionClass = injectionType; //MapPrimitiveTypes(ExtractRawInjectionClass(injectedType, injectionType)); this.injectionClass = injectionType; //ExtractRawInjectionClass(injectionType); }
public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { if (typeof(StateHolder).IsAssignableFrom(fieldType)) { return context.State; } return null; }
public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { if (typeof(StateHolder).IsAssignableFrom(fieldType)) { return(context.State); } return(null); }
private static ServiceLifetime GetServiceLifetime(this InjectionAttribute attribute) { switch (attribute) { case ScopedInjectionAttribute _: return(ServiceLifetime.Scoped); case SingletonInjectionAttribute _: return(ServiceLifetime.Singleton); default: return(ServiceLifetime.Transient); } }
public ConstructorInfo Find(Type pluggedType, DependencyCollection dependencies, PluginGraph graph) { var constructors = InjectionAttribute.GetConstructors(pluggedType).ToArray(); if (constructors.Length == 0) { return(null); } return((from constructor in constructors orderby constructor.GetParameters().Length descending select constructor).FirstOrDefault()); }
public static void Method1_3() { //TestSet7 var container = new Container(); container.RegisterType <Type>(); var typeActivator = new TypeActivator(); var injectionAttribute = new InjectionAttribute("TestSet17.Type"); var type = (System.Type)null; Assert.Throws <System.ArgumentNullException>(() => new RuntimeType(typeActivator, injectionAttribute, type, true)); }
public override void inject(object instance, IDependencyResolver dependencyResolver, IDependencyResolver explicitDependencies) { foreach (PropertyInfo propertyInfo in _properties) { InjectionAttribute atr = (InjectionAttribute)propertyInfo.GetCustomAttributes(typeof(InjectionAttribute), true)[0]; ResolutionOrder order = atr.resolution; bool soft = atr.soft; object value = ResolveDependency(order, soft, instance, propertyInfo.PropertyType, propertyInfo.Name, dependencyResolver, explicitDependencies); if (value != null) { ControlRemoteObject(instance, value, propertyInfo); propertyInfo.SetValue(instance, value, null); } } }
private void Register(ContainerBuilder builder, Type serviceType, Type implementationType, string serviceName, ServiceLifetime lifetime) { var registration = serviceName == null ? builder.RegisterType(implementationType).As(serviceType) : builder.RegisterType(implementationType).Named(serviceName, serviceType); registration .FindConstructorsWith(type => { var constructors = InjectionAttribute.GetConstructors(type).ToArray(); if (constructors.Length > 0) { return(constructors); } #if NetCore return(type.GetTypeInfo().DeclaredConstructors.Where(ctor => !ctor.IsStatic && ctor.IsPublic).ToArray()); #else return(type.GetConstructors()); #endif }) .UsingConstructor(new MostParametersConstructorSelector()) .OnActivated(args => DynamicInjectionBuilder.GetOrCreate(implementationType, true, true)(this, args.Instance)); OnRegistering(new AutofacServiceRegisterEventArgs(serviceType, implementationType, serviceName, lifetime, registration)); switch (lifetime) { case ServiceLifetime.Singleton: registration.SingleInstance(); break; case ServiceLifetime.Transient: registration.InstancePerDependency(); break; case ServiceLifetime.PerThread: registration.RegistrationData.Sharing = InstanceSharing.Shared; registration.RegistrationData.Lifetime = new PerThreadScopeLifetime(); break; case ServiceLifetime.PerRequest: registration.RegistrationData.Sharing = InstanceSharing.Shared; registration.RegistrationData.Lifetime = new PerRequestScopeLifetime(this); break; } }
public virtual void ProcessModel(IKernel kernel, ComponentModel model) { var targetType = model.Implementation; var constructors = InjectionAttribute.GetConstructors(targetType).Where(IsVisibleToContainer).ToArray(); if (constructors.Length == 0) { constructors = targetType.GetConstructors(BindingFlags.Public | BindingFlags.Instance) .Where(IsVisibleToContainer).ToArray(); } foreach (var constructor in constructors) { // We register each public constructor // and let the ComponentFactory select an // eligible amongst the candidates later model.AddConstructor(CreateConstructorCandidate(model, constructor)); } }
public static Action <IServiceContainer, object> GetOrCreate(Type typeToBuild, bool includeProperties, bool includeMethods) { return(_cache.GetOrAdd(new DynamicInjectionKey(typeToBuild, includeProperties, includeMethods), key => { var containerParameter = Expression.Parameter(typeof(IServiceContainer), "container"); var instanceParameter = Expression.Parameter(typeToBuild, "instance"); var buildPlanExpressions = new Queue <Expression>(); if (key.IncludeProperties) { foreach (var property in InjectionAttribute.GetProperties(key.InjectType)) { var expr = GetPropertyExpression(containerParameter, instanceParameter, property); if (expr != null) { buildPlanExpressions.Enqueue(expr); } } } if (key.IncludeMethods) { foreach (var method in InjectionAttribute.GetMethods(key.InjectType)) { buildPlanExpressions.Enqueue(Expression.Call( method.DeclaringType != null ? (Expression)Expression.Convert(instanceParameter, method.DeclaringType) : instanceParameter, method, GetParameterExpressions(containerParameter, method))); } } var planDelegate = buildPlanExpressions.Count > 0 ? Expression.Lambda(Expression.Block(buildPlanExpressions), containerParameter, instanceParameter).Compile() : null; return (container, instance) => { try { planDelegate?.DynamicInvoke(container, instance); } catch (TargetInvocationException e) { throw e.InnerException ?? e; } }; })); }
//[DebuggerStepThrough] ////[DebuggerHidden] public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { if (fieldType == typeof(TransientBuilderFactory)) { return context.ModuleInstance.TransientBuilderFactory; } if (fieldType == typeof(ObjectBuilderFactory)) { return context.ModuleInstance.ObjectBuilderFactory; } if (fieldType == typeof(ValueBuilderFactory)) { return context.ModuleInstance.ValueBuilderFactory; } if (fieldType == typeof(UnitOfWorkFactory)) { return context.ModuleInstance.UnitOfWorkFactory; } if (fieldType == typeof(ServiceFinder)) { return context.ModuleInstance.ServiceFinder; } if (fieldType == typeof(ModuleInstance)) { return context.ModuleInstance; } if (fieldType == typeof(Layer)) { return context.ModuleInstance.LayerInstance; } if (fieldType == typeof(Application)) { return context.ModuleInstance.LayerInstance.ApplicationInstance; } //else if( typeof( Qi4j) ) || typeof( Qi4jSPI) ) //{ // return context.ModuleInstance.layerInstance().applicationInstance().runtime(); //} return null; }
//[DebuggerStepThrough] ////[DebuggerHidden] public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { if (fieldType == typeof(TransientBuilderFactory)) { return(context.ModuleInstance.TransientBuilderFactory); } if (fieldType == typeof(ObjectBuilderFactory)) { return(context.ModuleInstance.ObjectBuilderFactory); } if (fieldType == typeof(ValueBuilderFactory)) { return(context.ModuleInstance.ValueBuilderFactory); } if (fieldType == typeof(UnitOfWorkFactory)) { return(context.ModuleInstance.UnitOfWorkFactory); } if (fieldType == typeof(ServiceFinder)) { return(context.ModuleInstance.ServiceFinder); } if (fieldType == typeof(ModuleInstance)) { return(context.ModuleInstance); } if (fieldType == typeof(Layer)) { return(context.ModuleInstance.LayerInstance); } if (fieldType == typeof(Application)) { return(context.ModuleInstance.LayerInstance.ApplicationInstance); } //else if( typeof( Qi4j) ) || typeof( Qi4jSPI) ) //{ // return context.ModuleInstance.layerInstance().applicationInstance().runtime(); //} return(null); }
public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { object obj = context.Uses.UseForType(fieldType); if (obj != null) { return obj; } ModuleInstance moduleInstance = context.ModuleInstance; TransientFinder compositeFinder = moduleInstance.FindCompositeModel(fieldType); if (compositeFinder.Model != null) { CompositeInstance compositeInstance = compositeFinder.Model.NewCompositeInstance(moduleInstance, context.Uses, context.State); context.Uses.Use(compositeInstance); return compositeInstance.Proxy; } return null; }
public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { object obj = context.Uses.UseForType(fieldType); if (obj != null) { return(obj); } ModuleInstance moduleInstance = context.ModuleInstance; TransientFinder compositeFinder = moduleInstance.FindCompositeModel(fieldType); if (compositeFinder.Model != null) { CompositeInstance compositeInstance = compositeFinder.Model.NewCompositeInstance(moduleInstance, context.Uses, context.State); context.Uses.Use(compositeInstance); return(compositeInstance.Proxy); } return(null); }
public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { ServiceReference service = context.ModuleInstance.ServiceFinder.FindService(fieldType); return(service); }
public static InjectionProvider ProviderFor(InjectionAttribute attribute) { return lookup[attribute.GetType()]; }
public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { ServiceReference service = context.ModuleInstance.ServiceFinder.FindService(fieldType); return service; }
private static void AddServices(IServiceCollection serviceCollection, Type serviceType, Type implementationType, InjectionAttribute injectionAttribute) { if (injectionAttribute.ServiceTypes == null || injectionAttribute.ServiceTypes.Length <= 0) { AddService(serviceCollection, serviceType, implementationType, injectionAttribute.Lifetime, injectionAttribute.Policy); } else { foreach (var type in injectionAttribute.ServiceTypes) { AddService(serviceCollection, type, implementationType, injectionAttribute.Lifetime, injectionAttribute.Policy); } } }
public static InjectionProvider ProviderFor(InjectionAttribute attribute) { return(lookup[attribute.GetType()]); }
public InjectedFieldModel(FieldInfo injectedField, InjectionAttribute injectionAttribute) { this.injectedField = new Field(injectedField); this.injectionAttribute = injectionAttribute; this.injectionProvider = InjectionProviderLookup.ProviderFor(injectionAttribute); }
//[DebuggerStepThrough] ////[DebuggerHidden] public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { return context.Next; }
//[DebuggerStepThrough] ////[DebuggerHidden] public object ProvideInjection(InjectionContext context, InjectionAttribute attribute, Type fieldType) { return(context.Next); }