| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System;
- using System.Globalization;
- namespace Masuit.Tools
- {
- public static class IConvertibleExtensions
- {
- /// <summary>
- /// 类型直转
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="value"></param>
- /// <returns></returns>
- public static T ConvertTo<T>(this IConvertible value) where T : IConvertible
- {
- return (T)ConvertTo(value, typeof(T));
- }
- /// <summary>
- /// 类型直转
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="value"></param>
- /// <param name="defaultValue">转换失败的默认值</param>
- /// <returns></returns>
- public static T TryConvertTo<T>(this IConvertible value, T defaultValue = default) where T : IConvertible
- {
- try
- {
- return (T)ConvertTo(value, typeof(T));
- }
- catch
- {
- return defaultValue;
- }
- }
- /// <summary>
- /// 类型直转
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="value"></param>
- /// <param name="result">转换失败的默认值</param>
- /// <returns></returns>
- public static bool TryConvertTo<T>(this IConvertible value, out T result) where T : IConvertible
- {
- try
- {
- result = (T)ConvertTo(value, typeof(T));
- return true;
- }
- catch
- {
- result = default;
- return false;
- }
- }
- /// <summary>
- /// 类型直转
- /// </summary>
- /// <param name="value"></param>
- /// <param name="type">目标类型</param>
- /// <param name="result">转换失败的默认值</param>
- /// <returns></returns>
- public static bool TryConvertTo(this IConvertible value, Type type, out object result)
- {
- try
- {
- result = ConvertTo(value, type);
- return true;
- }
- catch
- {
- result = default;
- return false;
- }
- }
- /// <summary>
- /// 类型直转
- /// </summary>
- /// <param name="value"></param>
- /// <param name="type">目标类型</param>
- /// <returns></returns>
- public static object ConvertTo(this IConvertible value, Type type)
- {
- if (null == value)
- {
- return default;
- }
- if (type.IsEnum)
- {
- return Enum.Parse(type, value.ToString(CultureInfo.InvariantCulture));
- }
- if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
- {
- var underlyingType = Nullable.GetUnderlyingType(type);
- return underlyingType!.IsEnum ? Enum.Parse(underlyingType, value.ToString(CultureInfo.CurrentCulture)) : Convert.ChangeType(value, underlyingType);
- }
- return Convert.ChangeType(value, type);
- }
- }
- }
|