using System; using System.Collections.Generic; using System.Globalization; using UnityEngine; namespace SUISS.Cloud.Extensions { public static class IDictionaryExtensions { public static TValue Get(this IDictionary dictionary, TKey key, TValue defaultValue) { TValue result; if (dictionary.TryGetValue(key, out result)) { return result; } return defaultValue; } public static string GetString(this IDictionary dictionary, TKey key, string defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { string result = defaultValue; try { result = Convert.ToString(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return result; } return defaultValue; } public static int GetInt(this IDictionary dictionary, TKey key, int defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { int result = defaultValue; try { result = Convert.ToInt32(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return result; } return defaultValue; } public static long GetLong(this IDictionary dictionary, TKey key, long defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { long result = defaultValue; try { result = Convert.ToInt64(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return result; } return defaultValue; } public static bool GetBoolean(this IDictionary dictionary, TKey key, bool defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { bool result = defaultValue; try { result = Convert.ToBoolean(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return result; } return defaultValue; } public static void RemoveAll(this IDictionary dict, Predicate> predicate) { List list = new List(); foreach (KeyValuePair obj in dict) { if (predicate(obj)) { list.Add(obj.Key); } } foreach (Key key in list) { dict.Remove(key); } } } }