2021-04-11 01:11:19 +08:00
|
|
|
|
// Licensed to the .NET Foundation under one or more agreements.
|
|
|
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public static class THelper
|
|
|
|
|
{
|
|
|
|
|
public static T ChangeType<T>(object value)
|
2021-05-06 13:00:16 +08:00
|
|
|
|
{
|
|
|
|
|
return ChangeType<T>(value, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T ChangeType<T>(object value, IFormatProvider provider)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
|
|
|
|
var t = typeof(T);
|
|
|
|
|
|
|
|
|
|
if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
2022-10-21 13:01:25 +08:00
|
|
|
|
return default;
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t = Nullable.GetUnderlyingType(t);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 13:00:16 +08:00
|
|
|
|
if (provider == null) return (T)Convert.ChangeType(value, t);
|
|
|
|
|
return (T)Convert.ChangeType(value, t, provider);
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
2021-05-08 13:00:52 +08:00
|
|
|
|
|
2021-04-11 01:11:19 +08:00
|
|
|
|
public static bool IsTypeNullable<T>()
|
|
|
|
|
{
|
2023-10-14 20:38:10 +08:00
|
|
|
|
return IsTypeNullable(typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsTypeNullable(Type type)
|
|
|
|
|
{
|
|
|
|
|
return Nullable.GetUnderlyingType(type) != null;
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Type GetNullableType<T>()
|
|
|
|
|
{
|
2022-10-21 13:01:25 +08:00
|
|
|
|
var type = typeof(T);
|
2021-04-11 01:11:19 +08:00
|
|
|
|
type = Nullable.GetUnderlyingType(type) ?? type;
|
|
|
|
|
if (type.IsValueType)
|
|
|
|
|
return typeof(Nullable<>).MakeGenericType(type);
|
|
|
|
|
else
|
|
|
|
|
return type;
|
|
|
|
|
}
|
2021-05-08 13:00:52 +08:00
|
|
|
|
|
2021-04-11 01:11:19 +08:00
|
|
|
|
public static Type GetUnderlyingType<T>()
|
|
|
|
|
{
|
2022-10-21 13:01:25 +08:00
|
|
|
|
var type = typeof(T);
|
2021-04-11 01:11:19 +08:00
|
|
|
|
Type targetType;
|
2023-01-05 15:47:52 +08:00
|
|
|
|
if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
|
|
|
{
|
|
|
|
|
targetType = Nullable.GetUnderlyingType(type);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
targetType = type;
|
|
|
|
|
}
|
|
|
|
|
return targetType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Type GetUnderlyingType(this Type type)
|
|
|
|
|
{
|
|
|
|
|
Type targetType;
|
2021-04-11 01:11:19 +08:00
|
|
|
|
if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
|
|
|
{
|
|
|
|
|
targetType = Nullable.GetUnderlyingType(type);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
targetType = type;
|
|
|
|
|
}
|
|
|
|
|
return targetType;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-14 20:38:10 +08:00
|
|
|
|
public static bool IsNumericType(this Type type)
|
2021-04-11 01:11:19 +08:00
|
|
|
|
{
|
2023-10-14 20:38:10 +08:00
|
|
|
|
return type != null
|
|
|
|
|
&& Type.GetTypeCode(type)
|
|
|
|
|
is TypeCode.Byte
|
|
|
|
|
or TypeCode.Decimal
|
|
|
|
|
or TypeCode.Double
|
|
|
|
|
or TypeCode.Int16
|
|
|
|
|
or TypeCode.Int32
|
|
|
|
|
or TypeCode.Int64
|
|
|
|
|
or TypeCode.SByte
|
|
|
|
|
or TypeCode.Single
|
|
|
|
|
or TypeCode.UInt16
|
|
|
|
|
or TypeCode.UInt32
|
|
|
|
|
or TypeCode.UInt64;
|
2021-04-11 01:11:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|