protected virtual IEnumerable<PropertyInfo> FindInjectableProperties(Type type, IMvxPropertyInjectorOptions options) { var injectableProperties = type .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy) .Where(p => p.PropertyType.GetTypeInfo().IsInterface) .Where(p => p.IsConventional()) .Where(p => p.CanWrite); switch (options.InjectIntoProperties) { case MvxPropertyInjection.MvxInjectInterfaceProperties: injectableProperties = injectableProperties .Where(p => p.GetCustomAttributes(typeof(MvxInjectAttribute), false).Any()); break; case MvxPropertyInjection.AllInterfaceProperties: break; case MvxPropertyInjection.None: Mvx.Error("Internal error - should not call FindInjectableProperties with MvxPropertyInjection.None"); injectableProperties = new PropertyInfo[0]; break; default: throw new MvxException("unknown option for InjectIntoProperties {0}", options.InjectIntoProperties); } return injectableProperties; }
/// <summary> /// Initializes a new instance of the <see cref="AutofacMvxIocProvider"/> class. /// </summary> /// <param name="container"> /// The container from which dependencies should be resolved. /// </param> /// <param name="propertyInjectionOptions">propertyInjectionOptions</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="container"/> is <see langword="null"/>. /// </exception> public AutofacMvxIocProvider(IContainer container, IMvxPropertyInjectorOptions propertyInjectionOptions) { Container = container ?? throw new ArgumentNullException(nameof(container)); PropertyInjectionOptions = propertyInjectionOptions ?? throw new ArgumentNullException(nameof(propertyInjectionOptions)); PropertyInjectionEnabled = propertyInjectionOptions.InjectIntoProperties != MvxPropertyInjection.None; if (propertyInjectionOptions.ThrowIfPropertyInjectionFails) { throw new NotSupportedException("Autofac does not support throwing an exception in case a service could not be injected into a property!"); } Container = container; PropertyInjectionOptions = propertyInjectionOptions; }
public virtual void Inject(object target, IMvxPropertyInjectorOptions options = null) { options = options ?? MvxPropertyInjectorOptions.All; if (options.InjectIntoProperties == MvxPropertyInjection.None) return; if (target == null) throw new ArgumentNullException(nameof(target)); var injectableProperties = this.FindInjectableProperties(target.GetType(), options); foreach (var injectableProperty in injectableProperties) { this.InjectProperty(target, injectableProperty, options); } }
public virtual void Inject(object target, IMvxPropertyInjectorOptions options = null) { options = options ?? MvxPropertyInjectorOptions.All; if (options.InjectIntoProperties == MvxPropertyInjection.None) { return; } if (target == null) { throw new ArgumentNullException(nameof(target)); } var injectableProperties = FindInjectableProperties(target.GetType(), options); foreach (var injectableProperty in injectableProperties) { InjectProperty(target, injectableProperty, options); } }
protected virtual void InjectProperty(object toReturn, PropertyInfo injectableProperty, IMvxPropertyInjectorOptions options) { object propertyValue; if (Mvx.TryResolve(injectableProperty.PropertyType, out propertyValue)) { try { injectableProperty.SetValue(toReturn, propertyValue, null); } catch (TargetInvocationException invocation) { throw new MvxIoCResolveException(invocation, "Failed to inject into {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); } } else { if (options.ThrowIfPropertyInjectionFails) throw new MvxIoCResolveException("IoC property injection failed for {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); else Mvx.Warning("IoC property injection skipped for {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); } }
/// <summary> /// Initializes a new instance of the <see cref="AutofacMvxIocProvider"/> class. /// </summary> /// <param name="container"> /// The container from which dependencies should be resolved. /// </param> /// <param name="propertyInjectionOptions">propertyInjectionOptions</param> /// <exception cref="System.ArgumentNullException"> /// Thrown if <paramref name="container"/> is <see langword="null"/>. /// </exception> public AutofacMvxIocProvider(ILifetimeScope container, IMvxPropertyInjectorOptions propertyInjectionOptions) : this(new ChildAutofacMvxIocProvider(container, propertyInjectionOptions)) { }
protected virtual IEnumerable <PropertyInfo> FindInjectableProperties(Type type, IMvxPropertyInjectorOptions options) { var injectableProperties = type .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy) .Where(p => p.PropertyType.GetTypeInfo().IsInterface) .Where(p => p.IsConventional()) .Where(p => p.CanWrite); switch (options.InjectIntoProperties) { case MvxPropertyInjection.MvxInjectInterfaceProperties: injectableProperties = injectableProperties .Where(p => p.GetCustomAttributes(typeof(MvxInjectAttribute), false).Any()); break; case MvxPropertyInjection.AllInterfaceProperties: break; case MvxPropertyInjection.None: MvxLog.Instance.Error("Internal error - should not call FindInjectableProperties with MvxPropertyInjection.None"); injectableProperties = new PropertyInfo[0]; break; default: throw new MvxException("unknown option for InjectIntoProperties {0}", options.InjectIntoProperties); } return(injectableProperties); }
protected virtual void InjectProperty(object toReturn, PropertyInfo injectableProperty, IMvxPropertyInjectorOptions options) { object propertyValue; if (Mvx.TryResolve(injectableProperty.PropertyType, out propertyValue)) { try { injectableProperty.SetValue(toReturn, propertyValue, null); } catch (TargetInvocationException invocation) { throw new MvxIoCResolveException(invocation, "Failed to inject into {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); } } else { if (options.ThrowIfPropertyInjectionFails) { throw new MvxIoCResolveException("IoC property injection failed for {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); } else { MvxLog.Instance.Warn("IoC property injection skipped for {0} on {1}", injectableProperty.Name, toReturn.GetType().Name); } } }