Skip to content

Commit b174c47

Browse files
committed
feat(数据表): 添加TryGet方法以安全获取数据
为BaseDataTable和IDataTable接口添加TryGet方法,支持通过int、long和string类型的ID安全获取数据。当数据不存在时返回false而不是抛出异常,提高代码健壮性。
1 parent 842958e commit b174c47

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

Runtime/Config/Config/BaseDataTable.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ public T Get(string id)
3333
return value;
3434
}
3535

36+
public bool TryGet(int id, out T value)
37+
{
38+
return LongDataMaps.TryGetValue(id, out value);
39+
}
40+
41+
public bool TryGet(long id, out T value)
42+
{
43+
return LongDataMaps.TryGetValue(id, out value);
44+
}
45+
46+
public bool TryGet(string id, out T value)
47+
{
48+
return StringDataMaps.TryGetValue(id, out value);
49+
}
50+
3651
public T this[int index]
3752
{
3853
get

Runtime/Config/Config/IDataTable.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ public interface IDataTable<T> : IDataTable where T : class
4848
/// <returns></returns>
4949
T Get(string id);
5050

51+
/// <summary>
52+
/// 尝试根据整数ID获取对象
53+
/// </summary>
54+
/// <param name="id">要获取的对象的整数ID</param>
55+
/// <param name="value">当找到对应ID的对象时,返回该对象;否则返回默认值</param>
56+
/// <returns>如果找到对应ID的对象则返回true,否则返回false</returns>
57+
bool TryGet(int id, out T value);
58+
59+
/// <summary>
60+
/// 尝试根据长整数ID获取对象
61+
/// </summary>
62+
/// <param name="id">要获取的对象的长整数ID</param>
63+
/// <param name="value">当找到对应ID的对象时,返回该对象;否则返回默认值</param>
64+
/// <returns>如果找到对应ID的对象则返回true,否则返回false</returns>
65+
bool TryGet(long id, out T value);
66+
67+
/// <summary>
68+
/// 尝试根据字符串ID获取对象
69+
/// </summary>
70+
/// <param name="id">要获取的对象的字符串ID</param>
71+
/// <param name="value">当找到对应ID的对象时,返回该对象;否则返回默认值</param>
72+
/// <returns>如果找到对应ID的对象则返回true,否则返回false</returns>
73+
bool TryGet(string id, out T value);
74+
5175
/// <summary>
5276
/// 根据列表索引获取对象
5377
/// </summary>

0 commit comments

Comments
 (0)