|
|
|
@ -8,8 +8,8 @@ namespace Avalonia.Diagnostics.ViewModels |
|
|
|
internal abstract class PropertyViewModel : ViewModelBase |
|
|
|
{ |
|
|
|
private const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static; |
|
|
|
private static readonly Type[] StringParameter = new[] { typeof(string) }; |
|
|
|
private static readonly Type[] StringIFormatProviderParameters = new[] { typeof(string), typeof(IFormatProvider) }; |
|
|
|
private static readonly Type[] StringParameter = { typeof(string) }; |
|
|
|
private static readonly Type[] StringIFormatProviderParameters = { typeof(string), typeof(IFormatProvider) }; |
|
|
|
|
|
|
|
public abstract object Key { get; } |
|
|
|
public abstract string Name { get; } |
|
|
|
@ -26,35 +26,46 @@ namespace Avalonia.Diagnostics.ViewModels |
|
|
|
} |
|
|
|
|
|
|
|
var converter = TypeDescriptor.GetConverter(value); |
|
|
|
return converter?.ConvertToString(value) ?? value.ToString(); |
|
|
|
|
|
|
|
//CollectionConverter does not deliver any important information. It just displays "(Collection)".
|
|
|
|
if (!converter.CanConvertTo(typeof(string)) || |
|
|
|
converter.GetType() == typeof(CollectionConverter)) |
|
|
|
{ |
|
|
|
return value.ToString(); |
|
|
|
} |
|
|
|
|
|
|
|
return converter.ConvertToString(value); |
|
|
|
} |
|
|
|
|
|
|
|
protected static object ConvertFromString(string s, Type targetType) |
|
|
|
private static object InvokeParse(string s, Type targetType) |
|
|
|
{ |
|
|
|
var converter = TypeDescriptor.GetConverter(targetType); |
|
|
|
|
|
|
|
if (converter != null && converter.CanConvertFrom(typeof(string))) |
|
|
|
var method = targetType.GetMethod("Parse", PublicStatic, null, StringIFormatProviderParameters, null); |
|
|
|
|
|
|
|
if (method != null) |
|
|
|
{ |
|
|
|
return converter.ConvertFrom(null, CultureInfo.InvariantCulture, s); |
|
|
|
return method.Invoke(null, new object[] { s, CultureInfo.InvariantCulture }); |
|
|
|
} |
|
|
|
else |
|
|
|
|
|
|
|
method = targetType.GetMethod("Parse", PublicStatic, null, StringParameter, null); |
|
|
|
|
|
|
|
if (method != null) |
|
|
|
{ |
|
|
|
var method = targetType.GetMethod("Parse", PublicStatic, null, StringIFormatProviderParameters, null); |
|
|
|
return method.Invoke(null, new object[] { s }); |
|
|
|
} |
|
|
|
|
|
|
|
if (method != null) |
|
|
|
{ |
|
|
|
return method.Invoke(null, new object[] { s, CultureInfo.InvariantCulture }); |
|
|
|
} |
|
|
|
throw new InvalidCastException("Unable to convert value."); |
|
|
|
} |
|
|
|
|
|
|
|
method = targetType.GetMethod("Parse", PublicStatic, null, StringParameter, null); |
|
|
|
protected static object ConvertFromString(string s, Type targetType) |
|
|
|
{ |
|
|
|
var converter = TypeDescriptor.GetConverter(targetType); |
|
|
|
|
|
|
|
if (method != null) |
|
|
|
{ |
|
|
|
return method.Invoke(null, new object[] { s }); |
|
|
|
} |
|
|
|
if (converter.CanConvertFrom(typeof(string))) |
|
|
|
{ |
|
|
|
return converter.ConvertFrom(null, CultureInfo.InvariantCulture, s); |
|
|
|
} |
|
|
|
|
|
|
|
throw new InvalidCastException("Unable to convert value."); |
|
|
|
return InvokeParse(s, targetType); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|