Beispiel #1
0
            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("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));
            }
Beispiel #2
0
            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));
            }