Code Review 2

This commit is contained in:
龙腾猫跃 committed 2026-05-07 09:20:23 +08:00
1 parent 9158a69b62
commit 368280fcb5
8 files changed
+54 -45

No files matched your search

+9 -10
View File
@@ -6,8 +6,7 @@ public static class EscapeUtils {
/// <summary>
/// WPF XML 转义。
/// </summary>
public static string? XmlEscape(string? str) {
if (str is null) return null;
public static string XmlEscape(string str) {
if (str.StartsWithF("{")) str = "{}" + str; // #4187
return str
.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("'", "&apos;")
@@ -34,7 +33,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="Regex.Escape"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? RegexEscape(string value)
public static string RegexEscape(string value)
=> Regex.Escape(value);
/// <summary>
@@ -42,7 +41,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="Regex.Unescape"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? RegexUnescape(string value)
public static string RegexUnescape(string value)
=> Regex.Unescape(value);
/// <summary>
@@ -50,7 +49,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="Uri.EscapeDataString"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? UrlEscape(string value)
public static string UrlEscape(string value)
=> Uri.EscapeDataString(value);
/// <summary>
@@ -58,7 +57,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="Uri.UnescapeDataString"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? UrlUnescape(string value)
public static string UrlUnescape(string value)
=> Uri.UnescapeDataString(value);
/// <summary>
@@ -66,7 +65,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="WebUtility.UrlEncode"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? FormUrlEscape(string value)
public static string FormUrlEscape(string value)
=> WebUtility.UrlEncode(value);
/// <summary>
@@ -74,7 +73,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="WebUtility.UrlDecode"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? FormUrlUnescape(string value)
public static string FormUrlUnescape(string value)
=> WebUtility.UrlDecode(value);
/// <summary>
@@ -82,7 +81,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="WebUtility.HtmlEncode"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? HtmlEscape(string value)
public static string HtmlEscape(string value)
=> WebUtility.HtmlEncode(value);
/// <summary>
@@ -90,7 +89,7 @@ public static class EscapeUtils {
/// 等同于 <see cref="WebUtility.HtmlDecode"/>。
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string? HtmlUnescape(string value)
public static string HtmlUnescape(string value)
=> WebUtility.HtmlDecode(value);
}
+4 -2
View File
@@ -35,7 +35,7 @@ public static class FileUtils {
/// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。
/// </summary>
public static void Write(string filePath, Stream stream) {
using FileStream fileStream = CreateAsStream(PathUtils.WithLongPath(filePath));
using FileStream fileStream = FileUtils.CreateAsStream(filePath);
if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin);
Logger.Trace(() => $"写入文件:{filePath}({stream.GetType().Name}{(stream.CanSeek ? $" {stream.Length} 字节" : "")})");
stream.CopyTo(fileStream);
@@ -85,7 +85,9 @@ public static class FileUtils {
Run(target);
} else {
System.Runtime.ExceptionServices.ExceptionDispatchInfo? internalEx = null; // 捕获内部异常
var thread = new Thread(() => { try { Run(target); } catch (Exception ex) { internalEx = System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex); } });
var thread = new Thread(
() => { try { Run(target); } catch (Exception ex) { internalEx = System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex); } })
{ IsBackground = true, Name = nameof(DeleteToRecycleBin) };
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
+2 -2
View File
@@ -7,9 +7,9 @@ public class HttpRequestCodeException(string message, HttpStatusCode statusCode,
/// <summary>
/// HTTP 请求的状态码。
/// </summary>
public HttpStatusCode StatusCode { get; set; } = statusCode;
public HttpStatusCode StatusCode { get; } = statusCode;
/// <summary>
/// 远程服务器给予的回复。
/// </summary>
public string? Response { get; set; } = response;
public string? Response { get; } = response;
}