public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { TypeConverter converter = WhiskeyTypeConverters.lookUp(value.GetType().Name); if (converter != null) { return(converter.ConvertTo(context, culture, value, destinationType)); } if (ConverterManager.Instance.hasConverter(value.GetType())) { return(ConverterManager.Instance.convertToString(value)); } else { try { return(Convert.ChangeType(value, destinationType)); } catch (Exception e) { return(value.ToString()); } } } return(base.ConvertTo(context, culture, value, destinationType)); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value.GetType() == typeof(string)) { PropertyAdapter adapter = (PropertyAdapter)context.Instance; string name = context.PropertyDescriptor.Name; GeneralPropertyDescriptor gpd = (GeneralPropertyDescriptor)context.PropertyDescriptor; WhiskeyProperty whiskeyProp = adapter.WhiskeyPropertys.Where((w) => { return(w.Name.Equals(name)); }).ToList()[0]; Type toType = whiskeyProp.TypeVal.Value.GetType(); TypeConverter converter = WhiskeyTypeConverters.lookUp(toType.Name); if (converter != null && value != null && converter.CanConvertFrom(value.GetType())) { object converted = converter.ConvertFrom(context, culture, value); whiskeyProp.TypeVal.Value = converted; } if (ConverterManager.Instance.hasConverter(toType)) { whiskeyProp.TypeVal.Value = ConverterManager.Instance.convertFromString(toType, (string)value); } else { try { whiskeyProp.TypeVal.Value = Convert.ChangeType(value, toType); } catch (Exception e) { //??? } } return(gpd.PropValue); } return(base.ConvertFrom(context, culture, value)); }