消除部分warning

This commit is contained in:
2023-05-16 15:21:04 +08:00
parent e7e2cea7bd
commit 67d599dce6
17 changed files with 396 additions and 54 deletions

View File

@@ -0,0 +1,350 @@
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace Tnb.Common;
[DebuggerStepThrough]
public static class Check
{
public static T NotNull<T>(T value, string parameterName)
{
if (value == null)
{
throw new ArgumentNullException(parameterName);
}
return value;
}
public static T NotNull<T>(
T value,
string parameterName,
string message)
{
if (value == null)
{
throw new ArgumentNullException(parameterName, message);
}
return value;
}
public static string NotNull(
string value,
string parameterName,
int maxLength = int.MaxValue,
int minLength = 0)
{
if (value == null)
{
throw new ArgumentException($"{parameterName} can not be null!", parameterName);
}
if (value.Length > maxLength)
{
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName);
}
if (minLength > 0 && value.Length < minLength)
{
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName);
}
return value;
}
public static string NotNullOrWhiteSpace(
string value,
string parameterName,
int maxLength = int.MaxValue,
int minLength = 0)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException($"{parameterName} can not be null, empty or white space!", parameterName);
}
if (value.Length > maxLength)
{
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName);
}
if (minLength > 0 && value.Length < minLength)
{
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName);
}
return value;
}
public static string NotNullOrEmpty(
string value,
string parameterName,
int maxLength = int.MaxValue,
int minLength = 0)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException($"{parameterName} can not be null or empty!", parameterName);
}
if (value.Length > maxLength)
{
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName);
}
if (minLength > 0 && value.Length < minLength)
{
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName);
}
return value;
}
public static ICollection<T> NotNullOrEmpty<T>(ICollection<T> value, string parameterName)
{
if (value == null || value.Count <= 0)
{
throw new ArgumentException(parameterName + " can not be null or empty!", parameterName);
}
return value;
}
public static Type AssignableTo<TBaseType>(
Type type,
string parameterName)
{
NotNull(type, parameterName);
if (!type.IsAssignableTo(typeof(TBaseType)))
{
throw new ArgumentException($"{parameterName} (type of {type.AssemblyQualifiedName}) should be assignable to the {typeof(TBaseType).FullName}!");
}
return type;
}
public static string Length(
[AllowNull] string value,
string parameterName,
int maxLength,
int minLength = 0)
{
if (minLength > 0)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(parameterName + " can not be null or empty!", parameterName);
}
if (value.Length < minLength)
{
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName);
}
}
if (value != null && value.Length > maxLength)
{
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName);
}
return value!;
}
public static short Positive(
short value,
string parameterName)
{
if(value == 0)
{
throw new ArgumentException($"{parameterName} is equal to zero");
}
else if(value < 0)
{
throw new ArgumentException($"{parameterName} is less than zero");
}
return value;
}
public static Int32 Positive(
Int32 value,
string parameterName)
{
if (value == 0)
{
throw new ArgumentException($"{parameterName} is equal to zero");
}
else if (value < 0)
{
throw new ArgumentException($"{parameterName} is less than zero");
}
return value;
}
public static Int64 Positive(
Int64 value,
string parameterName)
{
if (value == 0)
{
throw new ArgumentException($"{parameterName} is equal to zero");
}
else if (value < 0)
{
throw new ArgumentException($"{parameterName} is less than zero");
}
return value;
}
public static float Positive(
float value,
string parameterName)
{
if (value == 0)
{
throw new ArgumentException($"{parameterName} is equal to zero");
}
else if (value < 0)
{
throw new ArgumentException($"{parameterName} is less than zero");
}
return value;
}
public static double Positive(
double value,
string parameterName)
{
if (value == 0)
{
throw new ArgumentException($"{parameterName} is equal to zero");
}
else if (value < 0)
{
throw new ArgumentException($"{parameterName} is less than zero");
}
return value;
}
public static decimal Positive(
decimal value,
string parameterName)
{
if (value == 0)
{
throw new ArgumentException($"{parameterName} is equal to zero");
}
else if (value < 0)
{
throw new ArgumentException($"{parameterName} is less than zero");
}
return value;
}
public static Int16 Range(
Int16 value,
string parameterName,
Int16 minimumValue,
Int16 maximumValue = Int16.MaxValue)
{
if(value < minimumValue || value > maximumValue)
{
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
}
return value;
}
public static Int32 Range(
Int32 value,
string parameterName,
Int32 minimumValue,
Int32 maximumValue = Int32.MaxValue)
{
if (value < minimumValue || value > maximumValue)
{
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
}
return value;
}
public static Int64 Range(
Int64 value,
string parameterName,
Int64 minimumValue,
Int64 maximumValue = Int64.MaxValue)
{
if (value < minimumValue || value > maximumValue)
{
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
}
return value;
}
public static float Range(
float value,
string parameterName,
float minimumValue,
float maximumValue = float.MaxValue)
{
if (value < minimumValue || value > maximumValue)
{
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
}
return value;
}
public static double Range(
double value,
string parameterName,
double minimumValue,
double maximumValue = double.MaxValue)
{
if (value < minimumValue || value > maximumValue)
{
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
}
return value;
}
public static decimal Range(
decimal value,
string parameterName,
decimal minimumValue,
decimal maximumValue = decimal.MaxValue)
{
if (value < minimumValue || value > maximumValue)
{
throw new ArgumentException($"{parameterName} is out of range min: {minimumValue} - max: {maximumValue}");
}
return value;
}
public static T NotDefaultOrNull<T>(
T? value,
string parameterName)
where T : struct
{
if (value == null)
{
throw new ArgumentException($"{parameterName} is null!", parameterName);
}
if (value.Value.Equals(default(T)))
{
throw new ArgumentException($"{parameterName} has a default value!", parameterName);
}
return value.Value;
}
}

View File

@@ -31,7 +31,7 @@ public static class BooleanExtensions
/// </summary>
private static bool? GetBool(this object data)
{
switch (data.ToString().Trim().ToLower())
switch (data.ToString()?.Trim().ToLower())
{
case "0":
return false;

View File

@@ -16,7 +16,7 @@ public static class DictionaryExtensions
/// <param name="dictionary">要操作的字典.</param>
/// <param name="key">指定键名.</param>
/// <returns>获取到的值.</returns>
public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
public static TValue? GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
return dictionary.TryGetValue(key, out TValue value) ? value : default(TValue);
}

View File

@@ -152,7 +152,7 @@ public static class EnumExtensions
/// <returns></returns>
public static string GetDescription(this System.Enum value)
{
return value.GetType().GetMember(value.ToString()).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>()?.Description;
return value.GetType().GetMember(value.ToString()).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>()?.Description ?? string.Empty;
}
/// <summary>
@@ -162,7 +162,7 @@ public static class EnumExtensions
/// <returns></returns>
public static string GetDescription(this object value)
{
return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>()?.Description;
return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>()?.Description ?? string.Empty;
}
/// <summary>

View File

@@ -219,7 +219,7 @@ public static partial class Extensions
/// </summary>
private static bool? GetBool(this object data)
{
switch (data.ToString().Trim().ToLower())
switch (data.ToString()?.Trim().ToLower())
{
case "0":
return false;
@@ -466,7 +466,7 @@ public static partial class Extensions
{
try
{
return obj == null ? string.Empty : obj.ToString();
return obj == null ? string.Empty : obj.ToString()!;
}
catch
{
@@ -489,7 +489,7 @@ public static partial class Extensions
return string.Join(",", list);
}
return obj.ToString();
return obj.ToString()!;
}
catch
{
@@ -678,7 +678,7 @@ public static partial class Extensions
/// <returns></returns>
public static string ObjToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
if (thisValue != null) return thisValue.ToString()!.Trim();
return string.Empty;
}

View File

@@ -38,7 +38,7 @@ public static class RandomExtensions
Array array = System.Enum.GetValues(type);
int index = random.Next(array.GetLowerBound(0), array.GetUpperBound(0) + 1);
return (T)array.GetValue(index);
return (T)array.GetValue(index)!;
}
/// <summary>
@@ -67,7 +67,7 @@ public static class RandomExtensions
public static T NextItem<T>(this Random random, T[] items)
{
if (items == null || items.Length == 0)
return default(T);
return default(T)!;
return items[random.Next(items.Length)];
}

View File

@@ -111,10 +111,10 @@ public class UserAgent : IUserAgent
private DeviceInfo _device;
private OSInfo _os;
private bool? _isBot;
//private bool? _isBot;
private bool? _isMobileDevice;
private bool? _isTablet;
private bool? _isPdfConverter;
//private bool? _isPdfConverter;
static UserAgent()
{
@@ -142,7 +142,7 @@ public class UserAgent : IUserAgent
}
}
return _rawValue;
return _rawValue ?? "";
}
set
@@ -151,10 +151,10 @@ public class UserAgent : IUserAgent
_userAgent = null;
_device = null;
_os = null;
_isBot = null;
//_isBot = null;
_isMobileDevice = null;
_isTablet = null;
_isPdfConverter = null;
//_isPdfConverter = null;
}
}

