添加项目文件。
This commit is contained in:
75
SamplePre.Common/ConcurrentQueueExt.cs
Normal file
75
SamplePre.Common/ConcurrentQueueExt.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SamplePre.Common
|
||||
{
|
||||
public class ConcurrentQueueExt<T>
|
||||
{
|
||||
|
||||
private readonly ConcurrentQueue<T> queue = new ConcurrentQueue<T>();
|
||||
|
||||
public void Enqueue(T msgData)
|
||||
{
|
||||
if (msgData != null)
|
||||
{
|
||||
queue.Enqueue(msgData);
|
||||
}
|
||||
}
|
||||
|
||||
//public void Enqueue(List<T> msgDatas)
|
||||
//{
|
||||
// if (msgDatas.IsNullOrEmpty()) return;
|
||||
// foreach (var msgData in msgDatas)
|
||||
// {
|
||||
// queue.Enqueue(msgData);
|
||||
// }
|
||||
//}
|
||||
|
||||
public T GetOne()
|
||||
{
|
||||
if (queue.TryDequeue(out T result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public List<T> GetAll()
|
||||
{
|
||||
List<T> result = new List<T>();
|
||||
int count = queue.Count;
|
||||
if (count > 0)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (queue.TryDequeue(out T t))
|
||||
{
|
||||
result.Add(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
while (!queue.IsEmpty)
|
||||
{
|
||||
queue.TryDequeue(out _);
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return queue.Count;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
91
SamplePre.Common/Helper/ObjectCopyHelper.cs
Normal file
91
SamplePre.Common/Helper/ObjectCopyHelper.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SamplePre.Common.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// 实体对象复制工具类(反射实现)
|
||||
/// 适配.NET Framework 4.8,支持同实体/不同实体间同名字段值复制(浅拷贝)
|
||||
/// </summary>
|
||||
public static class ObjectCopyHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 核心方法:将源对象的同名字段值复制到目标对象
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource">源对象类型</typeparam>
|
||||
/// <typeparam name="TTarget">目标对象类型</typeparam>
|
||||
/// <param name="source">源对象(提供值)</param>
|
||||
/// <param name="target">目标对象(接收值,需提前实例化)</param>
|
||||
/// <param name="ignoreCase">是否忽略属性名大小写(默认true:Name和name视为同一个)</param>
|
||||
/// <param name="ignoreNull">是否忽略源对象的null值(默认false:源null会覆盖目标值)</param>
|
||||
public static void CopyProperties<TSource, TTarget>(this TSource source, TTarget target,
|
||||
bool ignoreCase = true, bool ignoreNull = false)
|
||||
where TSource : class // 源对象不能为空引用类型
|
||||
where TTarget : class // 目标对象不能为空引用类型
|
||||
{
|
||||
// 空值校验
|
||||
if (source == null) throw new ArgumentNullException(nameof(source), "源对象不能为null");
|
||||
if (target == null) throw new ArgumentNullException(nameof(target), "目标对象不能为null");
|
||||
|
||||
// 获取源对象和目标对象的属性信息(仅获取公共可读写属性)
|
||||
PropertyInfo[] sourceProps = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
PropertyInfo[] targetProps = typeof(TTarget).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
// 遍历源对象的所有属性,匹配目标对象的同名字段
|
||||
foreach (PropertyInfo sourceProp in sourceProps)
|
||||
{
|
||||
// 跳过源对象中不可读的属性(无getter)
|
||||
if (!sourceProp.CanRead) continue;
|
||||
|
||||
// 获取源对象当前属性的值
|
||||
object sourceValue = sourceProp.GetValue(source, null);
|
||||
|
||||
// 忽略源对象的null值(若开启该配置)
|
||||
if (ignoreNull && sourceValue == null) continue;
|
||||
|
||||
// 在目标对象中查找同名字段(支持忽略大小写)
|
||||
PropertyInfo targetProp = Array.Find(targetProps, p =>
|
||||
string.Equals(p.Name, sourceProp.Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));
|
||||
|
||||
// 目标对象无该属性/目标属性不可写/类型不匹配 → 跳过
|
||||
if (targetProp == null || !targetProp.CanWrite
|
||||
|| !sourceProp.PropertyType.IsAssignableFrom(targetProp.PropertyType))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 给目标对象的属性赋值
|
||||
targetProp.SetValue(target, sourceValue, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重载方法:快速复制(默认忽略大小写、不忽略null)
|
||||
/// </summary>
|
||||
public static void CopyProperties<TSource, TTarget>(this TSource source, TTarget target)
|
||||
where TSource : class
|
||||
where TTarget : class
|
||||
{
|
||||
CopyProperties(source, target, true, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 便捷方法:创建目标对象并完成复制(一步到位,无需提前实例化目标对象)
|
||||
/// </summary>
|
||||
public static TTarget CopyToNew<TSource, TTarget>(this TSource source,
|
||||
bool ignoreCase = true, bool ignoreNull = false)
|
||||
where TSource : class
|
||||
where TTarget : class, new() // 目标对象必须有无参构造函数
|
||||
{
|
||||
if (source == null) throw new ArgumentNullException(nameof(source));
|
||||
// 实例化目标对象并赋值
|
||||
TTarget target = new TTarget();
|
||||
source.CopyProperties(target, ignoreCase, ignoreNull);
|
||||
return target;
|
||||
}
|
||||
}
|
||||
}
|
||||
46
SamplePre.Common/Helper/ReflectionCommHelper.cs
Normal file
46
SamplePre.Common/Helper/ReflectionCommHelper.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SamplePre.Common
|
||||
{
|
||||
public class ReflectionCommHelper
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 根据属性名获取对象的属性值(支持公共/非公共属性)
|
||||
/// </summary>
|
||||
/// <param name="obj">目标对象(不能为null)</param>
|
||||
/// <param name="propertyName">属性名(大小写敏感)</param>
|
||||
/// <param name="ignoreCase">是否忽略大小写(默认false)</param>
|
||||
/// <returns>属性值(不存在返回null)</returns>
|
||||
public static object GetPropertyValue(object obj, string propertyName, bool ignoreCase = false)
|
||||
{
|
||||
// 空值校验
|
||||
if (obj == null)
|
||||
throw new ArgumentNullException(nameof(obj), "目标对象不能为null");
|
||||
if (string.IsNullOrEmpty(propertyName))
|
||||
throw new ArgumentNullException(nameof(propertyName), "属性名不能为空");
|
||||
|
||||
// 获取对象类型
|
||||
Type type = obj.GetType();
|
||||
// 获取属性信息(BindingFlags控制是否查找非公共/实例成员)
|
||||
PropertyInfo propInfo = type.GetProperty(
|
||||
propertyName,
|
||||
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
|
||||
(ignoreCase ? BindingFlags.IgnoreCase : BindingFlags.Default)
|
||||
);
|
||||
|
||||
// 属性不存在返回null
|
||||
if (propInfo == null)
|
||||
return null;
|
||||
|
||||
// 获取属性值
|
||||
return propInfo.GetValue(obj);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
74
SamplePre.Common/Helper/XmlSerializerHelper.cs
Normal file
74
SamplePre.Common/Helper/XmlSerializerHelper.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace SamplePre.Common.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// XML序列化/反序列化工具类(强类型读取)
|
||||
/// </summary>
|
||||
public static class XmlSerializerHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 从XML文件反序列化为指定类型的对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标实体类型</typeparam>
|
||||
/// <param name="filePath">XML文件路径</param>
|
||||
/// <returns>实体对象</returns>
|
||||
public static T DeserializeFromXml<T>(string filePath) where T : class, new()
|
||||
{
|
||||
// 检查文件是否存在
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
throw new FileNotFoundException("XML配置文件不存在", filePath);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 初始化序列化器
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
||||
// 读取文件并反序列化
|
||||
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||
{
|
||||
return serializer.Deserialize(fs) as T;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("XML反序列化失败,检查XML格式和实体类是否匹配", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将对象序列化为XML并保存到文件
|
||||
/// </summary>
|
||||
/// <typeparam name="T">实体类型</typeparam>
|
||||
/// <param name="obj">实体对象</param>
|
||||
/// <param name="filePath">保存路径</param>
|
||||
public static void SerializeToXml<T>(T obj, string filePath) where T : class
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(obj), "序列化对象不能为null");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
||||
// 写入文件(UTF8编码,避免中文乱码)
|
||||
using (StreamWriter sw = new StreamWriter(filePath, false, System.Text.Encoding.UTF8))
|
||||
{
|
||||
serializer.Serialize(sw, obj);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("XML序列化失败", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
100
SamplePre.Common/LoggerHelper.cs
Normal file
100
SamplePre.Common/LoggerHelper.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SamplePre.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// nLog使用帮助类
|
||||
/// </summary>
|
||||
public class LoggerHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 实例化nLog,即为获取配置文件相关信息(获取以当前正在初始化的类命名的记录器)
|
||||
/// </summary>
|
||||
private readonly NLog.Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private static LoggerHelper _obj;
|
||||
|
||||
public static LoggerHelper Logger
|
||||
{
|
||||
get => _obj ?? (new LoggerHelper());
|
||||
set => _obj = value;
|
||||
}
|
||||
|
||||
#region Debug,调试
|
||||
public void Debug(string msg)
|
||||
{
|
||||
_logger.Debug(msg);
|
||||
}
|
||||
|
||||
public void Debug(string msg, Exception err)
|
||||
{
|
||||
_logger.Debug(err, msg);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Info,信息
|
||||
public void Info(string msg)
|
||||
{
|
||||
_logger.Info(msg);
|
||||
}
|
||||
|
||||
public void Info(string msg, Exception err)
|
||||
{
|
||||
_logger.Info(err, msg);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Warn,警告
|
||||
public void Warn(string msg)
|
||||
{
|
||||
_logger.Warn(msg);
|
||||
}
|
||||
|
||||
public void Warn(string msg, Exception err)
|
||||
{
|
||||
_logger.Warn(err, msg);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Trace,追踪
|
||||
public void Trace(string msg)
|
||||
{
|
||||
_logger.Trace(msg);
|
||||
}
|
||||
|
||||
public void Trace(string msg, Exception err)
|
||||
{
|
||||
_logger.Trace(err, msg);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Error,错误
|
||||
public void Error(string msg)
|
||||
{
|
||||
_logger.Error(msg);
|
||||
}
|
||||
|
||||
public void Error(string msg, Exception err)
|
||||
{
|
||||
_logger.Error(err, msg);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Fatal,致命错误
|
||||
public void Fatal(string msg)
|
||||
{
|
||||
_logger.Fatal(msg);
|
||||
}
|
||||
|
||||
public void Fatal(string msg, Exception err)
|
||||
{
|
||||
_logger.Fatal(err, msg);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
14
SamplePre.Common/SamplePre.Common.csproj
Normal file
14
SamplePre.Common/SamplePre.Common.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NLog" Version="5.2.5" />
|
||||
<PackageReference Include="S7netplus" Version="0.20.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user