public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { var instantiatableType = GetInstantiableType(destinationType); var castValue = value as ConfiguredDictionaryParameter; if (castValue != null && instantiatableType != null) { var dictionary = (IDictionary)Activator.CreateInstance(instantiatableType); var generics = instantiatableType.GetGenericArguments(); foreach (var item in castValue.Dictionary) { if (String.IsNullOrEmpty(item.Key)) { throw new FormatException(ConfigurationResources.DictionaryKeyMayNotBeNullOrEmpty); } var convertedKey = TypeManipulation.ChangeToCompatibleType(item.Key, generics[0]); var convertedValue = TypeManipulation.ChangeToCompatibleType(item.Value, generics[1]); dictionary.Add(convertedKey, convertedValue); } return(dictionary); } return(base.ConvertTo(context, culture, value, destinationType)); }
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { var castValue = value as ConfiguredListParameter; if (castValue != null) { // 99% of the time this type of parameter will be associated // with an ordinal list - List<T> or T[] sort of thing... var instantiatableType = GetInstantiableListType(destinationType); if (instantiatableType != null) { var generics = instantiatableType.GetGenericArguments(); var collection = (IList)Activator.CreateInstance(instantiatableType); foreach (string item in castValue.List) { collection.Add(TypeManipulation.ChangeToCompatibleType(item, generics[0])); } return(collection); } // ...but there is a very small chance this is a Dictionary<int, T> where // the keys are all 0-based and ordinal. This clause checks for // that one edge case. We should only have gotten here if // ConfigurationExtensions.GetConfiguredParameterValue saw // a 0-based configuration dictionary. instantiatableType = GetInstantiableDictionaryType(destinationType); if (instantiatableType != null) { var dictionary = (IDictionary)Activator.CreateInstance(instantiatableType); var generics = instantiatableType.GetGenericArguments(); for (int i = 0; i < castValue.List.Length; i++) { var convertedKey = TypeManipulation.ChangeToCompatibleType(i, generics[0]); var convertedValue = TypeManipulation.ChangeToCompatibleType(castValue.List[i], generics[1]); dictionary.Add(convertedKey, convertedValue); } return(dictionary); } } return(base.ConvertTo(context, culture, value, destinationType)); }
public static object ChangeToCompatibleType(object value, Type destinationType, ICustomAttributeProvider memberInfo) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (value == null) { if (!destinationType.IsValueType) { return(null); } return(Activator.CreateInstance(destinationType)); } else { TypeConverter typeConverter; if (memberInfo != null) { TypeConverterAttribute typeConverterAttribute = memberInfo.GetCustomAttributes(typeof(TypeConverterAttribute), true).Cast <TypeConverterAttribute>().FirstOrDefault <TypeConverterAttribute>(); if (typeConverterAttribute != null && !string.IsNullOrEmpty(typeConverterAttribute.ConverterTypeName)) { typeConverter = TypeManipulation.GetTypeConverterFromName(typeConverterAttribute.ConverterTypeName); if (typeConverter.CanConvertFrom(value.GetType())) { return(typeConverter.ConvertFrom(value)); } } } typeConverter = TypeDescriptor.GetConverter(value.GetType()); if (typeConverter.CanConvertTo(destinationType)) { return(typeConverter.ConvertTo(value, destinationType)); } if (destinationType.IsInstanceOfType(value)) { return(value); } typeConverter = TypeDescriptor.GetConverter(destinationType); if (typeConverter.CanConvertFrom(value.GetType())) { return(typeConverter.ConvertFrom(value)); } if (value is string) { MethodInfo method = destinationType.GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public); if (method != null) { object[] array = new object[2]; array[0] = value; object[] array2 = array; if ((bool)method.Invoke(null, array2)) { return(array2[1]); } } } throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, ConfigurationSettingsReaderResources.TypeConversionUnsupported, new object[] { value.GetType(), destinationType })); } }