diff --git a/GFramework.Core.Abstractions/registries/IRegistry.cs b/GFramework.Core.Abstractions/registries/IRegistry.cs index d5120fd..74002b8 100644 --- a/GFramework.Core.Abstractions/registries/IRegistry.cs +++ b/GFramework.Core.Abstractions/registries/IRegistry.cs @@ -7,8 +7,18 @@ namespace GFramework.Core.Abstractions.registries; /// /// 注册表中用作键的类型 /// 注册表中存储的值的类型 -public interface IRegistry +public interface IRegistry { + /// + /// 获取注册表中所有的键 + /// + IEnumerable Keys { get; } + + /// + /// 获取注册表中项的数量 + /// + int Count { get; } + /// /// 根据指定的键获取对应的值 /// @@ -35,4 +45,24 @@ public interface IRegistry /// 要添加的键 /// 要添加的值 IRegistry Registry(T key, Tr value); + + /// + /// 从注册表中移除指定键的项 + /// + /// 要移除的键 + /// 如果成功移除则返回true,否则返回false + bool Unregister(T key); + + /// + /// 获取注册表中所有的键值对 + /// + /// 包含所有注册键值对的只读字典 + IReadOnlyDictionary GetAll(); + + + /// + /// 获取注册表中所有的值 + /// + /// 包含所有注册值的只读集合 + IReadOnlyCollection Values(); } \ No newline at end of file diff --git a/GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs b/GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs index 7cbe166..cc97229 100644 --- a/GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs +++ b/GFramework.Core.Abstractions/registries/KeyValueRegistryBase.cs @@ -1,4 +1,5 @@ -using GFramework.Core.Abstractions.bases; +using System.Collections.ObjectModel; +using GFramework.Core.Abstractions.bases; namespace GFramework.Core.Abstractions.registries; @@ -61,6 +62,7 @@ public abstract class KeyValueRegistryBase return this; } + /// /// 注册键值对映射对象到注册表中 /// @@ -70,4 +72,42 @@ public abstract class KeyValueRegistryBase { return Registry(mapping.Key, mapping.Value); } + + /// + /// 从注册表中移除指定键的项 + /// + /// 要移除的键 + /// 如果成功移除则返回true,否则返回false + public bool Unregister(TKey key) + { + return Map.Remove(key); + } + + /// + /// 获取注册表中所有的键值对 + /// + /// 包含所有注册键值对的只读字典 + public IReadOnlyDictionary GetAll() + { + return Map as IReadOnlyDictionary ?? new ReadOnlyDictionary(Map); + } + + /// + /// 获取注册表中所有的值 + /// + /// 包含所有注册值的只读集合 + public IReadOnlyCollection Values() + { + return Map.Values as IReadOnlyCollection ?? new ReadOnlyCollection([]); + } + + /// + /// 获取注册表中所有的键 + /// + public IEnumerable Keys => Map.Keys; + + /// + /// 获取注册表中项的数量 + /// + public int Count => Map.Count; } \ No newline at end of file