예제 #1
0
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                var instantiableType  = GetInstantiableType(destinationType);
                var elementCollection = (ListElementCollection)value;

                if (elementCollection == null || !(instantiableType != null))
                {
                    return(base.ConvertTo(context, culture, value, destinationType));
                }
                var genericArguments = instantiableType.GetGenericArguments();
                var list             = (IList)Activator.CreateInstance(instantiableType);

                foreach (var listItemElement in elementCollection)
                {
                    list.Add(TypeManipulation.ChangeToCompatibleType(listItemElement.Value, genericArguments[0], null));
                }
                return(list);
            }
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType)
            {
                var instantiableType = GetInstantiableType(destinationType);

                if (!(value is DictionaryElementCollection elementCollection) || !(instantiableType != null))
                {
                    return(base.ConvertTo(context, culture, value, destinationType));
                }
                var dictionary       = (IDictionary)Activator.CreateInstance(instantiableType);
                var genericArguments = instantiableType.GetGenericArguments();

                foreach (var listItemElement in elementCollection)
                {
                    if (string.IsNullOrEmpty(listItemElement.Key))
                    {
                        throw new ConfigurationErrorsException("Key cannot be null in a dictionary element.");
                    }
                    var compatibleType1 = TypeManipulation.ChangeToCompatibleType(listItemElement.Key, genericArguments[0], null);
                    var compatibleType2 = TypeManipulation.ChangeToCompatibleType(listItemElement.Value, genericArguments[1], null);
                    dictionary.Add(compatibleType1, compatibleType2);
                }
                return(dictionary);
            }
 public IEnumerable <Parameter> ToParameters()
 {
     foreach (var parameterElement in this)
     {
         var localParameter = parameterElement;
         yield return(new ResolvedParameter((pi, c) => pi.Name == localParameter.Name, (pi, c) => TypeManipulation.ChangeToCompatibleType(localParameter.CoerceValue(), pi.ParameterType, (ICustomAttributeProvider)pi)));
     }
 }