GeWuYou b49079de3e style(coding-style): 统一代码风格并优化文档格式
- 移除多余using语句和空行,统一代码缩进格式
- 优化注释文档中的缩进和对齐格式
- 简化条件语句和方法实现,提升代码可读性
- 重构协程系统相关类的字段和方法定义格式
- 更新架构服务中容器访问方式的实现
- 调整异步操作类的属性和方法组织结构
- [skip ci]
2026-01-27 20:30:04 +08:00

74 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GFramework.Core.Abstractions.bases;
using GFramework.Game.Abstractions.registry;
namespace GFramework.Core.Abstractions.registries;
/// <summary>
/// 基于Dictionary的通用键值注册表基类
/// 提供基于字典的键值对注册、查询和管理功能
/// </summary>
/// <typeparam name="TKey">键的类型</typeparam>
/// <typeparam name="TValue">值的类型</typeparam>
public abstract class KeyValueRegistryBase<TKey, TValue>
: IRegistry<TKey, TValue>
{
/// <summary>
/// 存储键值对映射关系的字典
/// </summary>
protected readonly IDictionary<TKey, TValue> Map;
/// <summary>
/// 初始化KeyValueRegistryBase的新实例
/// </summary>
/// <param name="comparer">用于比较键的相等性的比较器如果为null则使用默认比较器</param>
protected KeyValueRegistryBase(IEqualityComparer<TKey>? comparer = null)
{
// 使用指定的比较器或默认比较器创建字典
Map = new Dictionary<TKey, TValue>(comparer ?? EqualityComparer<TKey>.Default);
}
/// <summary>
/// 根据指定的键获取对应的值
/// </summary>
/// <param name="key">要查找的键</param>
/// <returns>与键关联的值</returns>
/// <exception cref="KeyNotFoundException">当键不存在时抛出异常</exception>
public virtual TValue Get(TKey key)
{
return Map.TryGetValue(key, out var value)
? value
: throw new KeyNotFoundException($"{GetType().Name}: key not registered: {key}");
}
/// <summary>
/// 判断是否包含指定的键
/// </summary>
/// <param name="key">要检查的键</param>
/// <returns>如果包含该键返回true否则返回false</returns>
public virtual bool Contains(TKey key)
{
return Map.ContainsKey(key);
}
/// <summary>
/// 注册键值对到注册表中
/// </summary>
/// <param name="key">要注册的键</param>
/// <param name="value">要注册的值</param>
/// <returns>当前注册表实例,支持链式调用</returns>
public virtual IRegistry<TKey, TValue> Registry(TKey key, TValue value)
{
Map.Add(key, value);
return this;
}
/// <summary>
/// 注册键值对映射对象到注册表中
/// </summary>
/// <param name="mapping">包含键值对的映射对象</param>
/// <returns>当前注册表实例,支持链式调用</returns>
public virtual IRegistry<TKey, TValue> Registry(IKeyValue<TKey, TValue> mapping)
{
return Registry(mapping.Key, mapping.Value);
}
}