public override object PerformConversion(IConfiguration configuration, Type targetType)
{
System.Diagnostics.Debug.Assert( targetType == typeof(IDictionary) || targetType == typeof(Hashtable) );
Hashtable dict = new Hashtable();
String keyTypeName = configuration.Attributes["keyType"];
Type defaultKeyType = typeof(String);
String valueTypeName = configuration.Attributes["valueType"];
Type defaultValueType = typeof(String);
if (keyTypeName != null)
{
defaultKeyType = (Type) Context.Composition.PerformConversion( keyTypeName, typeof(Type) );
}
if (valueTypeName != null)
{
defaultValueType = (Type) Context.Composition.PerformConversion( valueTypeName, typeof(Type) );
}
foreach(IConfiguration itemConfig in configuration.Children)
{
// Preparing the key
String keyValue = itemConfig.Attributes["key"];
if (keyValue == null)
{
throw new ConverterException("You must provide a key for the dictionary entry");
}
Type convertKeyTo = defaultKeyType;
if (itemConfig.Attributes["keyType"] != null)
{
convertKeyTo = (Type) Context.Composition.PerformConversion(
itemConfig.Attributes["keyType"], typeof(Type) );
}
object key = Context.Composition.PerformConversion(keyValue, convertKeyTo);
// Preparing the value
Type convertValueTo = defaultValueType;
if (itemConfig.Attributes["valueType"] != null)
{
convertValueTo = (Type) Context.Composition.PerformConversion(
itemConfig.Attributes["valueType"], typeof(Type) );
}
object value;
if (itemConfig.Children.Count == 0)
{
value = Context.Composition.PerformConversion(
itemConfig, convertValueTo);
}
else
{
value = Context.Composition.PerformConversion(
itemConfig.Children[0], convertValueTo);
}
dict.Add( key, value );
}
return dict;
}
}