public override object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (destinationType == typeof (object))
return value;
string str_val = value as String;
if (str_val != null) {
if (destinationType.IsEnum)
return Enum.Parse (destinationType, str_val, true);
if (destinationType == typeof (GridLength)) {
if (str_val == "Auto")
return new GridLength (1, GridUnitType.Auto);
else {
var length = 1.0;
var type = str_val.EndsWith ("*") ? GridUnitType.Star : GridUnitType.Pixel;
if (type == GridUnitType.Star)
str_val = str_val.Substring (0, str_val.Length - 1);
if (str_val.Length > 0)
length = double.Parse (str_val);
return new GridLength (length, type);
}
}
if (destinationType == typeof (int))
return int.Parse (str_val, NumberStyles.Any, null);
if (destinationType == typeof (TimeSpan))
return TimeSpan.Parse (str_val);
if (destinationType == typeof (FontWeight))
return new FontWeight ((FontWeightKind) Enum.Parse (typeof (FontWeightKind), str_val, true));
if (destinationType == typeof (FontStyle))
return new FontStyle ((FontStyleKind) Enum.Parse (typeof (FontStyleKind), str_val, true));
if (destinationType == typeof (FontStretch))
return new FontStretch ((FontStretchKind) Enum.Parse (typeof (FontStretchKind), str_val, true));
if (destinationType == typeof (Cursor))
return Cursors.FromEnum ((CursorType) Enum.Parse (typeof (CursorType), str_val, true));
if (destinationType == typeof (CacheMode)) {
if (str_val == "BitmapCache")
return new BitmapCache ();
}
if (destinationType == typeof (System.Globalization.CultureInfo))
return CultureInfo.GetCultureInfo (str_val);
if (destinationType == typeof (ImageSource) ||
destinationType == typeof (BitmapSource) ||
destinationType == typeof (BitmapImage))
return new BitmapImage (new Uri (str_val, UriKind.RelativeOrAbsolute));
}
if (value is Color && destinationType.IsAssignableFrom(typeof(SolidColorBrush))) {
return new SolidColorBrush ((Color)value);
}
if (IsAssignableToIConvertible (value.GetType ()) && IsAssignableToIConvertible (destinationType))
return ValueFromConvertible (destinationType, (IConvertible) value);
if (str_val != null) {
Kind k = destinationKind;
IntPtr unmanaged_value = IntPtr.Zero;
try {
if (NativeMethods.value_from_str (k,
propertyName,
str_val,
out unmanaged_value)) {
value = Value.ToObject (destinationType, unmanaged_value);
return value;
}
} finally {
NativeMethods.value_delete_value2 (unmanaged_value);
}
}
if (destinationType.IsAssignableFrom (value.GetType ()))
return value;
if (!base.CanConvertFrom (context, value.GetType ())) {
Console.Error.WriteLine ("MoonlightTypeConverter: Cannot convert from type {0} to type {1}", value.GetType(), destinationType);
}
return base.ConvertFrom (context, culture, value);
}