|
|
|
@ -92,8 +92,7 @@ namespace Avalonia.Utilities |
|
|
|
/// <returns>True if the type accepts null values; otherwise false.</returns>
|
|
|
|
public static bool AcceptsNull(Type type) |
|
|
|
{ |
|
|
|
var t = type.GetTypeInfo(); |
|
|
|
return !t.IsValueType || (t.IsGenericType && (t.GetGenericTypeDefinition() == typeof(Nullable<>))); |
|
|
|
return !type.IsValueType || IsNullableType(type); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -119,10 +118,8 @@ namespace Avalonia.Utilities |
|
|
|
} |
|
|
|
|
|
|
|
var from = value.GetType(); |
|
|
|
var fromTypeInfo = from.GetTypeInfo(); |
|
|
|
var toTypeInfo = to.GetTypeInfo(); |
|
|
|
|
|
|
|
if (toTypeInfo.IsAssignableFrom(fromTypeInfo)) |
|
|
|
if (to.IsAssignableFrom(from)) |
|
|
|
{ |
|
|
|
result = value; |
|
|
|
return true; |
|
|
|
@ -134,7 +131,7 @@ namespace Avalonia.Utilities |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
if (toTypeInfo.IsEnum && from == typeof(string)) |
|
|
|
if (to.IsEnum && from == typeof(string)) |
|
|
|
{ |
|
|
|
if (Enum.IsDefined(to, (string)value)) |
|
|
|
{ |
|
|
|
@ -143,7 +140,7 @@ namespace Avalonia.Utilities |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!fromTypeInfo.IsEnum && toTypeInfo.IsEnum) |
|
|
|
if (!from.IsEnum && to.IsEnum) |
|
|
|
{ |
|
|
|
result = null; |
|
|
|
|
|
|
|
@ -154,7 +151,7 @@ namespace Avalonia.Utilities |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (fromTypeInfo.IsEnum && IsNumeric(to)) |
|
|
|
if (from.IsEnum && IsNumeric(to)) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
@ -223,10 +220,8 @@ namespace Avalonia.Utilities |
|
|
|
} |
|
|
|
|
|
|
|
var from = value.GetType(); |
|
|
|
var fromTypeInfo = from.GetTypeInfo(); |
|
|
|
var toTypeInfo = to.GetTypeInfo(); |
|
|
|
|
|
|
|
if (toTypeInfo.IsAssignableFrom(fromTypeInfo)) |
|
|
|
if (to.IsAssignableFrom(from)) |
|
|
|
{ |
|
|
|
result = value; |
|
|
|
return true; |
|
|
|
@ -307,9 +302,7 @@ namespace Avalonia.Utilities |
|
|
|
/// <returns>The default value.</returns>
|
|
|
|
public static object Default(Type type) |
|
|
|
{ |
|
|
|
var typeInfo = type.GetTypeInfo(); |
|
|
|
|
|
|
|
if (typeInfo.IsValueType) |
|
|
|
if (type.IsValueType) |
|
|
|
{ |
|
|
|
return Activator.CreateInstance(type); |
|
|
|
} |
|
|
|
@ -335,9 +328,11 @@ namespace Avalonia.Utilities |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) |
|
|
|
Type underlyingType = Nullable.GetUnderlyingType(type); |
|
|
|
|
|
|
|
if (underlyingType != null) |
|
|
|
{ |
|
|
|
return IsNumeric(Nullable.GetUnderlyingType(type)); |
|
|
|
return IsNumeric(underlyingType); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
@ -352,6 +347,11 @@ namespace Avalonia.Utilities |
|
|
|
Explicit = 2 |
|
|
|
} |
|
|
|
|
|
|
|
private static bool IsNullableType(Type type) |
|
|
|
{ |
|
|
|
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); |
|
|
|
} |
|
|
|
|
|
|
|
private static MethodInfo FindTypeConversionOperatorMethod(Type fromType, Type toType, OperatorType operatorType) |
|
|
|
{ |
|
|
|
const string implicitName = "op_Implicit"; |
|
|
|
|