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

GenericDictionaryConverter

using Castle.Core.Configuration; using Castle.Core.Internal; using System; using System.Collections.Generic; using System.Reflection; namespace Castle.MicroKernel.SubSystems.Conversion { [Serializable] public class GenericDictionaryConverter : AbstractTypeConverter { private class DictionaryHelper<TKey, TValue> : IGenericCollectionConverterHelper { private readonly GenericDictionaryConverter parent; public DictionaryHelper(GenericDictionaryConverter parent) { this.parent = parent; } public object ConvertConfigurationToCollection(IConfiguration configuration) { Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>(); foreach (IConfiguration child in configuration.Children) { string text = child.Attributes["key"]; if (text == null) throw new ConverterException("You must provide a key for the dictionary entry"); Type type = typeof(TKey); if (child.Attributes["keyType"] != null) type = parent.Context.Composition.PerformConversion<Type>(child.Attributes["keyType"]); if (!ReflectionUtil.Is<TKey>(type)) throw new ArgumentException(string.Format("Could not create dictionary<{0},{1}> because {2} is not assignable to key type {0}", typeof(TKey), typeof(TValue), type)); TKey key = (TKey)parent.Context.Composition.PerformConversion(text, type); Type type2 = typeof(TValue); if (child.Attributes["valueType"] != null) type2 = parent.Context.Composition.PerformConversion<Type>(child.Attributes["valueType"]); if (!ReflectionUtil.Is<TValue>(type2)) throw new ArgumentException(string.Format("Could not create dictionary<{0},{1}> because {2} is not assignable to value type {1}", typeof(TKey), typeof(TValue), type2)); if (child.Children.Count == 1) dictionary.Add(key, (TValue)parent.Context.Composition.PerformConversion(child.Children[0], type2)); else dictionary.Add(key, (TValue)parent.Context.Composition.PerformConversion(child.Value, type2)); } return dictionary; } } public override bool CanHandleType(Type type) { if (!type.GetTypeInfo().get_IsGenericType()) return false; Type genericTypeDefinition = type.GetGenericTypeDefinition(); if ((object)genericTypeDefinition != typeof(IDictionary<, >)) return (object)genericTypeDefinition == typeof(Dictionary<, >); return true; } public override object PerformConversion(string value, Type targetType) { throw new NotImplementedException(); } public override object PerformConversion(IConfiguration configuration, Type targetType) { Type[] genericArguments = TypeExtensions.GetGenericArguments(targetType); if (genericArguments.Length != 2) throw new ConverterException("Expected type with two generic arguments."); string text = configuration.Attributes["keyType"]; Type type = genericArguments[0]; string text2 = configuration.Attributes["valueType"]; Type type2 = genericArguments[1]; if (text != null) type = base.Context.Composition.PerformConversion<Type>(text); if (text2 != null) type2 = base.Context.Composition.PerformConversion<Type>(text2); return typeof(DictionaryHelper<, >).MakeGenericType(type, type2).CreateInstance<IGenericCollectionConverterHelper>(new object[1] { this }).ConvertConfigurationToCollection(configuration); } } }