View File

@@ -190,7 +190,7 @@ public static class CodeGenHelper
field = entityInfo.Columns.Find(it => it.PropertyName.Equals(sort.ToUpperCase()))?.DbColumnName;
break;
}
return string.IsNullOrEmpty(field) ? null : field;
return string.IsNullOrEmpty(field) ? "" : field;
}
/// <summary>

View File

@@ -63,7 +63,7 @@ public static class ComparisonHelper<T>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(T x, T y)
public int Compare(T? x, T? y)
{
return _comparer.Compare(_keySelector(x), _keySelector(y));
}

View File

@@ -1,4 +1,5 @@
using JNPF.DependencyInjection;
using System.Diagnostics.CodeAnalysis;
using JNPF.DependencyInjection;
namespace JNPF.Common.Security;
@@ -49,14 +50,14 @@ public static class EqualityHelper<T>
: this(keySelector, EqualityComparer<TV>.Default)
{ }
public bool Equals(T x, T y)
public bool Equals(T? x, T? y)
{
return _comparer.Equals(_keySelector(x), _keySelector(y));
}
public int GetHashCode(T obj)
public int GetHashCode([DisallowNull] T obj)
{
return _comparer.GetHashCode(_keySelector(obj));
return _comparer.GetHashCode(_keySelector(obj)!);
}
}
}

View File

@@ -11,6 +11,7 @@ using NPOI.XSSF.UserModel;
namespace JNPF.Common.Security;
#pragma warning disable CS8602, CS0618, CA2200
/// <summary>
/// Excel导出操作类
/// 版 本V3.2.0
@@ -740,7 +741,7 @@ public class ExcelExportHelper<T>
}
catch (Exception ex)
{
throw;
throw ex;
}
}
@@ -1039,7 +1040,7 @@ public class ExcelExportHelper<T>
}
catch (Exception ex)
{
throw;
throw ex;
}
}
@@ -1168,4 +1169,5 @@ public class ExcelExportHelper<T>
}
#endregion
}
}
#pragma warning restore CS8602, CS0618, CA2200

View File

@@ -108,7 +108,7 @@ namespace JNPF.Common.Helper
//}
//else
//{
dataRow[j] = cell.ToString().Trim();
dataRow[j] = cell.ToString()?.Trim();
//}
}
}

View File

