SafeDictionary.cs 921 B

123456789101112131415161718192021222324252627
  1. using System.Collections.Generic;
  2. namespace TapSDK.Core
  3. {
  4. public static class SafeDictionary
  5. {
  6. public static T GetValue<T> (Dictionary<string, object> dic, string key, T defaultVal = default(T))
  7. {
  8. if (dic == null || dic.Keys.Count == 0) return default(T);
  9. if (!dic.TryGetValue(key, out var outputValue))
  10. return defaultVal;
  11. if(typeof(T) == typeof(int)){
  12. return (T)(object)int.Parse(outputValue.ToString());
  13. }
  14. if(typeof(T) == typeof(double)){
  15. return (T)(object)double.Parse(outputValue.ToString());
  16. }
  17. if(typeof(T) == typeof(long)){
  18. return (T)(object)long.Parse(outputValue.ToString());
  19. }
  20. if(typeof(T) == typeof(bool)){
  21. return (T)outputValue;
  22. }
  23. return (T) outputValue;
  24. }
  25. }
  26. }