ObservableDictionary<TKey, TValue>¶
- 种类:
class - 命名空间:
Runestone.AesirArchitecture - 程序集:
Runestone.AesirArchitecture
继承链: System.Object → ObservableDictionary<TKey, TValue>
实现接口: System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>,System.Collections.Generic.IDictionary<TKey, TValue>,Runestone.AesirArchitecture.IReadOnlyObservableDictionary<TKey, TValue>,System.Collections.Generic.IEnumerable<KeyValuePair<TKey, TValue>>,System.Collections.IEnumerable,Runestone.AesirArchitecture.IObservableCollection<KeyValuePair<TKey, TValue>>,System.Collections.Generic.ICollection<KeyValuePair<TKey, TValue>>,System.Collections.Generic.IReadOnlyCollection<KeyValuePair<TKey, TValue>>,Runestone.AesirArchitecture.IObservableDictionary<TKey, TValue>
类型参数
TKey— 键类型TValue— 值类型
声明¶
[DefaultMember]
[Serializable]
public sealed class ObservableDictionary<TKey, TValue> : System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>,
System.Collections.Generic.IDictionary<TKey, TValue>,
Runestone.AesirArchitecture.IReadOnlyObservableDictionary<TKey, TValue>,
System.Collections.Generic.IEnumerable<KeyValuePair<TKey, TValue>>,
System.Collections.IEnumerable,
Runestone.AesirArchitecture.IObservableCollection<KeyValuePair<TKey, TValue>>,
System.Collections.Generic.ICollection<KeyValuePair<TKey, TValue>>,
System.Collections.Generic.IReadOnlyCollection<KeyValuePair<TKey, TValue>>,
Runestone.AesirArchitecture.IObservableDictionary<TKey, TValue>
可观察字典实现。 Model 层持有可写实例,View 层通过 IReadOnlyObservableDictionary{TKey, TValue} 只读订阅。
备注
内部组合 Dictionary{TKey,TValue} 存储键值,变更通知经 MiniEvent{T} 分发——Invoke 路径零分配(直接多播调用)。注意:订阅路径(AddListener / 句柄创建)有与监听者数量成正比的委托分配, 勿在每帧订阅场景使用。 [SerializeField] 标记 dictionary 字段——Unity 原生不序列化 Dictionary{TKey, TValue} (该标记对 Unity 序列化无效果),安装 Odin Inspector 且宿主走 Odin 序列化(如 AesirMonoBehaviour 派生组件)时, 该字段可被 Odin 序列化管线接管,便于在 Inspector 中编辑初始键值。
变更通知为单一事件(AddListener),载荷为 CollectionChangedEventArgs{T} (T = KeyValuePair{TKey,TValue}):写操作完成后才触发,监听者回调中读取到的集合已是变更后的状态; 索引器为不存在的键赋值触发 Add、为已有键赋新值触发 Replace(旧值见载荷 OldItem)、赋相同值不通知; Remove 不存在的键、Clear 空字典不通知。字典无索引概念,载荷索引固定 -1。
遍历性能:foreach 具体类型走结构体枚举器,零分配;通过 IReadOnlyObservableDictionary{TKey, TValue} / IEnumerable{T} 接口遍历会装箱一次枚举器(与 BCL Dictionary{TKey, TValue} 行为一致)。
构造方法¶
| 名称 | 描述 |
|---|---|
ObservableDictionary() |
默认构造,创建空字典。 |
ObservableDictionary(IEnumerable<KeyValuePair<TKey, TValue>>) |
指定初始键值构造。初始键值不触发变更通知(语义同反序列化填充)。 |
ObservableDictionary(IEqualityComparer<TKey>) |
指定键比较器构造。 |
ObservableDictionary()¶
默认构造,创建空字典。
ObservableDictionary(IEnumerable<KeyValuePair>)¶
指定初始键值构造。初始键值不触发变更通知(语义同反序列化填充)。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
initialItems |
IEnumerable<KeyValuePair<TKey, TValue>> |
初始键值序列。 |
ObservableDictionary(IEqualityComparer)¶
指定键比较器构造。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
comparer |
IEqualityComparer<TKey> |
键比较器;为 null 时使用 EqualityComparer{TKey}.Default。 |
属性¶
| 名称 | 描述 |
|---|---|
Keys |
所有键的集合。 |
Values |
所有值的集合。 |
Comparer |
内部 Dictionary{TKey, TValue} 使用的键比较器。 |
Item |
读写指定键的值。 读取:键不存在时抛 KeyNotFoundException(fail-fast)。 写入:键不存在时添加并触发 Add;键已存在且新值不同时更新并触发 Replace(旧值见载荷 OldItem);值相同不通知。 |
IsReadOnly |
固定返回 false,该集合可写。 |
Count |
键值对数量。 |
Keys¶
所有键的集合。
Values¶
所有值的集合。
Comparer¶
内部 Dictionary{TKey, TValue} 使用的键比较器。
Item¶
读写指定键的值。 读取:键不存在时抛 KeyNotFoundException(fail-fast)。
写入:键不存在时添加并触发 Add;键已存在且新值不同时更新并触发 Replace(旧值见载荷 OldItem);值相同不通知。
IsReadOnly¶
固定返回 false,该集合可写。
Count¶
键值对数量。
方法¶
声明的方法
| 名称 | 描述 |
|---|---|
AddListener(Action<CollectionChangedEventArgs<KeyValuePair<TKey, TValue>>>) |
— |
GetEnumerator() |
返回遍历键值对的结构体枚举器,foreach 具体类型时零分配。 |
Contains(KeyValuePair<TKey, TValue>) |
判断是否包含指定键值对(键存在且值相等)。 |
ContainsKey(TKey) |
判断是否包含指定键。 |
Remove(KeyValuePair<TKey, TValue>) |
移除与指定键值对匹配的项(键存在且值相等),成功时触发 Remove 通知。 |
Remove(TKey) |
移除指定键的键值对,成功时触发 Remove 通知(参数含被移除的键值对)。 |
TryGetValue(TKey, ref TValue) |
获取与指定键关联的值。 |
Add(KeyValuePair<TKey, TValue>) |
添加键值对,触发 Add 通知。键已存在时抛 ArgumentException(fail-fast)。 |
Add(TKey, TValue) |
添加键值对,触发 Add 通知。键已存在时抛 ArgumentException(fail-fast)。 |
Clear() |
清空字典。字典非空时以 Reset 通知;已为空时不通知。 |
ClearListeners() |
清空所有变更监听。 |
CopyTo(KeyValuePair<TKey, TValue>[], int) |
从指定数组索引开始复制键值对到目标数组。 |
RemoveListener(Action<CollectionChangedEventArgs<KeyValuePair<TKey, TValue>>>) |
— |
继承的方法
| 名称 | 描述 | 声明类型 |
|---|---|---|
GetType() |
— | object |
Equals(object) |
— | object |
GetHashCode() |
— | object |
ToString() |
— | object |
MemberwiseClone() |
— | object |
Finalize() |
— | object |
AddListener(Action<CollectionChangedEventArgs<KeyValuePair>>)¶
public AutoRemoveListenerHandle AddListener(Action<CollectionChangedEventArgs<KeyValuePair<TKey, TValue>>> callback)
参数
| 名称 | 类型 | 说明 |
|---|---|---|
callback |
Action<CollectionChangedEventArgs<KeyValuePair<TKey, TValue>>> |
— |
返回值
| 类型 | 说明 |
|---|---|
AutoRemoveListenerHandle |
— |
GetEnumerator()¶
返回遍历键值对的结构体枚举器,foreach 具体类型时零分配。
返回值
| 类型 | 说明 |
|---|---|
ObservableDictionary<TKey, TValue>.Enumerator<TKey, TValue> |
键值对枚举器。 |
Contains(KeyValuePair)¶
判断是否包含指定键值对(键存在且值相等)。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
item |
KeyValuePair<TKey, TValue> |
要查找的键值对。 |
返回值
| 类型 | 说明 |
|---|---|
bool |
包含返回 true,否则返回 false。 |
ContainsKey(TKey)¶
判断是否包含指定键。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
key |
TKey |
要查找的键。 |
返回值
| 类型 | 说明 |
|---|---|
bool |
包含返回 true,否则返回 false。 |
Remove(KeyValuePair)¶
移除与指定键值对匹配的项(键存在且值相等),成功时触发 Remove 通知。
备注
不复用 Remove(TKey)——其按键删除不校验值;此处先验证键值对完全匹配再移除,避免误删同键不同值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
item |
KeyValuePair<TKey, TValue> |
要移除的键值对。 |
返回值
| 类型 | 说明 |
|---|---|
bool |
找到并移除返回 true;未匹配时不触发通知,返回 false。 |
Remove(TKey)¶
移除指定键的键值对,成功时触发 Remove 通知(参数含被移除的键值对)。
备注
使用 Remove(TKey, out TValue) 在移除的同时取回旧值,单次哈希查找。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
key |
TKey |
要移除的键。 |
返回值
| 类型 | 说明 |
|---|---|
bool |
找到并移除返回 true;键不存在时不触发通知,返回 false。 |
TryGetValue(TKey, ref TValue)¶
获取与指定键关联的值。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
key |
TKey |
要查找的键。 |
value |
ref TValue |
键存在时为关联的值,否则为 TValue 的默认值。 |
返回值
| 类型 | 说明 |
|---|---|
bool |
键存在返回 true,否则返回 false。 |
Add(KeyValuePair)¶
添加键值对,触发 Add 通知。键已存在时抛 ArgumentException(fail-fast)。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
item |
KeyValuePair<TKey, TValue> |
要添加的键值对。 |
Add(TKey, TValue)¶
添加键值对,触发 Add 通知。键已存在时抛 ArgumentException(fail-fast)。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
key |
TKey |
键。 |
value |
TValue |
值。 |
Clear()¶
清空字典。字典非空时以 Reset 通知;已为空时不通知。
ClearListeners()¶
清空所有变更监听。
备注
清除全部监听引用,防止因监听者未释放导致的内存泄漏。 与 Clear 不同——后者清空的是字典键值。
CopyTo(KeyValuePair[], int)¶
从指定数组索引开始复制键值对到目标数组。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
array |
KeyValuePair<TKey, TValue>[] |
目标数组。 |
arrayIndex |
int |
目标数组起始索引。 |
RemoveListener(Action<CollectionChangedEventArgs<KeyValuePair>>)¶
参数
| 名称 | 类型 | 说明 |
|---|---|---|
callback |
Action<CollectionChangedEventArgs<KeyValuePair<TKey, TValue>>> |
— |
Additional Notes¶
首个
## Additional Notes是增量生成文档标识符,请勿修改标题级别和内容!本文档由Script Doc Generator辅助生成。