committed by
GitHub
9 changed files with 122 additions and 15 deletions
@ -0,0 +1,24 @@ |
|||
using System; |
|||
using System.Globalization; |
|||
using Avalonia.Data; |
|||
using Avalonia.Data.Converters; |
|||
|
|||
namespace Avalonia.Diagnostics.Converters |
|||
{ |
|||
internal class GetTypeNameConverter : IValueConverter |
|||
{ |
|||
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) |
|||
{ |
|||
if (value is Type type) |
|||
{ |
|||
return type.GetTypeName(); |
|||
} |
|||
return BindingOperations.DoNothing; |
|||
} |
|||
|
|||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) |
|||
{ |
|||
return BindingOperations.DoNothing; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
using System; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Linq; |
|||
|
|||
namespace Avalonia.Diagnostics |
|||
{ |
|||
internal static class TypeExtesnions |
|||
{ |
|||
private static readonly ConditionalWeakTable<Type, string> s_getTypeNameCache = |
|||
new ConditionalWeakTable<Type, string>(); |
|||
|
|||
public static string GetTypeName(this Type type) |
|||
{ |
|||
if (!s_getTypeNameCache.TryGetValue(type, out var name)) |
|||
{ |
|||
name = type.Name; |
|||
if (Nullable.GetUnderlyingType(type) is Type nullable) |
|||
{ |
|||
name = nullable.Name + "?"; |
|||
} |
|||
else if (type.IsGenericType) |
|||
{ |
|||
var definition = type.GetGenericTypeDefinition(); |
|||
var arguments = type.GetGenericArguments(); |
|||
name = definition.Name.Substring(0, definition.Name.IndexOf('`')); |
|||
name = $"{name}<{string.Join(",", arguments.Select(GetTypeName))}>"; |
|||
} |
|||
s_getTypeNameCache.Add(type, name); |
|||
} |
|||
return name; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue