DictionaryConverter
using Castle.Core.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Castle.MicroKernel.SubSystems.Conversion
{
[Serializable]
public class DictionaryConverter : AbstractTypeConverter
{
public override bool CanHandleType(Type type)
{
if ((object)type != typeof(IDictionary))
return (object)type == typeof(Hashtable);
return true;
}
public override object PerformConversion(string value, Type targetType)
{
throw new NotImplementedException();
}
public override object PerformConversion(IConfiguration configuration, Type targetType)
{
Dictionary<object, object> dictionary = new Dictionary<object, object>();
string text = configuration.Attributes["keyType"];
Type type = typeof(string);
string text2 = configuration.Attributes["valueType"];
Type type2 = typeof(string);
if (text != null)
type = base.Context.Composition.PerformConversion<Type>(text);
if (text2 != null)
type2 = base.Context.Composition.PerformConversion<Type>(text2);
foreach (IConfiguration child in configuration.Children) {
string text3 = child.Attributes["key"];
if (text3 == null)
throw new ConverterException("You must provide a key for the dictionary entry");
Type targetType2 = type;
if (child.Attributes["keyType"] != null)
targetType2 = base.Context.Composition.PerformConversion<Type>(child.Attributes["keyType"]);
object key = base.Context.Composition.PerformConversion(text3, targetType2);
Type targetType3 = type2;
if (child.Attributes["valueType"] != null)
targetType3 = base.Context.Composition.PerformConversion<Type>(child.Attributes["valueType"]);
object value = (child.Children.Count != 0) ? base.Context.Composition.PerformConversion(child.Children[0], targetType3) : base.Context.Composition.PerformConversion(child, targetType3);
dictionary.Add(key, value);
}
return dictionary;
}
}
}