<PackageReference Include="Castle.Windsor" Version="6.0.0" />

PropertiesDependenciesModelInspector

This implementation of IContributeComponentModelConstruction collects all potential writable public properties exposed by the component implementation and populates the model with them. The Kernel might be able to set some of these properties when the component is requested.
using Castle.Core; using Castle.Core.Configuration; using Castle.MicroKernel.SubSystems.Conversion; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Reflection; namespace Castle.MicroKernel.ModelBuilder.Inspectors { [Serializable] public class PropertiesDependenciesModelInspector : IContributeComponentModelConstruction { [NonSerialized] private readonly IConversionManager converter; public PropertiesDependenciesModelInspector(IConversionManager converter) { this.converter = converter; } public virtual void ProcessModel(IKernel kernel, ComponentModel model) { InspectProperties(model); } protected virtual void InspectProperties(ComponentModel model) { Type implementation = model.Implementation; if (model.InspectionBehavior == PropertiesInspectionBehavior.Undefined) model.InspectionBehavior = GetInspectionBehaviorFromTheConfiguration(model.Configuration); if (model.InspectionBehavior != PropertiesInspectionBehavior.None) { List<PropertyInfo> properties = GetProperties(model, implementation); if (properties.Count != 0) { ICollection<PropertyDependencyFilter> propertyFilters = StandardPropertyFilters.GetPropertyFilters(model, false); if (propertyFilters == null) properties.ForEach(delegate(PropertyInfo p) { model.AddProperty(BuildDependency(p, true)); }); else { foreach (PropertyDependencyFilter item in propertyFilters.Concat(new PropertyDependencyFilter[1] { StandardPropertyFilters.Create(PropertyFilter.Default) })) { PropertySet[] array = item(model, properties, BuildDependency); if (array != null) { PropertySet[] array2 = array; foreach (PropertySet property in array2) { model.AddProperty(property); } } if (properties.Count == 0) break; } } } } } private PropertySet BuildDependency(PropertyInfo property, bool isOptional) { PropertyDependencyModel dependency = new PropertyDependencyModel(property, isOptional); return new PropertySet(property, dependency); } private PropertiesInspectionBehavior GetInspectionBehaviorFromTheConfiguration(IConfiguration config) { if (config != null && ((NameValueCollection)config.get_Attributes())["inspectionBehavior"] != null) { string text = ((NameValueCollection)config.get_Attributes())["inspectionBehavior"]; try { return converter.PerformConversion<PropertiesInspectionBehavior>(text); } catch (Exception) { throw new ConverterException(string.Format("Error on properties inspection. Could not convert the inspectionBehavior attribute value into an expected enum value. Value found is '{0}' while possible values are '{1}'", text, string.Join(", ", Enum.GetNames(typeof(PropertiesInspectionBehavior))))); } } return PropertiesInspectionBehavior.All; } private List<PropertyInfo> GetProperties(ComponentModel model, Type targetType) { BindingFlags bindingAttr = (model.InspectionBehavior != PropertiesInspectionBehavior.DeclaredOnly) ? (BindingFlags.Instance | BindingFlags.Public) : (BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); return targetType.GetProperties(bindingAttr).Where(IsValidPropertyDependency).ToList(); } private static bool HasDoNotWireAttribute(PropertyInfo property) { return CustomAttributeExtensions.IsDefined(property, typeof(DoNotWireAttribute)); } private static bool HasParameters(PropertyInfo property) { ParameterInfo[] indexParameters = property.GetIndexParameters(); if (indexParameters != null) return indexParameters.Length != 0; return false; } private static bool IsSettable(PropertyInfo property) { if (property.CanWrite) return property.GetSetMethod() != (MethodInfo)null; return false; } private static bool IsValidPropertyDependency(PropertyInfo property) { if (IsSettable(property) && !HasParameters(property)) return !HasDoNotWireAttribute(property); return false; } } }