|
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using UnityEngine;
-
- namespace SUISS.Core
- {
- public static class IDictionaryExtensions
- {
- public static TValue Get<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
- {
- TValue result;
- if (dictionary.TryGetValue(key, out result))
- {
- return result;
- }
- return defaultValue;
- }
-
- public static string GetString<TKey, TValue>(this IDictionary<TKey, TValue> 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<TKey, TValue>(this IDictionary<TKey, TValue> 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<TKey, TValue>(this IDictionary<TKey, TValue> 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 float GetFloat<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, float defaultValue)
- {
- TValue tvalue;
- if (dictionary.TryGetValue(key, out tvalue))
- {
- try
- {
- return Convert.ToSingle(tvalue, CultureInfo.InvariantCulture);
- }
- catch (Exception exception)
- {
- UnityEngine.Debug.LogException(exception);
- }
- return defaultValue;
- }
- return defaultValue;
- }
-
- public static decimal GetDecimal<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, decimal defaultValue)
- {
- TValue tvalue;
- if (dictionary.TryGetValue(key, out tvalue))
- {
- try
- {
- return Convert.ToDecimal(tvalue, CultureInfo.InvariantCulture);
- }
- catch (Exception exception)
- {
- UnityEngine.Debug.LogException(exception);
- }
- return defaultValue;
- }
- return defaultValue;
- }
-
- public static double GetDouble<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, double defaultValue)
- {
- TValue tvalue;
- if (dictionary.TryGetValue(key, out tvalue))
- {
- try
- {
- return Convert.ToDouble(tvalue, CultureInfo.InvariantCulture);
- }
- catch (Exception exception)
- {
- UnityEngine.Debug.LogException(exception);
- }
- return defaultValue;
- }
- return defaultValue;
- }
-
- public static bool GetBoolean<TKey, TValue>(this IDictionary<TKey, TValue> 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<Key, Value>(this IDictionary<Key, Value> dict, Func<KeyValuePair<Key, Value>, bool> predicate)
- {
- List<Key> list = new List<Key>();
- foreach (KeyValuePair<Key, Value> arg in dict)
- {
- if (predicate(arg))
- {
- list.Add(arg.Key);
- }
- }
- foreach (Key key in list)
- {
- dict.Remove(key);
- }
- }
- }
- }
|