Beispiel #1
0
        public static Object InvokeConverter <TTarget>(this TTarget target, String parameter, String argument, String delimiter)
        {
            if (!target.HasConverter())
            {
                throw new CustomConverterException(parameter, argument,
                                                   $"Converter invocation impossible because of missing converter for type {((target is Type) ? (target as Type).Name : "<unknown>")}.");
            }

            try
            {
                Object converter = CustomConverterExtension.converters[CustomConverterExtension.GetQualifier(target)];

                MethodInfo method = converter.GetType().GetMethod(nameof(ICustomConverter <TTarget> .Convert));

                return(method.Invoke(converter, new Object[] { parameter, argument, delimiter }));
            }
            catch (Exception exception)
            {
                if (exception.InnerException is CustomConverterException)
                {
                    throw exception.InnerException;
                }
                else
                {
                    throw new CustomConverterException(parameter, argument,
                                                       "Converter invocation has failed. See inner exception for more details.", exception);
                }
            }
        }
Beispiel #2
0
        public static Boolean HasConverter(this Type type)
        {
            if (type is null)
            {
                return(false);
            }

            return(CustomConverterExtension.converters.ContainsKey(CustomConverterExtension.GetQualifier(type)));
        }
Beispiel #3
0
        public static void RemoveConverter <TTarget>(this ICustomConverter <TTarget> converter)
        {
            if (converter == null)
            {
                return;
            }

            String qualifier = CustomConverterExtension.GetQualifier <TTarget>();

            if (CustomConverterExtension.converters.ContainsKey(qualifier))
            {
                CustomConverterExtension.converters.Remove(qualifier);
            }
        }
Beispiel #4
0
        public static void AddConverter <TTarget>(this ICustomConverter <TTarget> converter)
        {
            if (converter == null)
            {
                return;
            }

            String qualifier = CustomConverterExtension.GetQualifier <TTarget>();

            if (!CustomConverterExtension.converters.ContainsKey(qualifier))
            {
                CustomConverterExtension.converters.Add(qualifier, null);
            }

            CustomConverterExtension.converters[qualifier] = converter;
        }
Beispiel #5
0
 /// <summary>
 /// Gets the qualifier for a particular target type.
 /// </summary>
 /// <remarks>
 /// This method returns the qualifier for a particular target type.
 /// </remarks>
 /// <typeparam name="TTarget">
 /// The affected custom type to get the qualifier for.
 /// </typeparam>
 /// <returns>
 /// The qualifier for a particular target type.
 /// </returns>
 /// <see cref="CustomConverterExtension.GetQualifier(Type)"/>
 private static String GetQualifier <TTarget>()
 {
     return(CustomConverterExtension.GetQualifier(typeof(TTarget)));
 }
Beispiel #6
0
 /// <summary>
 /// Gets the qualifier for a particular target type.
 /// </summary>
 /// <remarks>
 /// This method returns the qualifier for a particular target type.
 /// </remarks>
 /// <typeparam name="TTarget">
 /// The affected custom type to get the qualifier for.
 /// </typeparam>
 /// <param name="target">
 /// The target type to get the qualifier for.
 /// </param>
 /// <returns>
 /// The qualifier for a particular target type.
 /// </returns>
 /// <see cref="CustomConverterExtension.GetQualifier(Type)"/>
 private static String GetQualifier <TTarget>(TTarget target)
 {
     return(CustomConverterExtension.GetQualifier(target as Type));
 }