protected virtual TConverterBase CreateInstance()
        {
            if (InitialConverterInstance != null)
            {
                return(InitialConverterInstance);
            }
            if (ConverterType == null)
            {
                return(null);
            }

            if (!typeof(TConverterBase).GetTypeInfo().IsAssignableFrom(ConverterType.GetTypeInfo()))
            {
                throw new XamlSchemaException(String.Format("ConverterType '{0}' is not derived from '{1}' type", ConverterType, typeof(TConverterBase)));
            }

            if (ConverterType.GetTypeInfo().GetConstructors().Any(r => r.GetParameters().Length == 0))
            {
                return((TConverterBase)Activator.CreateInstance(ConverterType));
            }

            if (ConverterType.GetTypeInfo().GetConstructors().Any(r => r.GetParameters().Length == 1 && r.GetParameters()[0].ParameterType == typeof(Type)))
            {
                return((TConverterBase)Activator.CreateInstance(ConverterType, TargetType.UnderlyingType));
            }
            return(null);
        }
        protected virtual TConverterBase CreateInstance()
        {
            if (ConverterType == null)
            {
                return(null);
            }

            if (!typeof(TConverterBase).GetTypeInfo().IsAssignableFrom(ConverterType.GetTypeInfo()))
            {
                throw new XamlSchemaException(String.Format("ConverterType '{0}' is not derived from '{1}' type", ConverterType, typeof(TConverterBase)));
            }

            if (TargetType != null && TargetType.UnderlyingType != null)
            {
                // special case: Enum
                if (TargetType.UnderlyingType.GetTypeInfo().IsEnum)
                {
                    return((TConverterBase)(object)new EnumConverter(TargetType.UnderlyingType));
                }
                // special case: Nullable<T>
                if (TargetType.IsNullable && TargetType.UnderlyingType.GetTypeInfo().IsValueType)
                {
                    return((TConverterBase)(object)new NullableConverter(TargetType.UnderlyingType));
                }
            }

            if (ConverterType.GetTypeInfo().GetConstructors().All(r => r.GetParameters().Length > 0))
            {
                return(null);
            }

            return((TConverterBase)Activator.CreateInstance(ConverterType));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WebApiArgFormatterAttribute"/> class.
 /// </summary>
 /// <param name="converterType">The converter <see cref="Type"/> (implements <see cref="IPropertyMapperConverter"/>).</param>
 public WebApiArgFormatterAttribute(Type converterType)
 {
     ConverterType = Check.NotNull(converterType, nameof(converterType));
     if (!typeof(IPropertyMapperConverter).IsAssignableFrom(ConverterType.GetTypeInfo()))
     {
         throw new ArgumentException($"Type '{converterType.Name}' must implement 'IPropertyMapperConverter'.", nameof(converterType));
     }
 }
Exemplo n.º 4
0
        protected override TypeConverter CreateInstance()
        {
            if (ConverterType == null)
            {
                return(null);
            }

            var converterTypeInfo = ConverterType.GetTypeInfo();

            if (!typeof(TypeConverter).GetTypeInfo().IsAssignableFrom(converterTypeInfo) &&
                !ReflectionHelpers.TypeConverterType.GetTypeInfo().IsAssignableFrom(converterTypeInfo))
            {
                throw new XamlSchemaException($"ConverterType '{ConverterType}' is not derived from '{typeof(TypeConverter)}' or '{ReflectionHelpers.TypeConverterType}'");
            }

            object instance = null;

            if (converterTypeInfo.GetConstructors().Any(r => r.GetParameters().Length == 0))
            {
                instance = Activator.CreateInstance(ConverterType);
            }

            if (converterTypeInfo.GetConstructors().Any(r => r.GetParameters().Length == 1 && r.GetParameters()[0].ParameterType == typeof(Type)))
            {
                instance = Activator.CreateInstance(ConverterType, TargetType.UnderlyingType);
            }

            if (ReferenceEquals(instance, null))
            {
                return(null);
            }

            var tc = instance as TypeConverter;

            if (tc != null)
            {
                return(tc);
            }

            return(new SystemTypeConverter(instance));
        }