Browse Source
Merge pull request #6533 from YohDeadfall/faster-type-utils
Compiler intrinsic based type nullability check
pull/6559/head
Dan Walmsley
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
14 additions and
2 deletions
-
src/Avalonia.Base/Data/Converters/FuncValueConverter.cs
-
src/Avalonia.Base/Utilities/TypeUtilities.cs
|
|
|
@ -26,7 +26,7 @@ namespace Avalonia.Data.Converters |
|
|
|
/// <inheritdoc/>
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
|
|
|
{ |
|
|
|
if (value is TIn || (value == null && TypeUtilities.AcceptsNull(typeof(TIn)))) |
|
|
|
if (TypeUtilities.CanCast<TIn>(value)) |
|
|
|
{ |
|
|
|
return _convert((TIn)value); |
|
|
|
} |
|
|
|
|
|
|
|
@ -3,6 +3,7 @@ using System.ComponentModel; |
|
|
|
using System.Globalization; |
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
|
|
|
|
namespace Avalonia.Utilities |
|
|
|
{ |
|
|
|
@ -93,6 +94,17 @@ namespace Avalonia.Utilities |
|
|
|
return !type.IsValueType || IsNullableType(type); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a value indicating whether null can be assigned to the specified type.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type</typeparam>
|
|
|
|
/// <returns>True if the type accepts null values; otherwise false.</returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public static bool AcceptsNull<T>() |
|
|
|
{ |
|
|
|
return default(T) is null; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a value indicating whether value can be casted to the specified type.
|
|
|
|
/// If value is null, checks if instances of that type can be null.
|
|
|
|
@ -102,7 +114,7 @@ namespace Avalonia.Utilities |
|
|
|
/// <returns>True if the cast is possible, otherwise false.</returns>
|
|
|
|
public static bool CanCast<T>(object value) |
|
|
|
{ |
|
|
|
return value is T || (value is null && AcceptsNull(typeof(T))); |
|
|
|
return value is T || (value is null && AcceptsNull<T>()); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|