lith 3 年 前
コミット
13facee80b

+ 19 - 4
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/EnumExtensions.cs

@@ -12,7 +12,24 @@ namespace Vit.Extensions
         #region String --> Enum
 
         /// <summary>
-        /// T 必须为Enum,且不可为Nullable
+        /// enumType must be Enum, can be Nullable
+        /// </summary>
+        /// <param name="data"></param>
+        /// <param name="enumType"></param>
+        /// <returns></returns>
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static object StringToEnum(this string data,Type enumType)
+        {
+            try
+            {
+                return Enum.Parse(enumType.GetUnderlyingTypeIfNullable(), data);
+            }
+            catch { }
+            return default;
+        }
+
+        /// <summary>
+        /// T must be Enum, can be Nullable
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="data"></param>
@@ -22,15 +39,13 @@ namespace Vit.Extensions
         {
             try
             {
-                return (T)Enum.Parse(typeof(T), data);
+                return (T)Enum.Parse(typeof(T).GetUnderlyingTypeIfNullable(), data);
             }
             catch { }
             return default(T);
         }
 
 
-
-
         //public static string EnumToString(this Enum data)
         //{
         //    return data.ToString();

+ 3 - 13
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/ObjectExtensions.cs

@@ -18,7 +18,7 @@ namespace Vit.Extensions
             if (data == null)
             {
                 return false;
-                //throw new ArgumentNullException(nameof(type));
+                //throw new ArgumentNullException(nameof(data));
             }
             return data.GetType().TypeIsValueTypeOrStringType();
         }
@@ -28,7 +28,7 @@ namespace Vit.Extensions
         #region Convert
 
         /// <summary>
-        /// 若Type为Nullable类型(例如 long?)则转换为对应的值类型(例如long),否则直接转换。
+        /// 若Type为Nullable类型(例如 long?)则转换为对应的值类型(例如long),否则直接转换。(亦可为Enum类型)
         /// 若转换失败,会返回default(T)
         /// </summary>
         /// <typeparam name="T"></typeparam>
@@ -42,22 +42,12 @@ namespace Vit.Extensions
                 return default(T);
                 //throw new ArgumentNullException(nameof(value));
             }
-            //try
-            //{
             return (T)System.Convert.ChangeType(value, typeof(T).GetUnderlyingTypeIfNullable());
-            //}
-            //catch (System.Exception)
-            //{
-
-            //    throw;
-            //    return default(T);
-            //}
-
         }
 
 
         /// <summary>
-        /// 若为Nullable类型(例如 long?)则转换为对应的值类型(例如long),否则直接转换。
+        /// 若为Nullable类型(例如 long?)则转换为对应的值类型(例如long),否则直接转换。(亦可为Enum类型)
         /// </summary>
         /// <param name="type"></param>
         /// <param name="value"></param>

+ 4 - 6
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/TypeExtensions.cs

@@ -1,7 +1,5 @@
 using System;
-using System.Collections.Generic;
 using System.Runtime.CompilerServices;
-using System.Text;
 
 namespace Vit.Extensions
 {
@@ -45,16 +43,16 @@ namespace Vit.Extensions
 
 
 
-        #region IfNullable
+        #region IsNullable
         /// <summary>
         /// 是否为Nullable类型(例如 long?)
         /// </summary>
         /// <param name="type"></param>
         /// <returns></returns>
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        public static bool IfNullable(this Type type)
+        public static bool IsNullable(this Type type)
         {
-            return true== type?.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
+            return true == type?.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
         }
         #endregion
 
@@ -68,7 +66,7 @@ namespace Vit.Extensions
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static global::System.Type GetUnderlyingTypeIfNullable(this global::System.Type type)
         {
-            return type.IfNullable() ? type.GetGenericArguments()[0] : type;
+            return type.IsNullable() ? type.GetGenericArguments()[0] : type;
 
 
             //if (type == null)

+ 7 - 4
dotnet/Library/Vit/Vit.Core/Vit.Core/Module/Serialization/Serialization_Newtonsoft.cs

@@ -200,10 +200,12 @@ namespace Vit.Core.Module.Serialization
 
             Type type = typeof(T);
 
+            if (type.GetUnderlyingTypeIfNullable().IsEnum)
+                return value.StringToEnum<T>();
+
             if (type.TypeIsValueTypeOrStringType())
-            {
                 return (T)DeserializeStruct(value, type);
-            }
+
 
             //if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();
 
@@ -222,10 +224,11 @@ namespace Vit.Core.Module.Serialization
         {
             if (null == value || null == type) return null;
 
+            if (type.GetUnderlyingTypeIfNullable().IsEnum)
+                return value.StringToEnum(type);
+
             if (type.TypeIsValueTypeOrStringType())
-            {
                 return DeserializeStruct(value, type);
-            }
 
             //if (string.IsNullOrWhiteSpace(value)) return type.DefaultValue();