@@ -505,6 +505,7 @@ public class FileHelper
#endregion
#pragma warning disable CA1416 // 验证平台兼容性
#region
/// <summary>
@@ -669,6 +670,7 @@ public class FileHelper
}
#endregion
#pragma warning restore CA1416 // 验证平台兼容性
#region
@@ -712,7 +714,8 @@ public class FileHelper
if (selectFiles.Count > 0)
{
return selectFiles.FirstOrDefault().FullName;
//modified by PhilPan
return selectFiles.First().FullName;
}
return Path.Combine(folderPath, $@"{prefix}_{DateTime.Now.ParseToUnixTime()}.log");
@@ -737,7 +740,7 @@ public class FileHelper
Microsoft.AspNetCore.Http.HttpContext? httpContext = App.HttpContext;
httpContext.Response.ContentType = "application/octet-stream";
httpContext.Response.Headers.Add("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
httpContext.Response.Headers.Add("Content-Length", buff.Length.ToString());
httpContext.Response.Headers.Add("Content-Length", buff?.Length.ToString());
httpContext.Response.Body.WriteAsync(buff);
httpContext.Response.Body.Flush();
httpContext.Response.Body.Close();

View File

@@ -46,7 +46,7 @@ public static class JsonHelper
/// <returns></returns>
public static T ToObject<T>(this string json)
{
return _ = _jsonSerializer.Deserialize<T>(json) ?? default(T);
return _jsonSerializer.Deserialize<T>(json);
}
/// <summary>
@@ -58,7 +58,7 @@ public static class JsonHelper
/// <returns></returns>
public static T ToObject<T>(this string json, object jsonSerializerOptions = default)
{
return _ = _jsonSerializer.Deserialize<T>(json, jsonSerializerOptions) ?? default(T);
return _jsonSerializer.Deserialize<T>(json, jsonSerializerOptions);
}
/// <summary>
@@ -69,7 +69,7 @@ public static class JsonHelper
/// <returns></returns>
public static T ToObject<T>(this object json)
{
return _ = ToJsonString(json).ToObject<T>() ?? default(T);
return ToJsonString(json).ToObject<T>();
}
/// <summary>
@@ -81,7 +81,7 @@ public static class JsonHelper
/// <returns></returns>
public static T ToObject<T>(this object json, object jsonSerializerOptions = default)
{
return _ = ToJsonString(json, jsonSerializerOptions).ToObject<T>(jsonSerializerOptions) ?? default(T);
return ToJsonString(json, jsonSerializerOptions).ToObject<T>(jsonSerializerOptions);
}
/// <summary>
@@ -92,7 +92,7 @@ public static class JsonHelper
/// <returns></returns>
public static List<T> ToList<T>(this string json)
{
return _ = _jsonSerializer.Deserialize<List<T>>(json) ?? null;
return _jsonSerializer.Deserialize<List<T>>(json);
}
/// <summary>
@@ -104,7 +104,7 @@ public static class JsonHelper
/// <returns></returns>
public static List<T> ToList<T>(this string json, object jsonSerializerOptions = default)
{
return _ = _jsonSerializer.Deserialize<List<T>>(json, jsonSerializerOptions) ?? null;
return _jsonSerializer.Deserialize<List<T>>(json, jsonSerializerOptions);
}
/// <summary>

View File

@@ -217,6 +217,7 @@ public static class MachineHelper
}
#endregion
#pragma warning disable CA1416 // 验证平台兼容性
#region Windows
/// <summary>
@@ -364,4 +365,6 @@ public static class MachineHelper
}
#endregion
#pragma warning restore CA1416 // 验证平台兼容性
}

View File

@@ -60,7 +60,7 @@ public class SuperQueryHelper
{
case JnpfKeyConst.COMINPUT:
case JnpfKeyConst.TEXTAREA:
item.fieldValue = item.fieldValue?.ToString().Replace("\r\n", string.Empty);
item.fieldValue = item.fieldValue?.ToString()?.Replace("\r\n", string.Empty);
switch (item.symbol)
{
case "==": // 等于
@@ -549,8 +549,9 @@ public class SuperQueryHelper
queryOr.mainWhere = true;
queryList.Add(queryOr);
}
//modified by PhilPan
continue;
}
continue;
switch (item.symbol)
{
case ">=": // 大于等于