Browse Source

增加一些扩展

懒得勤快 3 years ago
parent
commit
34fc9101fa

+ 14 - 0
Masuit.Tools.Abstractions/Extensions/BaseType/IEnumerableExtensions.cs

@@ -164,6 +164,20 @@ namespace Masuit.Tools
             }
         }
 
+        /// <summary>
+        /// 添加多个元素
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="this"></param>
+        /// <param name="values"></param>
+        public static void AddRange<T>(this ICollection<T> @this, IEnumerable<T> values)
+        {
+            foreach (var obj in values)
+            {
+                @this.Add(obj);
+            }
+        }
+
         /// <summary>
         /// 添加多个元素
         /// </summary>

+ 2 - 0
Masuit.Tools.Abstractions/Extensions/BaseType/StringExtensions.cs

@@ -20,6 +20,8 @@ namespace Masuit.Tools
     {
         public static string Join(this IEnumerable<string> strs, string separate = ", ") => string.Join(separate, strs);
 
+        public static string Join<T>(this IEnumerable<T> strs, string separate = ", ") => string.Join(separate, strs);
+
         /// <summary>
         /// 字符串转时间
         /// </summary>

+ 2 - 2
Masuit.Tools.Abstractions/Files/FileExt.cs

@@ -33,7 +33,7 @@ namespace Masuit.Tools.Files
         /// <param name="fs">源</param>
         /// <param name="dest">目标地址</param>
         /// <param name="bufferSize">缓冲区大小,默认8MB</param>
-        public static async void CopyToFileAsync(this Stream fs, string dest, int bufferSize = 1024 * 1024 * 8)
+        public static async Task CopyToFileAsync(this Stream fs, string dest, int bufferSize = 1024 * 1024 * 8)
         {
             using var fsWrite = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite);
             byte[] buf = new byte[bufferSize];
@@ -97,4 +97,4 @@ namespace Masuit.Tools.Files
             return sb.ToString();
         }
     }
-}
+}

+ 2 - 2
Masuit.Tools.AspNetCore/ModelBinder/BodyOrDefaultModelBinder.cs

@@ -28,7 +28,7 @@ public class BodyOrDefaultModelBinder : IModelBinder
         var text = Encoding.UTF8.GetString(buffer);
         request.Body.Position = 0;
 
-        if (bindingContext.ModelType.IsPrimitive || bindingContext.ModelType == typeof(string) || bindingContext.ModelType.IsEnum || bindingContext.ModelType == typeof(DateTime) || bindingContext.ModelType == typeof(Guid))
+        if (bindingContext.ModelType.IsPrimitive || bindingContext.ModelType == typeof(string) || bindingContext.ModelType.IsEnum || bindingContext.ModelType == typeof(DateTime) || bindingContext.ModelType == typeof(Guid) || (bindingContext.ModelType.IsGenericType && bindingContext.ModelType.GetGenericTypeDefinition() == typeof(Nullable<>)))
         {
             var parameter = bindingContext.ModelMetadata.ParameterName;
             var value = "";
@@ -36,7 +36,7 @@ public class BodyOrDefaultModelBinder : IModelBinder
             {
                 value = request.Query[parameter] + "";
             }
-            else if (request.ContentType.StartsWith("application/json"))
+            else if (request.ContentType is not null && request.ContentType.StartsWith("application/json"))
             {
                 try
                 {