添加项目文件。

This commit is contained in:
chfei committed 2026-08-14 18:00:58 +08:00
1 parent 6a017c9547
commit 58abbb5b4f
86 files changed
+8729

No files matched your search

@@ -0,0 +1,76 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace StoreHub.Framework.Helpers;
public static class ObjectHelper
{
private static readonly ConcurrentDictionary<string, PropertyInfo> CachedObjectProperties =
new();
public static void TrySetProperty<TObject, TValue>(
TObject obj,
Expression<Func<TObject, TValue>> propertySelector,
Func<TValue> valueFactory,
params Type[] ignoreAttributeTypes
)
{
TrySetProperty(obj, propertySelector, x => valueFactory(), ignoreAttributeTypes);
}
public static void TrySetProperty<TObject, TValue>(
TObject obj,
Expression<Func<TObject, TValue>> propertySelector,
Func<TObject, TValue> valueFactory,
params Type[] ignoreAttributeTypes
)
{
string cacheKey =
$"{obj?.GetType().FullName}-"
+ $"{propertySelector}-"
+ $"{(ignoreAttributeTypes != null ? "-" + string.Join("-", ignoreAttributeTypes.Select(x => x.FullName)) : "")}";
PropertyInfo property = CachedObjectProperties.GetOrAdd(
cacheKey,
(name) =>
{
if (propertySelector.Body.NodeType != ExpressionType.MemberAccess)
{
return null;
}
MemberExpression memberExpression = (MemberExpression)propertySelector.Body;
PropertyInfo? propertyInfo = obj?.GetType()
.GetProperties()
.FirstOrDefault(
x =>
x.Name == memberExpression.Member.Name
&& x.GetSetMethod(true) != null
);
if (propertyInfo == null)
{
return null;
}
if (
ignoreAttributeTypes != null
&& ignoreAttributeTypes.Any(
ignoreAttribute => propertyInfo.IsDefined(ignoreAttribute, true)
)
)
{
return null;
}
return propertyInfo;
}
);
property?.SetValue(obj, valueFactory(obj));
}
}
+156
View File
@@ -0,0 +1,156 @@
using MAX.Core.Commons;
using System;
using System.Diagnostics;
using System.Text;
namespace StoreHub.Framework.Helpers;
public class WinCmdHelper
{
#region 执行Dos命令
/// <summary>
/// 执行Dos命令
/// </summary>
/// <param name="cmd">Dos命令及参数</param>
/// <param name="isShowCmdWindow">是否显示cmd窗口</param>
/// <param name="isCloseCmdProcess">执行完毕后是否关闭cmd进程</param>
/// <returns>成功返回true,失败返回false</returns>
public static Result ExcuteDosCommand(string cmd, string workdir, bool isShowCmdWindow = false, bool isWaitForExit = false, bool isCloseCmdProcess = true)
{
Result result = new();
try
{
StringBuilder errString = new();
StringBuilder outString = new();
Process p = new();
p.StartInfo.FileName = "cmd";
p.StartInfo.WorkingDirectory = workdir;
p.StartInfo.Arguments = "/c " + cmd;
p.StartInfo.StandardOutputEncoding = Encoding.UTF8;
p.StartInfo.StandardErrorEncoding = Encoding.UTF8;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = !isShowCmdWindow;
p.OutputDataReceived += new DataReceivedEventHandler(delegate (object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
outString.AppendLine(e.Data);
Debug.WriteLine(e.Data);
}
});
p.ErrorDataReceived += new DataReceivedEventHandler(delegate (object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
errString.AppendLine(e.Data);
}
});
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
if (isWaitForExit)
{
p.WaitForExit();
}
if (isCloseCmdProcess)
{
p.Close();
}
if (errString.Length <= 0)
{
result.Success = true;
result.Data = outString.ToString();
}
Console.WriteLine(string.Format("成功执行Dos命令[{0}]!", cmd));
return result;
}
catch (Exception ex)
{
Console.WriteLine("执行命令失败,请检查输入的命令是否正确:" + ex.Message, ex);
result.Msg = ex.Message;
}
return result;
}
private static void P_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
throw new NotImplementedException();
}
#endregion
#region 判断指定的进程是否在运行中
/// <summary>
/// 判断指定的进程是否在运行中
/// </summary>
/// <param name="processName">要判断的进程名称,不包括扩展名exe</param>
/// <param name="processFileName">进程文件的完整路径</param>
/// <returns>存在返回true,否则返回false</returns>
public static bool CheckProcessExists(string processName, string processFileName)
{
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process p in processes)
{
if (!string.IsNullOrEmpty(processFileName))
{
if (processFileName == p?.MainModule?.FileName)
{
return true;
}
}
else
{
return true;
}
}
return false;
}
#endregion
#region 结束指定的windows进程
/// <summary>
/// 结束指定的windows进程,如果进程存在
/// </summary>
/// <param name="processName">进程名称,不包含扩展名</param>
/// <param name="processFileName">进程文件完整路径,如果为空则删除所有进程名为processName参数值的进程</param>
public static bool KillProcessExists(string processName, string processFileName)
{
try
{
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process p in processes)
{
if (!string.IsNullOrEmpty(processFileName))
{
if (processFileName == p.MainModule.FileName)
{
p.Kill();
p.Close();
}
}
else
{
p.Kill();
p.Close();
}
}
Console.WriteLine(string.Format("成功结束[{0}]进程!", processes));
return true;
}
catch (Exception ex)
{
Console.WriteLine("结束指定的Widnows进程异常:" + ex.Message, ex);
return false;
}
}
#endregion
}