Dictionary<TKey, TValue>.ContainsKey 方法 不区分大小写

本文介绍如何使用C#创建一个大小写不敏感的字符串键对象字典。通过使用StringComparer.OrdinalIgnoreCase,可以确保键名在比较时不区分大小写。
Dictionary <string, object> dictionary = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);


using System; using System.Collections.Generic; using LitJson; using UnityEngine; [Serializable] public class CustomTable { public Dictionary<string, object> Msgs; public CustomTable() { Msgs = new Dictionary<string, object>(); } public void AddMsg(string name, object value) { if (Msgs.ContainsKey(name)) Msgs[name] = value; else Msgs.Add(name, value); } public bool ContainsKey(string name) { return Msgs.ContainsKey(name); } public T GetMsg<T>(string name) { if (Msgs.ContainsKey(name)) return (T)Msgs[name]; Debug.Log($"Msgs '{name}' does not exist in the table."); return default(T); } public T GetMsg<T>(string name, out object obj) { if (Msgs.TryGetValue(name, out obj)) return (T)obj; //if (Msgs.ContainsKey(name)) return (T)Msgs[name]; Debug.Log($"Msgs '{name}' does not exist in the table."); return default(T); } public object GetMsg(string name, string typeName) { if (Msgs.ContainsKey(name)) { Type targetType = Type.GetType(typeName); var value = Convert.ChangeType(Msgs[name], targetType); return value; } Debug.Log($"Msgs '{name}' does not exist in the table."); return null; } public object GetMsg(string name) { if (Msgs.ContainsKey(name)) return Msgs[name]; Debug.Log($"Msgs '{name}' does not exist in the table."); return null; } public void SetMsg(string name, object value) { if (Msgs.ContainsKey(name)) Msgs[name] = value; } public string ToJson() { string json = JsonMapper.ToJson(Msgs); if (!string.IsNullOrEmpty(json)) return json; Debug.Log("Data serialization failed."); return string.Empty; } public static string ToJson(CustomTable table) { string json = JsonMapper.ToJson(table.Msgs); if (!string.IsNullOrEmpty(json)) return json; Debug.Log("Data serialization failed."); return string.Empty; } public string ToJsonOther() { var dict = new Dictionary<string, object>(); foreach (var kvp in Msgs) { dict.Add(kvp.Key, kvp.Value); } string json = JsonMapper.ToJson(dict); if (!string.IsNullOrEmpty(json)) return json; Debug.Log("Data serialization failed."); return string.Empty; } public static string ToJsonOther(CustomTable table) { var dict = new Dictionary<string, object>(); foreach (var kvp in table.Msgs) { dict.Add(kvp.Key, kvp.Value); } string json = JsonMapper.ToJson(dict); if (!string.IsNullOrEmpty(json)) return json; Debug.Log("Data serialization failed."); return string.Empty; } public static CustomTable GetTable(string json) { try { CustomTable table = JsonMapper.ToObject<CustomTable>(json); return table; } catch (Exception e) { Debug.Log($"When parsing fails, the reason is:{e.Message}"); return null; } } public static CustomTable GetTableOther(string json) { try { var dict = JsonMapper.ToObject<Dictionary<string, object>>(json); CustomTable table = new CustomTable(); foreach (var kvp in dict) { table.AddMsg(kvp.Key, kvp.Value); } return table; } catch (Exception e) { Debug.Log($"When parsing fails, the reason is:{e.Message}"); return null; } } }
09-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值