Exemplo n.º 1
0
        public IConverter Create(Type type, object[] converterParameters)
        {
            if (!type.IsArray)
            {
                throw new ArgumentException($"Type '{type}' was expected to be an array.");
            }

            Type       elementType          = type.GetElementType();
            Type       elementConverterType = ConverterFactoryHelper.GetConverterParameter <Type>(typeof(ArrayConverterFactory), converterParameters, 0, 0, 1);
            IConverter elementConverter     = ConverterFactoryHelper.CreateConverter(elementConverterType, elementType, null);

            Type listConverterType = typeof(ArrayConverter <>).MakeGenericType(elementType);

            return((IConverter)Activator.CreateInstance(listConverterType, elementConverter));
        }
Exemplo n.º 2
0
        public IConverter Create(Type type, object[] converterParameters)
        {
            Type genericTypeDef = type.GetGenericTypeDefinition();

            if (genericTypeDef != typeof(List <>) && genericTypeDef != typeof(IList <>))
            {
                throw new ArgumentException($"Type '{type}' was expected to be a generic List<> or IList<>.");
            }

            Type       elementType          = type.GetGenericArguments()[0];
            Type       elementConverterType = ConverterFactoryHelper.GetConverterParameter <Type>(typeof(ListConverterFactory), converterParameters, 0, 0, 1);
            IConverter elementConverter     = ConverterFactoryHelper.CreateConverter(elementConverterType, elementType, null);

            Type listConverterType = typeof(ListConverter <>).MakeGenericType(elementType);

            return((IConverter)Activator.CreateInstance(listConverterType, new object[] { elementConverter }));
        }
Exemplo n.º 3
0
        public IConverter Create(Type type, object[] parameters)
        {
            Type genericTypeDef = type.GetGenericTypeDefinition();

            if (genericTypeDef != typeof(Dictionary <,>) && genericTypeDef != typeof(IDictionary <,>))
            {
                throw new ArgumentException($"Type '{type}' was expected to be a generic Dictionary<,> or IDictionary<,>.");
            }

            Type[] genericArguments = type.GetGenericArguments();
            if (genericArguments[0] != typeof(string))
            {
                throw new ArgumentException($"Type '{type}' was expected to have string keys.");
            }

            Type elementType = genericArguments[1];

            Type elementConverterType = ConverterFactoryHelper.GetConverterParameter <Type>(typeof(DictionaryConverterFactory), parameters, 0, 0, 1);
            Func <IConverter> elementConverterFactory = () => ConverterFactoryHelper.CreateConverter(elementConverterType, elementType, null);

            Type dictConverterType = typeof(DictionaryConverter <>).MakeGenericType(elementType);

            return((IConverter)Activator.CreateInstance(dictConverterType, elementConverterFactory));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a converter for the given type. This converter factory
        /// takes one optional parameter for the name of the type property.
        /// This defaults to 'Type'.
        /// </summary>
        /// <param name="type">The type for which to create the converter.</param>
        /// <param name="parameters">An optional set of converter-specific parameters
        /// for the new converter.</param>
        /// <returns>An instance of <see cref="IConverter"/> which is capable
        /// of converting objects of the given type.</returns>
        public IConverter Create(Type type, object[] parameters)
        {
            string typeProperty = ConverterFactoryHelper.GetConverterParameter <string>(typeof(TypedConverterFactory), parameters, 0, 0, 1);

            return(new TypedConverter(type, typeProperty ?? "Type"));
        }