/// <inheritdoc />
        public override bool TryConvert(object value, Type destinationType, out object result, ConversionArgs args)
        {
            System.ComponentModel.TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
            if (converter.GetType() != typeof(System.ComponentModel.TypeConverter) && converter.CanConvertFrom(value.GetType()))
            {
                try {
                    result = converter.ConvertFrom(null, args.Culture, value);
                    return(true);
                }
                catch {
                }
            }

            converter = TypeDescriptor.GetConverter(value.GetType());
            if (converter.GetType() != typeof(System.ComponentModel.TypeConverter) && converter.CanConvertTo(destinationType))
            {
                try {
                    result = converter.ConvertTo(null, args.Culture, value, destinationType);
                    return(true);
                }
                catch {
                }
            }

            result = null;
            return(false);
        }
        public bool IsForType(TypeConverter converter) {
            Contract.Requires<ArgumentNullException>(converter != null);

            Type currentConverterType = this.typeConverter.GetType();
            Type paramConverterType = converter.GetType();
            return paramConverterType.IsAssignableFrom(currentConverterType);
        }
Esempio n. 3
0
 private static string GetTypeConverterId(System.ComponentModel.TypeConverter typeConverter)
 {
     if (typeConverter == null)
     {
         return(string.Empty);
     }
     return(typeConverter.GetType().FullName);
 }
		public void InitializeConverter()
		{
			if ( _converter != null )
			{
				return;
			}

			_converter = TypeDescriptor.GetConverter( _type );
			if ( _converter.GetType() == GetType() )
			{
				string message = string.Format( "Conversion failed. Converter for {0} is missing in TypeDescriptor.", _type );
				throw new InvalidOperationException( message );
			}
		}
 private static void DebugValidate(TypeConverter converter, object instance, bool noCustomTypeDesc)
 {
     #if DEBUG
     if (!DebugShouldValidate(instance)) return;
     TypeConverter debugConverter = DebugTypeDescriptor.GetConverter(instance, noCustomTypeDesc);
     Debug.Assert(debugConverter.GetType() == converter.GetType(), "TypeDescriptor engine Validation Failure.");
     #endif
 }
 private static void DebugValidate(TypeConverter converter, Type type)
 {
     #if DEBUG
     if (!DebugShouldValidate(type)) return;
     TypeConverter debugConverter = DebugTypeDescriptor.GetConverter(type);
     Debug.Assert(debugConverter.GetType() == converter.GetType(), "TypeDescriptor engine Validation Failure.");
     #endif
 }
Esempio n. 7
0
 private bool GetIfConverterCanConvert(TypeConverter converter)
 {
     string converterTypeName = converter.GetType().Name;
     if (converterTypeName == "MatrixConverter" ||
         converterTypeName == "CollectionConverter"
         )
     {
         return false;
     }
     return true;
 }
 private void Write4_TypeConverter(string n, string ns, TypeConverter o, bool isNullable, bool needType)
 {
     if (o == null)
     {
         if (isNullable)
         {
             base.WriteNullTagLiteral(n, ns);
         }
     }
     else
     {
         if (!needType)
         {
             System.Type type = o.GetType();
             if (type != typeof(TypeConverter))
             {
                 if (type == typeof(EncodingConveter))
                 {
                     this.Write132_EncodingConveter(n, ns, (EncodingConveter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(AudioChannelsTypeConverter))
                 {
                     this.Write125_AudioChannelsTypeConverter(n, ns, (AudioChannelsTypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(PropertyTypeConverter))
                 {
                     this.Write123_PropertyTypeConverter(n, ns, (PropertyTypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(RatingTypeConverter))
                 {
                     this.Write131_RatingTypeConverter(n, ns, (RatingTypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(ISOSpeedTypeConverter))
                 {
                     this.Write130_ISOSpeedTypeConverter(n, ns, (ISOSpeedTypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(DPITypeConverter))
                 {
                     this.Write129_DPITypeConverter(n, ns, (DPITypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(ImageSizeTypeConverter))
                 {
                     this.Write128_ImageSizeTypeConverter(n, ns, (ImageSizeTypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(DurationTypeConverter))
                 {
                     this.Write127_DurationTypeConverter(n, ns, (DurationTypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(AudioSampleRateTypeConverter))
                 {
                     this.Write126_AudioSampleRateTypeConverter(n, ns, (AudioSampleRateTypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(BitrateTypeConverter))
                 {
                     this.Write124_BitrateTypeConverter(n, ns, (BitrateTypeConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(ArchiveFormatConverter))
                 {
                     this.Write35_ArchiveFormatConverter(n, ns, (ArchiveFormatConverter) o, isNullable, true);
                     return;
                 }
                 if (type == typeof(KeysConverter))
                 {
                     this.Write5_KeysConverter(n, ns, (KeysConverter) o, isNullable, true);
                     return;
                 }
                 if (type != typeof(KeysConverter2))
                 {
                     throw base.CreateUnknownTypeException(o);
                 }
                 this.Write6_KeysConverter2(n, ns, (KeysConverter2) o, isNullable, true);
                 return;
             }
         }
         base.WriteStartElement(n, ns, o, false, null);
         if (needType)
         {
             base.WriteXsiType("TypeConverter", "");
         }
         base.WriteEndElement(o);
     }
 }