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

ServiceOverrideDescriptor

using Castle.Core; using Castle.Core.Configuration; using Castle.Core.Internal; using Castle.MicroKernel.Registration; using Castle.MicroKernel.Util; using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; namespace Castle.MicroKernel.ModelBuilder.Descriptors { public class ServiceOverrideDescriptor : AbstractPropertyDescriptor { private readonly object value; public ServiceOverrideDescriptor(params ServiceOverride[] overrides) { value = overrides; } public ServiceOverrideDescriptor(IDictionary dictionary) { value = dictionary; } public override void BuildComponentModel(IKernel kernel, ComponentModel model) { IDictionary dictionary = value as IDictionary; if (dictionary != null) { IDictionaryEnumerator enumerator = dictionary.GetEnumerator(); try { while (enumerator.MoveNext()) { DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current; Apply(model, dictionaryEntry.Key, dictionaryEntry.Value, null); } } finally { (enumerator as IDisposable)?.Dispose(); } } ServiceOverride[] array = value as ServiceOverride[]; if (array != null) array.ForEach(delegate(ServiceOverride o) { Apply(model, o.DependencyKey, o.Value, o); }); } private void Apply(ComponentModel model, object dependencyKey, object dependencyValue, ServiceOverride override) { if (dependencyValue is string) ApplySimpleReference(model, dependencyKey, (string)dependencyValue); else if (dependencyValue is IEnumerable<string>) { ApplyReferenceList(model, dependencyKey, (IEnumerable<string>)dependencyValue, override); } else if (dependencyValue is Type) { ApplySimpleReference(model, dependencyKey, ComponentName.DefaultNameFor((Type)dependencyValue)); } else if (dependencyValue is IEnumerable<Type>) { ApplyReferenceList(model, dependencyKey, ((IEnumerable<Type>)dependencyValue).Select(ComponentName.DefaultNameFor), override); } } private void ApplyReferenceList(ComponentModel model, object name, IEnumerable<string> items, ServiceOverride serviceOverride) { MutableConfiguration val = new MutableConfiguration("list"); if (serviceOverride != null && serviceOverride.Type != (Type)null) ((NameValueCollection)val.get_Attributes()).Add("type", serviceOverride.Type.AssemblyQualifiedName); foreach (string item in items) { string text = ReferenceExpressionUtil.BuildReference(item); ((List<IConfiguration>)val.get_Children()).Add(new MutableConfiguration("item", text)); } AddParameter(model, GetNameString(name), val); } private void ApplySimpleReference(ComponentModel model, object dependencyName, string componentKey) { string text = ReferenceExpressionUtil.BuildReference(componentKey); AddParameter(model, GetNameString(dependencyName), text); } private string GetNameString(object key) { if (key is Type) return ((Type)key).AssemblyQualifiedName; return key.ToString(); } } }