静态 Code Review
This commit is contained in:
1 parent
0e2d24c0cd
commit
2744437977
13 files changed
+54
-76
No files matched your search
@@ -4,7 +4,7 @@ namespace MeloongCore;
|
||||
/// 使用粗粒度锁的线程安全 <see cref="List{T}"/> 实现。
|
||||
/// 通常建议使用 <see cref="ConcurrentBag{T}"/> 或其他线程安全集合,它们具有更好的性能。
|
||||
/// </summary>
|
||||
public class ConcurrentList<T> : IList<T>, IList, IEnumerable, IEnumerable<T> {
|
||||
public class ConcurrentList<T> : IList<T>, IList {
|
||||
|
||||
private readonly List<T> _items = [];
|
||||
|
||||
|
||||
@@ -14,7 +14,9 @@ public class JsonConfigProvider : IConfigProvider {
|
||||
public static readonly ConcurrentBag<WeakReference<JsonConfigProvider>> allProviders = [];
|
||||
private readonly string filePath;
|
||||
|
||||
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 'required' 修饰符或声明为可以为 null。
|
||||
public JsonConfigProvider(string filePath) {
|
||||
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 'required' 修饰符或声明为可以为 null。
|
||||
this.filePath = filePath;
|
||||
allProviders.Add(new(this));
|
||||
saveAction = Throttler.Throttle(Save, TimeSpan.FromMilliseconds(500), leading: false, trailing: true);
|
||||
@@ -48,8 +50,8 @@ public class JsonConfigProvider : IConfigProvider {
|
||||
} else if (encrypted) {
|
||||
// 需要加密,在 JSON 中保存密文字符串
|
||||
throw new NotImplementedException("加密需要改为使用识别码,现在尚未实现。");
|
||||
Logger.Trace($"配置已修改:{key} = 已加密({filePath})");
|
||||
json.Value[key] = CryptographyUtils.AesEncrypt(value is string str ? str : JsonConvert.SerializeObject(value));
|
||||
// Logger.Trace($"配置已修改:{key} = 已加密({filePath})");
|
||||
// json.Value[key] = CryptographyUtils.AesEncrypt(value is string str ? str : JsonConvert.SerializeObject(value));
|
||||
} else {
|
||||
// 用 JToken 保留原始结构
|
||||
JToken token = JToken.FromObject(value);
|
||||
|
||||
@@ -122,12 +122,12 @@ public static class Logger {
|
||||
public ref struct LogInterpolatedStringHandler {
|
||||
private StringBuilder? _sb;
|
||||
public readonly bool isEnabled;
|
||||
public LogInterpolatedStringHandler(int literalLength, int formattedCount, LogLevel level, out bool isEnabled) {
|
||||
public LogInterpolatedStringHandler(int literalLength, LogLevel level, out bool isEnabled) {
|
||||
this.isEnabled = isEnabled = Logger.Instance.MinLevel <= level;
|
||||
_sb = this.isEnabled ? new StringBuilder(literalLength) : null;
|
||||
}
|
||||
public void AppendLiteral(string value) => _sb?.Append(value);
|
||||
public void AppendFormatted<T>(T value) => _sb?.Append(value?.ToString());
|
||||
public void AppendFormatted<T>(T value) => _sb?.Append(value);
|
||||
public void AppendFormatted<T>(T value, string? format) => _sb?.Append(value is IFormattable formattable ? formattable.ToString(format, null) : value?.ToString());
|
||||
public void AppendFormatted<T>(T value, int alignment) {
|
||||
if (_sb is null) return;
|
||||
@@ -156,7 +156,7 @@ public static class Logger {
|
||||
public ref struct LogInterpolatedStringHandler<TLevel> where TLevel : struct, ILogLevelProvider {
|
||||
private LogInterpolatedStringHandler _inner;
|
||||
public bool IsEnabled => _inner.isEnabled;
|
||||
public LogInterpolatedStringHandler(int literalLength, int formattedCount, out bool isEnabled) => _inner = new(literalLength, formattedCount, default(TLevel).Level, out isEnabled);
|
||||
public LogInterpolatedStringHandler(int literalLength, int formattedCount, out bool isEnabled) => _inner = new(literalLength, default(TLevel).Level, out isEnabled);
|
||||
public void AppendLiteral(string value) => _inner.AppendLiteral(value);
|
||||
public void AppendFormatted<T>(T value) => _inner.AppendFormatted(value);
|
||||
public void AppendFormatted<T>(T value, string? format) => _inner.AppendFormatted(value, format);
|
||||
|
||||
@@ -7,7 +7,7 @@ public sealed class ValueRange<T>(
|
||||
T? lower, T? upper,
|
||||
bool isLowerInclusive, bool isUpperInclusive,
|
||||
bool hasLower, bool hasUpper
|
||||
) : IEquatable<ValueRange<T>?> where T : notnull, IComparable<T> {
|
||||
) : IEquatable<ValueRange<T>?> where T : IComparable<T> {
|
||||
|
||||
#region 基础字段
|
||||
|
||||
@@ -129,7 +129,7 @@ public sealed class ValueRange<T>(
|
||||
/// 取当前范围与另一个范围的交集。
|
||||
/// </summary>
|
||||
/// <returns> 若两个范围存在交集,则返回交集范围;否则返回 <see langword="null" />。 </returns>
|
||||
public ValueRange<T>? Intersect(ValueRange<T> other) {
|
||||
public ValueRange<T> Intersect(ValueRange<T> other) {
|
||||
T? lower = default;
|
||||
T? upper = default;
|
||||
var isLowerInclusive = true;
|
||||
@@ -200,12 +200,12 @@ public sealed class ValueRange<T>(
|
||||
}
|
||||
public override int GetHashCode() {
|
||||
int hashCode = 169504101;
|
||||
hashCode=hashCode*-1521134295+EqualityComparer<T?>.Default.GetHashCode(Lower);
|
||||
hashCode=hashCode*-1521134295+EqualityComparer<T?>.Default.GetHashCode(Upper);
|
||||
hashCode=hashCode*-1521134295+HasLower.GetHashCode();
|
||||
hashCode=hashCode*-1521134295+HasUpper.GetHashCode();
|
||||
hashCode=hashCode*-1521134295+IsLowerInclusive.GetHashCode();
|
||||
hashCode=hashCode*-1521134295+IsUpperInclusive.GetHashCode();
|
||||
hashCode=hashCode*-1521134295+EqualityComparer<T?>.Default.GetHashCode(lower);
|
||||
hashCode=hashCode*-1521134295+EqualityComparer<T?>.Default.GetHashCode(upper);
|
||||
hashCode=hashCode*-1521134295+hasLower.GetHashCode();
|
||||
hashCode=hashCode*-1521134295+hasUpper.GetHashCode();
|
||||
hashCode=hashCode*-1521134295+isLowerInclusive.GetHashCode();
|
||||
hashCode=hashCode*-1521134295+isUpperInclusive.GetHashCode();
|
||||
return hashCode;
|
||||
}
|
||||
#pragma warning disable CS8604 // 引用类型参数可能为 null。
|
||||
|
||||
@@ -18,7 +18,7 @@ public class RateLimitedWorker(Action workload, double minimalIntervalMs, RateLi
|
||||
private readonly RateLimitedAction action = mode switch {
|
||||
RateLimitMode.ImmediateThenMerge => Throttler.Throttle(workload, TimeSpan.FromMilliseconds(minimalIntervalMs), leading: true, trailing: true),
|
||||
RateLimitMode.WaitUnillNoNewInvoke => Debouncer.Debounce(workload, TimeSpan.FromMilliseconds(minimalIntervalMs), leading: false, trailing: true),
|
||||
RateLimitMode.ImmediateThenIgnore => Debouncer.Debounce(workload, TimeSpan.FromMilliseconds(minimalIntervalMs), leading: true, trailing: false)
|
||||
_ => Debouncer.Debounce(workload, TimeSpan.FromMilliseconds(minimalIntervalMs), leading: true, trailing: false)
|
||||
};
|
||||
|
||||
public void Start() {
|
||||
|
||||
Reference in new issue
Block a user