Browse Source

Preferring user-provided string conversion over TypeDescriptor

pull/4587/head
Luis von der Eltz 6 years ago
parent
commit
bc0ada0094
  1. 10
      src/Avalonia.Controls/ColumnDefinitions.cs
  2. 37
      src/Avalonia.Diagnostics/Diagnostics/ViewModels/PropertyViewModel.cs

10
src/Avalonia.Controls/ColumnDefinitions.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Linq; using System.Linq;
using System.Text;
using Avalonia.Collections; using Avalonia.Collections;
namespace Avalonia.Controls namespace Avalonia.Controls
@ -13,7 +14,7 @@ namespace Avalonia.Controls
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ColumnDefinitions"/> class. /// Initializes a new instance of the <see cref="ColumnDefinitions"/> class.
/// </summary> /// </summary>
public ColumnDefinitions() : base () public ColumnDefinitions()
{ {
} }
@ -27,6 +28,11 @@ namespace Avalonia.Controls
AddRange(GridLength.ParseLengths(s).Select(x => new ColumnDefinition(x))); AddRange(GridLength.ParseLengths(s).Select(x => new ColumnDefinition(x)));
} }
public override string ToString()
{
return string.Join(",", this.Select(x => x.Width));
}
/// <summary> /// <summary>
/// Parses a string representation of column definitions collection. /// Parses a string representation of column definitions collection.
/// </summary> /// </summary>
@ -34,4 +40,4 @@ namespace Avalonia.Controls
/// <returns>The <see cref="ColumnDefinitions"/>.</returns> /// <returns>The <see cref="ColumnDefinitions"/>.</returns>
public static ColumnDefinitions Parse(string s) => new ColumnDefinitions(s); public static ColumnDefinitions Parse(string s) => new ColumnDefinitions(s);
} }
} }

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

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.Reflection; using System.Reflection;
@ -8,8 +9,8 @@ namespace Avalonia.Diagnostics.ViewModels
internal abstract class PropertyViewModel : ViewModelBase internal abstract class PropertyViewModel : ViewModelBase
{ {
private const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static; private const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
private static readonly Type[] StringParameter = new[] { typeof(string) }; private static readonly Type[] StringParameter = { typeof(string) };
private static readonly Type[] StringIFormatProviderParameters = new[] { typeof(string), typeof(IFormatProvider) }; private static readonly Type[] StringIFormatProviderParameters = { typeof(string), typeof(IFormatProvider) };
public abstract object Key { get; } public abstract object Key { get; }
public abstract string Name { get; } public abstract string Name { get; }
@ -25,19 +26,37 @@ namespace Avalonia.Diagnostics.ViewModels
return "(null)"; return "(null)";
} }
var converter = TypeDescriptor.GetConverter(value); //Check if there's an user provided ToString(), prefer that over the TypeDescriptor conversion
return converter?.ConvertToString(value) ?? value.ToString(); if (value.GetType().GetMethod(nameof(ToString), System.Type.EmptyTypes)
.DeclaringType != typeof(object))
{
return value.ToString();
}
try
{
var converter = TypeDescriptor.GetConverter(value);
return converter.ConvertToString(value);
}
catch
{
return value.ToString();
}
} }
protected static object ConvertFromString(string s, Type targetType) protected static object ConvertFromString(string s, Type targetType)
{ {
var converter = TypeDescriptor.GetConverter(targetType); try
if (converter != null && converter.CanConvertFrom(typeof(string)))
{ {
return converter.ConvertFrom(null, CultureInfo.InvariantCulture, s); var converter = TypeDescriptor.GetConverter(targetType);
if (converter.CanConvertFrom(typeof(string)))
{
return converter.ConvertFrom(null, CultureInfo.InvariantCulture, s);
}
} }
else catch
{ {
var method = targetType.GetMethod("Parse", PublicStatic, null, StringIFormatProviderParameters, null); var method = targetType.GetMethod("Parse", PublicStatic, null, StringIFormatProviderParameters, null);

Loading…
Cancel
Save