修改可空类型
This commit is contained in:
@@ -11,7 +11,7 @@ namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
public class AssemblyHelper
|
||||
{
|
||||
public static List<Type> GetClass(string assemblyFile, string className = null, string spaceName = null)
|
||||
public static List<Type> GetClass(string assemblyFile, string? className = null, string? spaceName = null)
|
||||
{
|
||||
Assembly assembly = Assembly.Load(assemblyFile);
|
||||
return assembly.GetTypes().Where(m => m.IsClass
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -13,13 +14,14 @@ namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
this._getField = getfield;
|
||||
}
|
||||
public bool Equals(T x, T y)
|
||||
public bool Equals(T? x, T? y)
|
||||
{
|
||||
return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));
|
||||
return EqualityComparer<C>.Default.Equals(_getField(x!), _getField(y!));
|
||||
}
|
||||
|
||||
public int GetHashCode(T obj)
|
||||
{
|
||||
return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));
|
||||
return EqualityComparer<C>.Default.GetHashCode(this._getField(obj)!);
|
||||
}
|
||||
}
|
||||
public static class DistinctHelper
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Yi.Framework.Common.Helper
|
||||
}
|
||||
|
||||
|
||||
public static async Task<string> Post(string url, object item = null, Dictionary<string, string> head = null)
|
||||
public static async Task<string> Post(string url, object? item = null, Dictionary<string, string>? head = null)
|
||||
{
|
||||
|
||||
using StringContent json = new(JsonSerializer.Serialize(item), Encoding.UTF8, MediaTypeNames.Application.Json);
|
||||
|
||||
@@ -24,6 +24,11 @@ namespace Yi.Framework.Common.Helper
|
||||
// 获取所有可用网卡IP信息
|
||||
var ipCollection = nics?.Select(x => x.GetIPProperties())?.SelectMany(x => x.UnicastAddresses);
|
||||
|
||||
if (ipCollection is null)
|
||||
{
|
||||
return instanceIp;
|
||||
}
|
||||
|
||||
foreach (var ipadd in ipCollection)
|
||||
{
|
||||
if (!IPAddress.IsLoopback(ipadd.Address) && ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Yi.Framework.Common.Helper
|
||||
|
||||
public static T StrToObj<T>(string str)
|
||||
{
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
|
||||
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str)!;
|
||||
}
|
||||
/// <summary>
|
||||
/// 转换对象为JSON格式数据
|
||||
@@ -85,7 +85,7 @@ namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
|
||||
new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
|
||||
return (T)serializer.ReadObject(ms);
|
||||
return (T)serializer.ReadObject(ms)!;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace Yi.Framework.Common.Helper
|
||||
byte[] bIn = Encoding.Unicode.GetBytes(pass);
|
||||
byte[] bSalt = Convert.FromBase64String(salt);
|
||||
byte[] bAll = new byte[bSalt.Length + bIn.Length];
|
||||
byte[] bRet = null;
|
||||
byte[]? bRet = null;
|
||||
|
||||
Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
|
||||
Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
|
||||
|
||||
@@ -22,15 +22,15 @@ namespace Yi.Framework.Common.Helper
|
||||
|
||||
public static string GetMimeMapping(string FileName)
|
||||
{
|
||||
string text = null;
|
||||
string text = null!;
|
||||
int num = FileName.LastIndexOf('.');
|
||||
if (0 < num && num > FileName.LastIndexOf('\\'))
|
||||
{
|
||||
text = (string)MimeHelper._mimeMappingTable[FileName.Substring(num)];
|
||||
text = (string)MimeHelper._mimeMappingTable[FileName.Substring(num)]!;
|
||||
}
|
||||
if (text == null)
|
||||
{
|
||||
text = (string)MimeHelper._mimeMappingTable[".*"];
|
||||
text = (string)MimeHelper._mimeMappingTable[".*"]!;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ namespace Yi.Framework.Common.Helper
|
||||
/// </summary>
|
||||
public class RSAHelper
|
||||
{
|
||||
public readonly RSA _privateKeyRsaProvider;
|
||||
public readonly RSA _publicKeyRsaProvider;
|
||||
public readonly RSA? _privateKeyRsaProvider;
|
||||
public readonly RSA? _publicKeyRsaProvider;
|
||||
private readonly HashAlgorithmName _hashAlgorithmName;
|
||||
private readonly Encoding _encoding;
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Yi.Framework.Common.Helper
|
||||
/// <param name="encoding">编码类型</param>
|
||||
/// <param name="privateKey">私钥</param>
|
||||
/// <param name="publicKey">公钥</param>
|
||||
public RSAHelper(RSAType rsaType, Encoding encoding, string privateKey, string publicKey = null)
|
||||
public RSAHelper(RSAType rsaType, Encoding encoding, string privateKey, string? publicKey = null)
|
||||
{
|
||||
_encoding = encoding;
|
||||
if (!string.IsNullOrEmpty(privateKey))
|
||||
@@ -50,7 +50,7 @@ namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
byte[] dataBytes = _encoding.GetBytes(data);
|
||||
|
||||
var signatureBytes = _privateKeyRsaProvider.SignData(dataBytes, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
|
||||
var signatureBytes = _privateKeyRsaProvider!.SignData(dataBytes, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
|
||||
|
||||
return Convert.ToBase64String(signatureBytes);
|
||||
}
|
||||
@@ -70,7 +70,7 @@ namespace Yi.Framework.Common.Helper
|
||||
byte[] dataBytes = _encoding.GetBytes(data);
|
||||
byte[] signBytes = Convert.FromBase64String(sign);
|
||||
|
||||
var verify = _publicKeyRsaProvider.VerifyData(dataBytes, signBytes, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
|
||||
var verify = _publicKeyRsaProvider!.VerifyData(dataBytes, signBytes, _hashAlgorithmName, RSASignaturePadding.Pkcs1);
|
||||
|
||||
return verify;
|
||||
}
|
||||
@@ -225,7 +225,7 @@ namespace Yi.Framework.Common.Helper
|
||||
/// </summary>
|
||||
/// <param name="publicKeyString"></param>
|
||||
/// <returns></returns>
|
||||
public RSA CreateRsaProviderFromPublicKey(string publicKeyString)
|
||||
public RSA? CreateRsaProviderFromPublicKey(string publicKeyString)
|
||||
{
|
||||
// encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
|
||||
byte[] seqOid = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace Yi.Framework.Common.Helper
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static TEntity Deserialize<TEntity>(byte[] value)
|
||||
public static TEntity? Deserialize<TEntity>(byte[] value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
string urlPars = null;
|
||||
string? urlPars = null;
|
||||
bool isEnter = false;
|
||||
foreach (var item in dic)
|
||||
{
|
||||
@@ -69,7 +69,7 @@ namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
string urlPars = null;
|
||||
string? urlPars = null;
|
||||
bool isEnter = false;
|
||||
foreach (var item in dic)
|
||||
{
|
||||
|
||||
@@ -9,13 +9,13 @@ namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
public static class TreeHelper
|
||||
{
|
||||
public static List<T> SetTree<T>(List<T> list, Action<T> action = null)
|
||||
public static List<T> SetTree<T>(List<T> list, Action<T> action = null!)
|
||||
{
|
||||
if (list is not null && list.Count > 0)
|
||||
{
|
||||
IList<T> result = new List<T>();
|
||||
long pid = list.Min(m => (m as ITreeModel<T>).ParentId);
|
||||
IList<T> t = list.Where(m => (m as ITreeModel<T>).ParentId == pid).ToList();
|
||||
long pid = list.Min(m => (m as ITreeModel<T>)!.ParentId);
|
||||
IList<T> t = list.Where(m => (m as ITreeModel<T>)!.ParentId == pid).ToList();
|
||||
foreach (T model in t)
|
||||
{
|
||||
if (action is not null)
|
||||
@@ -24,20 +24,20 @@ namespace Yi.Framework.Common.Helper
|
||||
}
|
||||
result.Add(model);
|
||||
var item = (model as ITreeModel<T>);
|
||||
IList<T> children = list.Where(m => (m as ITreeModel<T>).ParentId == item.Id).ToList();
|
||||
IList<T> children = list.Where(m => (m as ITreeModel<T>)!.ParentId == item!.Id).ToList();
|
||||
if (children.Count > 0)
|
||||
{
|
||||
SetTreeChildren(list, children, model, action);
|
||||
SetTreeChildren(list, children, model, action!);
|
||||
}
|
||||
}
|
||||
return result.OrderByDescending(m => (m as ITreeModel<T>).OrderNum).ToList();
|
||||
return result.OrderByDescending(m => (m as ITreeModel<T>)!.OrderNum).ToList();
|
||||
}
|
||||
return null;
|
||||
return null!;
|
||||
}
|
||||
private static void SetTreeChildren<T>(IList<T> list, IList<T> children, T model, Action<T> action = null)
|
||||
private static void SetTreeChildren<T>(IList<T> list, IList<T> children, T model, Action<T> action = null!)
|
||||
{
|
||||
var mm = (model as ITreeModel<T>);
|
||||
mm.Children = new List<T>();
|
||||
mm!.Children = new List<T>();
|
||||
foreach (T item in children)
|
||||
{
|
||||
if (action is not null)
|
||||
@@ -46,13 +46,13 @@ namespace Yi.Framework.Common.Helper
|
||||
}
|
||||
mm.Children.Add(item);
|
||||
var _item = (item as ITreeModel<T>);
|
||||
IList<T> _children = list.Where(m => (m as ITreeModel<T>).ParentId == _item.Id).ToList();
|
||||
IList<T> _children = list.Where(m => (m as ITreeModel<T>)!.ParentId == _item!.Id).ToList();
|
||||
if (_children.Count > 0)
|
||||
{
|
||||
SetTreeChildren(list, _children, item, action);
|
||||
SetTreeChildren(list, _children, item, action!);
|
||||
}
|
||||
}
|
||||
mm.Children = mm.Children.OrderByDescending(m => (m as ITreeModel<T>).OrderNum).ToList();
|
||||
mm.Children = mm.Children.OrderByDescending(m => (m as ITreeModel<T>)!.OrderNum).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Yi.Framework.Common.Helper
|
||||
/// <typeparam name="T">类</typeparam>
|
||||
/// <param name="obj">对象</param>
|
||||
/// <returns>字符格式的JSON数据</returns>
|
||||
public static string GetXML<T>(object obj)
|
||||
public static string? GetXML<T>(object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -42,7 +42,7 @@ namespace Yi.Framework.Common.Helper
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(T), new XmlRootAttribute(rootName));
|
||||
StringReader reader = new StringReader(xml);
|
||||
|
||||
T res = (T)serializer.Deserialize(reader);
|
||||
T res = (T)serializer.Deserialize(reader)!;
|
||||
reader.Close();
|
||||
reader.Dispose();
|
||||
return res;
|
||||
|
||||
Reference in New Issue
Block a user