private static SpecifiedPropertiesSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToInject, string name) { NamedTypeBuildKey key = new NamedTypeBuildKey(typeToInject, name); IPropertySelectorPolicy selector = (IPropertySelectorPolicy)policies.Get(typeToInject, name, typeof(IPropertySelectorPolicy), out _); if (!(selector is SpecifiedPropertiesSelectorPolicy)) { selector = new SpecifiedPropertiesSelectorPolicy(); policies.Set(key.Type, key.Name, typeof(IPropertySelectorPolicy), selector); } return((SpecifiedPropertiesSelectorPolicy)selector); }
private static SpecifiedPropertiesSelectorPolicy GetSelectorPolicy(IPolicyList policies, Type typeToInject, string name) { NamedTypeBuildKey key = new NamedTypeBuildKey(typeToInject, name); IPropertySelectorPolicy selector = policies.GetNoDefault <IPropertySelectorPolicy>(key, false); if (selector == null || !(selector is SpecifiedPropertiesSelectorPolicy)) { selector = new SpecifiedPropertiesSelectorPolicy(); policies.Set <IPropertySelectorPolicy>(selector, key); } return((SpecifiedPropertiesSelectorPolicy)selector); }
public override void AddPolicies(Type typeToCreate, string name, IPolicyList policies) { Guard.ArgumentNotNull(typeToCreate, "typeToCreate"); PropertyInfo propInfo = typeToCreate.GetProperty(propertyName); GuardPropertyExists(propInfo, typeToCreate, propertyName); GuardPropertyIsSettable(propInfo); GuardPropertyIsNotIndexer(propInfo); InitializeParameterValue(propInfo); GuardPropertyValueIsCompatible(propInfo, parameterValue); SpecifiedPropertiesSelectorPolicy selector = GetSelectorPolicy(policies, typeToCreate, name); selector.AddPropertyAndValue(propInfo, parameterValue); }
public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies) { Guard.ArgumentNotNull(implementationType, "implementationType"); PropertyInfo propInfo = implementationType.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); GuardPropertyExists(propInfo, implementationType, propertyName); GuardPropertyIsSettable(propInfo); GuardPropertyIsNotIndexer(propInfo); InitializeParameterValue(propInfo); GuardPropertyValueIsCompatible(propInfo, parameterValue); SpecifiedPropertiesSelectorPolicy selector = GetSelectorPolicy(policies, implementationType, name); selector.AddPropertyAndValue(propInfo, parameterValue); }
public IEnumerable <SelectedProperty> SelectProperties(IBuilderContext context) { var target = BuildKey.GetType(context.BuildKey); var typeTracker = context.Policies.Get <TypeTrackerPolicy>(context.BuildKey).TypeTracker; // Unity includes a policy used when explicitly configuring injection. Here we borrow // it to specify the properties we want to fill. Normally the user specifies specific // properties and the policies for filling them, here we do it automatically. var policy = new SpecifiedPropertiesSelectorPolicy(); foreach (var property in target.GetProperties()) { // Ignore indexed properties if (property.GetIndexParameters().Length > 0) { continue; } // Ignore read only properties if (!property.CanWrite) { continue; } // Ignore properties we can't fill anyway if (!typeTracker.HasDependency(property.PropertyType)) { continue; } // Ignore properties that would create obvious circular dependencies. It is still // possible to create one if you have multiple classes referring to each other though. if (property.PropertyType == property.DeclaringType || property.DeclaringType.IsAssignableFrom(property.PropertyType)) { continue; } policy.AddPropertyAndValue(property, new TypeToBeResolved( property.PropertyType, GetResolver(property))); } return(policy.SelectProperties(context)); }
/// <summary> /// Add policies to the <paramref name="policies"/> to configure the /// container to call this constructor with the appropriate parameter values. /// </summary> /// <param name="serviceType">Interface being registered, ignored in this implementation.</param> /// <param name="implementationType">Type to register.</param> /// <param name="name">Name used to resolve the type object.</param> /// <param name="policies">Policy list to add policies to.</param> public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies) { var propInfo = (implementationType ?? throw new ArgumentNullException(nameof(implementationType))).GetPropertiesHierarchical() .FirstOrDefault(p => p.Name == _propertyName && !p.GetSetMethod(true).IsStatic); GuardPropertyExists(propInfo, implementationType, _propertyName); GuardPropertyIsSettable(propInfo); GuardPropertyIsNotIndexer(propInfo); InitializeParameterValue(propInfo); GuardPropertyValueIsCompatible(propInfo, _parameterValue); SpecifiedPropertiesSelectorPolicy selector = GetSelectorPolicy(policies, implementationType, name); selector.AddPropertyAndValue(propInfo, _parameterValue); }
/// <summary> /// Add policies to the <paramref name="policies"/> to configure the /// container to call this constructor with the appropriate parameter values. /// </summary> /// <param name="serviceType">Interface being registered, ignored in this implementation.</param> /// <param name="implementationType">Type to register.</param> /// <param name="name">Name used to resolve the type object.</param> /// <param name="policies">Policy list to add policies to.</param> public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies) { Guard.ArgumentNotNull(implementationType, "implementationType"); PropertyInfo propInfo = implementationType.GetPropertiesHierarchical() .FirstOrDefault(p => p.Name == this.propertyName && !p.SetMethod.IsStatic); GuardPropertyExists(propInfo, implementationType, this.propertyName); GuardPropertyIsSettable(propInfo); GuardPropertyIsNotIndexer(propInfo); this.InitializeParameterValue(propInfo); GuardPropertyValueIsCompatible(propInfo, this.parameterValue); SpecifiedPropertiesSelectorPolicy selector = GetSelectorPolicy(policies, implementationType, name); selector.AddPropertyAndValue(propInfo, this.parameterValue); }