Browse Source

Filter out CollectionConverter

Making sure to call Parse when conversion with TypeConverter is not possible
pull/4587/head
Luis von der Eltz 6 years ago
parent
commit
faad15571a
  1. 57
      src/Avalonia.Diagnostics/Diagnostics/ViewModels/PropertyViewModel.cs

57
src/Avalonia.Diagnostics/Diagnostics/ViewModels/PropertyViewModel.cs

@ -25,54 +25,47 @@ namespace Avalonia.Diagnostics.ViewModels
return "(null)"; return "(null)";
} }
//Check if there's an user-provided ToString(), prefer that over the TypeDescriptor conversion var converter = TypeDescriptor.GetConverter(value);
if (value.GetType().GetMethod(nameof(ToString), System.Type.EmptyTypes)
.DeclaringType != typeof(object)) //CollectionConverter does not deliver any important information. It just displays "(Collection)".
if (!converter.CanConvertTo(typeof(string)) ||
converter.GetType() == typeof(CollectionConverter))
{ {
return value.ToString(); return value.ToString();
} }
try return converter.ConvertToString(value);
{ }
var converter = TypeDescriptor.GetConverter(value);
private static object InvokeParse(string s, Type targetType)
{
var method = targetType.GetMethod("Parse", PublicStatic, null, StringIFormatProviderParameters, null);
return converter.ConvertToString(value); if (method != null)
{
return method.Invoke(null, new object[] { s, CultureInfo.InvariantCulture });
} }
catch
method = targetType.GetMethod("Parse", PublicStatic, null, StringParameter, null);
if (method != null)
{ {
return value.ToString(); return method.Invoke(null, new object[] { s });
} }
throw new InvalidCastException("Unable to convert value.");
} }
protected static object ConvertFromString(string s, Type targetType) protected static object ConvertFromString(string s, Type targetType)
{ {
try var converter = TypeDescriptor.GetConverter(targetType);
{
var converter = TypeDescriptor.GetConverter(targetType);
if (converter.CanConvertFrom(typeof(string))) if (converter.CanConvertFrom(typeof(string)))
{
return converter.ConvertFrom(null, CultureInfo.InvariantCulture, s);
}
}
catch
{ {
var method = targetType.GetMethod("Parse", PublicStatic, null, StringIFormatProviderParameters, null); return converter.ConvertFrom(null, CultureInfo.InvariantCulture, s);
if (method != null)
{
return method.Invoke(null, new object[] { s, CultureInfo.InvariantCulture });
}
method = targetType.GetMethod("Parse", PublicStatic, null, StringParameter, null);
if (method != null)
{
return method.Invoke(null, new object[] { s });
}
} }
throw new InvalidCastException("Unable to convert value."); return InvokeParse(s, targetType);
} }
} }
} }

Loading…
Cancel
Save