瀏覽代碼

值类型的类型转换增强

懒得勤快 5 年之前
父節點
當前提交
4b604c5c80

+ 91 - 3
Masuit.Tools.Abstractions/Extensions/BaseType/IConvertibleExtensions.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Globalization;
 
 namespace Masuit.Tools
 {
@@ -8,18 +9,105 @@ namespace Masuit.Tools
         /// 类型直转
         /// </summary>
         /// <typeparam name="T"></typeparam>
-        /// <param name="value"></param>
+        /// <param name="convertibleValue"></param>
         /// <returns></returns>
-        public static T To<T>(this IConvertible value)
+
+        public static T ConvertTo<T>(this IConvertible convertibleValue) where T : IConvertible
+        {
+            return (T)ConvertTo(convertibleValue, typeof(T));
+        }
+
+        /// <summary>
+        /// 类型直转
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="convertibleValue"></param>
+        /// <param name="defaultValue">转换失败的默认值</param>
+        /// <returns></returns>
+        public static T TryConvertTo<T>(this IConvertible convertibleValue, T defaultValue = default) where T : IConvertible
+        {
+            try
+            {
+                return (T)ConvertTo(convertibleValue, typeof(T));
+            }
+            catch
+            {
+                return defaultValue;
+            }
+        }
+
+        /// <summary>
+        /// 类型直转
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="convertibleValue"></param>
+        /// <param name="value">转换失败的默认值</param>
+        /// <returns></returns>
+        public static bool TryConvertTo<T>(this IConvertible convertibleValue, out T value) where T : IConvertible
         {
             try
             {
-                return (T)Convert.ChangeType(value, typeof(T));
+                value = (T)ConvertTo(convertibleValue, typeof(T));
+                return true;
             }
             catch
+            {
+                value = default;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 类型直转
+        /// </summary>
+        /// <param name="convertibleValue"></param>
+        /// <param name="type">目标类型</param>
+        /// <param name="value">转换失败的默认值</param>
+        /// <returns></returns>
+        public static bool TryConvertTo(this IConvertible convertibleValue, Type type, out object value)
+        {
+            try
+            {
+                value = ConvertTo(convertibleValue, type);
+                return true;
+            }
+            catch
+            {
+                value = default;
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 类型直转
+        /// </summary>
+        /// <param name="convertibleValue"></param>
+        /// <param name="type">目标类型</param>
+        /// <returns></returns>
+        public static object ConvertTo(this IConvertible convertibleValue, Type type)
+        {
+            if (null == convertibleValue)
             {
                 return default;
             }
+
+            if (type.IsEnum)
+            {
+                return Enum.Parse(type, convertibleValue.ToString(CultureInfo.InvariantCulture));
+            }
+
+            if (!type.IsGenericType)
+            {
+                return Convert.ChangeType(convertibleValue, type);
+            }
+
+            if (type.GetGenericTypeDefinition() == typeof(Nullable<>))
+            {
+                var underlyingType = Nullable.GetUnderlyingType(type);
+                return underlyingType!.IsEnum ? Enum.Parse(underlyingType, convertibleValue.ToString(CultureInfo.CurrentCulture)) : Convert.ChangeType(convertibleValue, underlyingType);
+            }
+
+            throw new InvalidCastException($"不能将类型 \"{convertibleValue.GetType().FullName}\" 转换为 \"{type.FullName}\"");
         }
     }
 }

+ 3 - 3
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -7,7 +7,7 @@
     <LangVersion>8.0</LangVersion>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <CodeAnalysisRuleSet />
-    <Version>2.3.2.2</Version>
+    <Version>2.3.2.3</Version>
     <Authors>懒得勤快</Authors>
     <Description>Masuit.Tools基础公共库</Description>
     <Copyright>懒得勤快,长空X</Copyright>
@@ -20,9 +20,9 @@
     <RepositoryType>Github</RepositoryType>
     <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
     <PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
-    <FileVersion>2.3.2.2</FileVersion>
+    <FileVersion>2.3.2.3</FileVersion>
     <Company>masuit.com</Company>
-    <AssemblyVersion>2.3.2.2</AssemblyVersion>
+    <AssemblyVersion>2.3.2.3</AssemblyVersion>
     <PackageLicenseUrl>https://github.com/ldqk/Masuit.Tools/blob/master/LICENSE</PackageLicenseUrl>
   </PropertyGroup>
 

+ 3 - 3
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -20,10 +20,10 @@
     <UserSecretsId>830c282f-f7c1-42be-8651-4cd06ac8e73f</UserSecretsId>
     <RepositoryType>Github</RepositoryType>
     <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-    <Version>2.3.2.2</Version>
-    <FileVersion>2.3.2.2</FileVersion>
+    <Version>2.3.2.3</Version>
+    <FileVersion>2.3.2.3</FileVersion>
     <Company>masuit.com</Company>
-    <AssemblyVersion>2.3.2.2</AssemblyVersion>
+    <AssemblyVersion>2.3.2.3</AssemblyVersion>
     <Authors>懒得勤快X</Authors>
     <RepositoryUrl>https://github.com/ldqk/Masuit.Tools</RepositoryUrl>
   </PropertyGroup>

二進制
Masuit.Tools/Properties/AssemblyInfo.cs


+ 1 - 1
Masuit.Tools/package.nuspec

@@ -4,7 +4,7 @@
     <!--*-->
     <id>Masuit.Tools.Net</id>
     <!--*-->
-    <version>2.3.2.2</version>
+    <version>2.3.2.3</version>
     <title>Masuit.Tools</title>
     <!--*-->
     <authors>masuit.com</authors>